markdown icon indicating copy to clipboard operation
markdown copied to clipboard

<br/> to &lt;br/> is bug?

Open KagurazakaHanabi opened this issue 5 years ago • 1 comments

String data = "Hello ABC, <br/> I would like to thank you...."
print(markdownToHtml(data));

The output is <p>Hello ABC, &lt;br/> I would like to thank you....</p> if use <br /> instead of <br/>, result is <p>Hello ABC, <br /> I would like to thank you....</p>

Originally posted by @KagurazakaHanabi in https://github.com/flutter/flutter_markdown/issues/141#issuecomment-549797734

KagurazakaHanabi avatar Nov 21 '19 10:11 KagurazakaHanabi

I believe this is a bug. The CommonMark spec for open tags and empty elements allows this.

The cause of this problem is the InlineHtmlSyntax that requires a space before the closing bracket.

class InlineHtmlSyntax extends TextSyntax {
  InlineHtmlSyntax()
      : super(r'<[/!?]?[A-Za-z][A-Za-z0-9-]*(?:\s[^>]*)?>',
            startCharacter: $lt);
}

You could possibly use this to also allow the self-closing element

<[/!?]?[A-Za-z][A-Za-z0-9-]*(?:\s[^>]*|[/>]*)?>

Note that you can add your own inline syntaxes to get around this problem:

import 'package:charcode/charcode.dart';
import 'package:markdown/markdown.dart' as md;

class ImprovedInlineHtmlSyntax extends md.TextSyntax {
  ImprovedInlineHtmlSyntax()
      : super(r'<[/!?]?[A-Za-z][A-Za-z0-9-]*(?:\s[^>]*|[/>]*)?>', startCharacter: $lt);
}

md.markdownToHtml(notes,
                extensionSet: md.ExtensionSet.gitHubWeb, inlineSyntaxes: [ImprovedInlineHtmlSyntax()]);

SoftWyer avatar Apr 25 '20 09:04 SoftWyer