Skip to content
Open
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
10 changes: 10 additions & 0 deletions lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,18 @@ class MarkdownDecoder extends Converter<String, Markdown> {
static final RegExp _listPattern = RegExp(
r'^(?<indent>[ \t]{0,8})(?<marker>(\d{1,9})[\.)]|[*+-])(?<text>[ \t]+(.*))?$');

/// Replaces common LaTeX inline math with Unicode equivalents.
static String _replaceInlineMath(String text) => text
.replaceAll(r'$\rightarrow$', '\u{2192}')
.replaceAll(r'$\leftarrow$', '\u{2190}')
.replaceAll(r'$\Rightarrow$', '\u{21D2}')
.replaceAll(r'$\Leftarrow$', '\u{21D0}')
.replaceAll(r'$\Leftrightarrow$', '\u{21D4}');

@override
Markdown convert(String input) {
input = _replaceInlineMath(input);

final lines = LineSplitter.split(input).toList(growable: false);
if (lines.isEmpty) return const Markdown.empty();
final blocks = Queue<MD$Block>(); // Queue to accumulate blocks
Expand Down
11 changes: 11 additions & 0 deletions test/parser/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,17 @@ void main() => group('Parse', () {
}
});

test('Parse LaTeX inline math replaces rightarrow with arrow', () {
final markdown = markdownDecoder.convert(r'A $\rightarrow$ B');
expect(markdown.blocks, hasLength(1));
final paragraph = markdown.blocks.first as MD$Paragraph;
expect(paragraph.text, contains('→'));
expect(
paragraph.spans.map((s) => s.text).join(),
contains('→'),
);
});

group('Parse escaped characters', () {
const escapedCharacterTests = {
r'\\': r'\', // Backslash
Expand Down