Skip to content

Commit

Permalink
feat: add em and divider support to html converter (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xazin authored Mar 24, 2023
1 parent 7790b12 commit 06240a0
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/src/infra/html_converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class HTMLTag {
static const image = "img";
static const anchor = "a";
static const italic = "i";
static const em = "em";
static const bold = "b";
static const underline = "u";
static const del = "del";
Expand All @@ -29,6 +30,7 @@ class HTMLTag {
static const code = "code";
static const blockQuote = "blockquote";
static const div = "div";
static const divider = "hr";

static bool isTopLevel(String tag) {
return tag == h1 ||
Expand Down Expand Up @@ -69,6 +71,7 @@ class HTMLToNodesConverter {
child.localName == HTMLTag.strong ||
child.localName == HTMLTag.underline ||
child.localName == HTMLTag.italic ||
child.localName == HTMLTag.em ||
child.localName == HTMLTag.del) {
_handleRichTextElement(delta, child);
} else if (child.localName == HTMLTag.bold) {
Expand Down Expand Up @@ -130,13 +133,16 @@ class HTMLToNodesConverter {
return [_handleParagraph(element, attributes)];
} else if (element.localName == HTMLTag.image) {
return [_handleImage(element)];
} else if (element.localName == HTMLTag.divider) {
return [_handleDivider()];
} else {
final delta = Delta();
delta.insert(element.text);
if (delta.isNotEmpty) {
return [TextNode(delta: delta)];
}
}

return [];
}

Expand All @@ -148,6 +154,8 @@ class HTMLToNodesConverter {
return node;
}

Node _handleDivider() => Node(type: 'divider');

Map<String, String> _cssStringToMap(String? cssString) {
final result = <String, String>{};
if (cssString == null) {
Expand Down Expand Up @@ -235,7 +243,7 @@ class HTMLToNodesConverter {
} else if (element.localName == HTMLTag.underline) {
delta.insert(element.text,
attributes: {BuiltInAttributeKey.underline: true});
} else if (element.localName == HTMLTag.italic) {
} else if ([HTMLTag.italic, HTMLTag.em].contains(element.localName)) {
delta
.insert(element.text, attributes: {BuiltInAttributeKey.italic: true});
} else if (element.localName == HTMLTag.del) {
Expand Down Expand Up @@ -301,7 +309,9 @@ class HTMLToNodesConverter {
final result = <Node>[];
for (var child in element.children) {
result.addAll(_handleListElement(
child, {"subtype": BuiltInAttributeKey.bulletedList}));
child,
{"subtype": BuiltInAttributeKey.bulletedList},
));
}
return result;
}
Expand Down

0 comments on commit 06240a0

Please sign in to comment.