diff --git a/pkgs/markdown/CHANGELOG.md b/pkgs/markdown/CHANGELOG.md index 6e949b47b..a8ae6b82e 100644 --- a/pkgs/markdown/CHANGELOG.md +++ b/pkgs/markdown/CHANGELOG.md @@ -17,6 +17,7 @@ the delimiter row's column count is reached, so cells that would be discarded are never allocated. * Reuse HTML escape converters and skip HTML entity matching for strings without ampersands, reducing allocations during inline and code-block parsing. +* Sort footnote definitions by their original case-insensitive labels rather than URI-encoded HTML IDs, preserving first-reference order for Unicode and escaped labels. * Skip the regular expression in `EmojiSyntax`, `AutolinkSyntax`, `CodeSyntax`, and `ColorSwatchSyntax` when the character at the current position cannot start a match. This makes inline parsing faster, mostly on diff --git a/pkgs/markdown/lib/src/document.dart b/pkgs/markdown/lib/src/document.dart index 41f7ac15e..6ce702fe8 100644 --- a/pkgs/markdown/lib/src/document.dart +++ b/pkgs/markdown/lib/src/document.dart @@ -171,13 +171,13 @@ class Document { if (footnotes.isNotEmpty) { // Sort footnotes by appearing order. final ordinal = { - for (var i = 0; i < footnoteLabels.length; i++) - 'fn-${footnoteLabels[i]}': i, + for (var i = 0; i < footnoteLabels.length; i++) footnoteLabels[i]: i, }; footnotes.sort((l, r) { - final idl = l.attributes['id']?.toLowerCase() ?? ''; - final idr = r.attributes['id']?.toLowerCase() ?? ''; - return (ordinal[idl] ?? 0) - (ordinal[idr] ?? 0); + // 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); }); final list = Element('ol', [...footnotes]); diff --git a/pkgs/markdown/test/document_test.dart b/pkgs/markdown/test/document_test.dart index 0ad2bc6e4..ab320542c 100644 --- a/pkgs/markdown/test/document_test.dart +++ b/pkgs/markdown/test/document_test.dart @@ -150,5 +150,39 @@ void main() { ); expect(second, contains('href="#fn-b"')); }); + + test('orders Unicode footnotes by first reference, not definition', () { + const firstLabel = 'שני'; + const secondLabel = 'ראשון'; + final html = markdownToHtml( + 'ראה[^$firstLabel] ואחר כך[^$secondLabel].\n\n' + '[^$secondLabel]: הערה ראשונה\n' + '[^$firstLabel]: הערה שנייה', + extensionSet: ExtensionSet.gitHubWeb, + ); + final firstId = Uri.encodeComponent(firstLabel); + final secondId = Uri.encodeComponent(secondLabel); + final firstDefinition = html.indexOf('
  • '); + final secondDefinition = html.indexOf('
  • '); + expect(firstDefinition, greaterThanOrEqualTo(0)); + expect(secondDefinition, greaterThan(firstDefinition)); + }); + + test('orders escaped punctuation footnotes by first reference', () { + const firstLabel = 'one'; + const secondLabel = r'\['; + final html = markdownToHtml( + 'See[^$firstLabel] and[^$secondLabel].\n\n' + '[^$secondLabel]: second definition\n' + '[^$firstLabel]: first definition', + extensionSet: ExtensionSet.gitHubWeb, + ); + final firstId = Uri.encodeComponent(firstLabel); + final secondId = Uri.encodeComponent(secondLabel); + final firstDefinition = html.indexOf('
  • '); + final secondDefinition = html.indexOf('
  • '); + expect(firstDefinition, greaterThanOrEqualTo(0)); + expect(secondDefinition, greaterThan(firstDefinition)); + }); }); }