Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cathood0/flutter_quill_delta_from_html
Convert easily HTML inputs content to Quill Delta format
https://github.com/cathood0/flutter_quill_delta_from_html
delta delta-from-html flutter-quill html parser quill
Last synced: 2 months ago
JSON representation
Convert easily HTML inputs content to Quill Delta format
- Host: GitHub
- URL: https://github.com/cathood0/flutter_quill_delta_from_html
- Owner: CatHood0
- License: mit
- Created: 2024-07-06T02:31:11.000Z (6 months ago)
- Default Branch: master
- Last Pushed: 2024-09-15T20:36:19.000Z (4 months ago)
- Last Synced: 2024-10-13T08:45:00.982Z (3 months ago)
- Topics: delta, delta-from-html, flutter-quill, html, parser, quill
- Language: Dart
- Homepage: https://pub.dev/packages/flutter_quill_delta_from_html
- Size: 106 KB
- Stars: 2
- Watchers: 1
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Flutter Quill Delta from HTML
This is a **Dart** package that converts **HTML** input into Quill **Delta** format, which is used in the [Quill Js](https://quilljs.com/) package.
**This package** supports the conversion of a wide range of **HTML** tags and attributes into their corresponding **Delta** operations, ensuring that your **HTML** content is accurately represented in the **Quill editor**.
## Supported tags
```html
, : Bold text
, : Italic text
, : Underlined text
,: Strikethrough text
: Superscript text
: Subscript text
to
: Headings of various levels
- : Unordered lists
- : List items
- : Check lists
: Another alternative to make a check lists
: Hyperlinks with support for the href attribute
: Images with support for the src, align, and styles
: HTML tag containers
: Videos with support for the src
: Block quotations
,
: Code blocks
: Paragraph style alignment
: Paragraph alignment
: Paragraph direction
: Inline attributes
: Custom html
```## Getting Started
Add the dependency to your pubspec.yaml:
```yaml
dependencies:
flutter_quill_delta_from_html: ^1.4.1
```Then, import the package and use it in your Flutter application:
```dart
import 'package:flutter_quill_delta_from_html/flutter_quill_delta_from_html.dart';void main() {
String htmlContent = "Hello, world!
";
var delta = HtmlToDelta().convert(htmlContent);
/*
{ "insert": "hello, " },
{ "insert": "world", "attributes": {"bold": true} },
{ "insert": "!" },
{ "insert": "\n" }
*/
}
```## Creating your own `CustomHtmlPart` (alternative to create `CustomBlockEmbeds` from custom html)
First you need to define your own `CustomHtmlPart`
```dart
import 'package:flutter_quill_delta_from_html/flutter_quill_delta_from_html.dart';
import 'package:html/dom.dart' as dom;/// Custom block handler for elements.
class PullquoteBlock extends CustomHtmlPart {
@override
bool matches(dom.Element element) {
//you can put here the validation that you want
//
// To detect a, you just need to do something like:
// element.localName == 'p'
return element.localName == 'pullquote';
}@override
List convert(dom.Element element, {Map? currentAttributes}) {
final Delta delta = Delta();
final Map attributes = currentAttributes != null ? Map.from(currentAttributes) : {};// Extract custom attributes from the element
// The attributes represents the data into a html tag
// at this point, should have these attributes
//
//
// These attributes can be optional, so do you need to ensure to not use "!"
// to avoid any null conflict
final author = element.attributes['data-author'];
final style = element.attributes['data-style'];// Apply custom attributes to the Delta operations
if (author != null) {
delta.insert('Pullquote: "${element.text}" by $author', attributes);
} else {
delta.insert('Pullquote: "${element.text}"', attributes);
}if (style != null && style.toLowerCase() == 'italic') {
attributes['italic'] = true;
}delta.insert('\n', attributes);
return delta.toList();
}
}
```After, put your `PullquoteBlock` to `HtmlToDelta` using the param `customBlocks`
```dart
import 'package:flutter_quill_delta_from_html/flutter_quill_delta_from_html.dart';void main() {
// Example HTML snippet
final htmlText = '''
Regular paragraph before the custom block
This is a custom pullquote
Regular paragraph after the custom block
''';// Registering the custom block
final customBlocks = [PullquoteBlock()];// Initialize HtmlToDelta with the HTML text and custom blocks
final converter = HtmlToDelta(customBlocks: customBlocks);// Convert HTML to Delta operations
final delta = converter.convert(htmlText);
/*
This should be resulting delta
{"insert": "Regular paragraph before the custom block"},
{"insert": "Pullquote: \"This is a custom pullquote\" by John Doe", "attributes": {"italic": true}},
{"insert": "\n"},
{"insert": "Regular paragraph after the custom block\n"}
*/
}
```## HtmlOperations
The `HtmlOperations` class is designed to streamline the conversion process from `HTML` to `Delta` operations, accommodating a wide range of `HTML` structures and attributes commonly used in web content.
To utilize `HtmlOperations`, extend this class and implement the methods necessary to handle specific `HTML` elements. Each method corresponds to a different `HTML` tag or element type and converts it into Delta operations suitable for use with `QuillJS`.
```dart
abstract class HtmlOperations {
/// custom blocks are passed internally by HtmlToDelta
List? customBlocks;// You don't need to override this method
// since it just call the other methods
// when detect the type of HTML tag
List resolveCurrentElement(dom.Element element, [int indentLevel = 0]);List brToOp(dom.Element element);
List headerToOp(dom.Element element);
List listToOp(dom.Element element, [int indentLevel = 0]);
List paragraphToOp(dom.Element element);
List linkToOp(dom.Element element);
List spanToOp(dom.Element element);
List imgToOp(dom.Element element);
List videoToOp(dom.Element element);
List codeblockToOp(dom.Element element);
List blockquoteToOp(dom.Element element);
}
```## Contributions
If you find a bug or want to add a new feature, please open an issue or submit a pull request on the [GitHub repository](https://github.com/CatHood0/flutter_quill_delta_from_html).
This project is licensed under the MIT License - see the [LICENSE](https://github.com/CatHood0/flutter_quill_delta_from_html/blob/Main/LICENSE) file for details.
- : Ordered lists