From 71c6a82cdfe0fdc767a1bb008d0f615d2cd23f0a Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Wed, 11 Nov 2026 18:38:18 +0200 Subject: [PATCH] fix(markdown): recognize tab-separated code fence metadata Otzaria/tools#36 --- pkgs/markdown/CHANGELOG.md | 2 ++ .../fenced_code_block_syntax.dart | 19 +++++++++----- pkgs/markdown/lib/src/util.dart | 1 + pkgs/markdown/test/fenced_code_info_test.dart | 26 +++++++++++++++++++ pkgs/markdown/test/util_test.dart | 11 ++++++++ 5 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 pkgs/markdown/test/fenced_code_info_test.dart diff --git a/pkgs/markdown/CHANGELOG.md b/pkgs/markdown/CHANGELOG.md index f0817ca46d..6e949b47ba 100644 --- a/pkgs/markdown/CHANGELOG.md +++ b/pkgs/markdown/CHANGELOG.md @@ -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`. diff --git a/pkgs/markdown/lib/src/block_syntaxes/fenced_code_block_syntax.dart b/pkgs/markdown/lib/src/block_syntaxes/fenced_code_block_syntax.dart index 7954e0e42c..74a8a61f34 100644 --- a/pkgs/markdown/lib/src/block_syntaxes/fenced_code_block_syntax.dart +++ b/pkgs/markdown/lib/src/block_syntaxes/fenced_code_block_syntax.dart @@ -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'; @@ -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), ); } diff --git a/pkgs/markdown/lib/src/util.dart b/pkgs/markdown/lib/src/util.dart index 92bb25dfb4..75d60b03e3 100644 --- a/pkgs/markdown/lib/src/util.dart +++ b/pkgs/markdown/lib/src/util.dart @@ -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++) { diff --git a/pkgs/markdown/test/fenced_code_info_test.dart b/pkgs/markdown/test/fenced_code_info_test.dart new file mode 100644 index 0000000000..2a29ad395b --- /dev/null +++ b/pkgs/markdown/test/fenced_code_info_test.dart @@ -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"')); + }); +} diff --git a/pkgs/markdown/test/util_test.dart b/pkgs/markdown/test/util_test.dart index 6b422df582..2c54e61bf0 100644 --- a/pkgs/markdown/test/util_test.dart +++ b/pkgs/markdown/test/util_test.dart @@ -113,6 +113,17 @@ void main() { expect(decodeHtmlCharacters('A'), '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 {