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
2 changes: 2 additions & 0 deletions pkgs/markdown/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 7.4.1-wip

* Treat horizontal tabs as delimiters between fenced code language and
metadata, and skip punctuation unescaping when no backslashes are present.
* Avoid regular-expression work when classifying blank lines and dedenting Markdown blocks; scan ASCII whitespace directly while retaining partial-tab handling.
* Reduce allocations when rendering single-line HTML text nodes and reset renderer traversal state correctly when a `HtmlRenderer` instance is reused.
* Reduce allocations when rendering single-line text nodes in `HtmlRenderer`.
Expand Down
19 changes: 12 additions & 7 deletions pkgs/markdown/lib/src/block_syntaxes/fenced_code_block_syntax.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import '../ast.dart';
import '../block_parser.dart';
import '../charcode.dart' show $space;
import '../charcode.dart' show $space, $tab;
import '../line.dart';
import '../patterns.dart';
import '../util.dart';
Expand Down Expand Up @@ -176,15 +176,20 @@ class _FenceMatch {
}

// We assume the info string is trimmed already.
final firstSpaceIndex = info.indexOf(' ');
if (firstSpaceIndex == -1) {
// If there is no space, the whole string is the language.
var separatorIndex = -1;
for (var i = 0; i < info.length; i++) {
final char = info.codeUnitAt(i);
if (char == $space || char == $tab) {
separatorIndex = i;
break;
}
}
if (separatorIndex == -1) {
return (info, null);
}

return (
info.substring(0, firstSpaceIndex),
info.substring(firstSpaceIndex + 1),
info.substring(0, separatorIndex),
info.substring(separatorIndex + 1),
);
}

Expand Down
1 change: 1 addition & 0 deletions pkgs/markdown/lib/src/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ extension MatchExtensions on Match {

/// Escapes the ASCII punctuation characters after backslash(`\`).
String escapePunctuation(String input) {
if (!input.contains(r'\')) return input;
final buffer = StringBuffer();

for (var i = 0; i < input.length; i++) {
Expand Down
26 changes: 26 additions & 0 deletions pkgs/markdown/test/fenced_code_info_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:markdown/markdown.dart';
import 'package:test/test.dart';

void main() {
test('fenced language and metadata may be separated by a tab', () {
final html = markdownToHtml('```dart\tline-numbers\ncode\n```\n');
expect(html, contains('class="language-dart"'));
expect(html, contains('data-metadata="line-numbers"'));
});

test('tilde fence info is split at the first tab', () {
final html = markdownToHtml('~~~ dart\t\tone two\ncode\n~~~\n');
expect(html, contains('class="language-dart"'));
expect(html, contains('data-metadata="\tone two"'));
});

test('space-delimited metadata preserves interior spacing', () {
final html = markdownToHtml('```dart one two\ncode\n```\n');
expect(html, contains('class="language-dart"'));
expect(html, contains('data-metadata="one two"'));
});
}
11 changes: 11 additions & 0 deletions pkgs/markdown/test/util_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ void main() {
expect(decodeHtmlCharacters('&#65;'), 'A');
});
});

group('escapePunctuation', () {
test('keeps unescaped content intact', () {
expect(escapePunctuation('fence language'), 'fence language');
});

test('removes punctuation escapes but not ordinary backslashes', () {
expect(escapePunctuation(r'\*escaped\!'), '*escaped!');
expect(escapePunctuation(r'\a'), r'\a');
});
});
}

extension on Line {
Expand Down
Loading