diff --git a/pkgs/markdown/lib/src/document.dart b/pkgs/markdown/lib/src/document.dart index 6ce702fe8..0df061796 100644 --- a/pkgs/markdown/lib/src/document.dart +++ b/pkgs/markdown/lib/src/document.dart @@ -170,14 +170,12 @@ class Document { if (footnotes.isNotEmpty) { // Sort footnotes by appearing order. - final ordinal = { - for (var i = 0; i < footnoteLabels.length; i++) footnoteLabels[i]: i, - }; footnotes.sort((l, r) { // HTML IDs are URI-encoded, but the reference order uses raw labels. final labelL = l.footnoteLabel?.toLowerCase(); final labelR = r.footnoteLabel?.toLowerCase(); - return (ordinal[labelL] ?? 0) - (ordinal[labelR] ?? 0); + return (_footnoteLabelIndex[labelL] ?? 0) - + (_footnoteLabelIndex[labelR] ?? 0); }); final list = Element('ol', [...footnotes]); diff --git a/pkgs/markdown/lib/src/inline_parser.dart b/pkgs/markdown/lib/src/inline_parser.dart index 49609e80d..35fb2baa7 100644 --- a/pkgs/markdown/lib/src/inline_parser.dart +++ b/pkgs/markdown/lib/src/inline_parser.dart @@ -118,10 +118,14 @@ class InlineParser { } } + static final Set _builtInSyntaxes = { + ...ExtensionSet.gitHubWeb.inlineSyntaxes, + ...ExtensionSet.gitHubFlavored.inlineSyntaxes, + ...ExtensionSet.commonMark.inlineSyntaxes, + }; + static bool _isBuiltInExtension(InlineSyntax syntax) => - ExtensionSet.gitHubWeb.inlineSyntaxes.contains(syntax) || - ExtensionSet.gitHubFlavored.inlineSyntaxes.contains(syntax) || - ExtensionSet.commonMark.inlineSyntaxes.contains(syntax); + _builtInSyntaxes.contains(syntax); List parse() { while (!isDone) { @@ -484,7 +488,19 @@ class _PlainTextSyntax extends TextSyntax { final bool _allowSpaces; final bool _includeHebrew; - _PlainTextSyntax({required bool allowSpaces, required bool includeHebrew}) + // The syntax is stateless, so one instance per configuration is shared to + // avoid compiling its pattern for every parser. + static final _instances = <(bool, bool), _PlainTextSyntax>{}; + + factory _PlainTextSyntax({ + required bool allowSpaces, + required bool includeHebrew, + }) => _instances.putIfAbsent(( + allowSpaces, + includeHebrew, + ), () => _PlainTextSyntax._(allowSpaces, includeHebrew)); + + _PlainTextSyntax._(bool allowSpaces, bool includeHebrew) : _allowSpaces = allowSpaces, _includeHebrew = includeHebrew, super(_plainTextPattern(allowSpaces, includeHebrew));