Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/markdown/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions pkgs/markdown/lib/src/document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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', <Node>[...footnotes]);

Expand Down
34 changes: 34 additions & 0 deletions pkgs/markdown/test/document_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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('<li id="fn-$firstId">');
final secondDefinition = html.indexOf('<li id="fn-$secondId">');
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('<li id="fn-$firstId">');
final secondDefinition = html.indexOf('<li id="fn-$secondId">');
expect(firstDefinition, greaterThanOrEqualTo(0));
expect(secondDefinition, greaterThan(firstDefinition));
});
});
}
Loading