Amend DeltaX.fromHtml to retain the attributes

pull/1609/head
agu 1 year ago
parent 821e02d8c9
commit 2f8f529e72
  1. 40
      lib/src/models/documents/document.dart
  2. 2
      lib/src/packages/quill_markdown/markdown_to_delta.dart

@ -445,9 +445,22 @@ class DeltaX {
///
/// for more [info](https://github.com/singerdmx/flutter-quill/issues/1100)
static Delta fromHtml(String html) {
final markdown = html2md.convert(html).replaceAll('unsafe:', '');
final mdDocument = md.Document(encodeHtml: false);
var rules = [
html2md.Rule('image', filters: ['img'], replacement: (content, node) {
node.asElement()?.attributes.remove('class');
//Later we can convert this to delta along with the attributes by GoodInlineHtmlSyntax
return node.outerHTML;
}),
];
final markdown = html2md.convert(html, rules: rules).replaceAll('unsafe:', '');
final mdDocument = md.Document(
encodeHtml: false,
inlineSyntaxes: [
GoodInlineHtmlSyntax(),
],
);
final mdToDelta = MarkdownToDelta(markdownDocument: mdDocument);
@ -466,3 +479,24 @@ enum ChangeSource {
/// Silent change.
silent;
}
/// Convert the html to Element, not Text
class GoodInlineHtmlSyntax extends md.InlineHtmlSyntax {
@override
onMatch(parser, match) {
if (super.onMatch(parser, match)) {
return true;
}
var root = html2md.Node.root(match.group(0)!);
root = root.childNodes().last.firstChild!;
var node = md.Element.empty(root.nodeName);
var attrs = root.asElement()?.attributes.map((key, value) => MapEntry(key.toString(), value));
if (attrs != null) node.attributes.addAll(attrs);
parser.addNode(node);
parser.start = parser.pos;
return false;
}
}

@ -208,7 +208,7 @@ class MarkdownToDelta extends Converter<String, Delta>
final tag = element.tag;
if (_isEmbedElement(element)) {
_delta.insert(_toEmbeddable(element).toJson());
_delta.insert(_toEmbeddable(element).toJson(), element.attributes);
}
if (tag == 'br') {

Loading…
Cancel
Save