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: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"type": "shell",
"command": [
"dart format --fix -l 80 lib test"
"dart format -l 80 lib test"
],
"dependsOn": [],
"args": [],
Expand Down
33 changes: 24 additions & 9 deletions lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -453,17 +453,19 @@ List<MD$Span> _parseInlineSpans(String text) {

for (var i = 0; i < length; i++) {
final ch = codes[i];
// Check for escaped characters
if (ch == esc /* \ */ && i != length - 1) {
final nextChar = codes[i + 1];
// Check if the next character is an escaped character
if (_escapedChars.length > nextChar && _escapedChars[nextChar] == 1) {
hasExcluded = true; // We have an escaped character
excluded.add(i); // Exclude this character as it is escaped
i++; // skip next char

continue;
// If we are inside a monospace block, we should only look
// for the closing backtick.
if (mask.contains(MD$Style.monospace)) {
if (ch == 96 /* ` */ && i > 0 && codes[i - 1] != esc /* ignore \` */) {
// Found closing backtick
maybePushSpan(i);
mask ^= MD$Style.monospace;
start = i + 1;
}
// We continue to the next character, ignoring any other
// special markers.
continue;
}

// If this character is part of a link or image, skip it
Expand All @@ -478,6 +480,19 @@ List<MD$Span> _parseInlineSpans(String text) {
continue;
}

// Check for escaped characters
if (ch == esc /* \ */ && i != length - 1) {
final nextChar = codes[i + 1];
// Check if the next character is an escaped character
if (_escapedChars.length > nextChar && _escapedChars[nextChar] == 1) {
hasExcluded = true; // We have an escaped character
excluded.add(i); // Exclude this character as it is escaped
i++; // skip next char

continue;
}
}

// If the character is not a special inline marker, continue
if (_kind.length > ch && _kind[ch] == 0) continue;

Expand Down
41 changes: 41 additions & 0 deletions test/parser/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,47 @@ void main() => group('Parse', () {
);
});

test('Inline code should treat inline code as literal', () {
// A map of markdown syntax to its expected literal representation
// inside a code block.
const List<String> syntaxes = [
'*italic*',
'_italic_',
'**bold**',
'__underline__',
'~~strike~~',
'==highlight==',
'||spoiler||',
r'\* \_ \`',
'!alt',
'* Item 1',
'# Header',
'---',
'1. Item **1**',
'[Markdown Live Preview](https://markdownlivepreview.com/)',
'package:flutter_md/flutter_md.dart'
];

final text = syntaxes.map((syntax) => '`$syntax`').join();
final markdown = markdownDecoder.convert(text);

expect(markdown.blocks,
allOf(isNotEmpty, hasLength(1), everyElement(isA<MD$Paragraph>())));

final paragraph = markdown.blocks.first as MD$Paragraph;
final spans = paragraph.spans;

for (var i = 0; i < syntaxes.length; i++) {
final expectedText = syntaxes.elementAt(i);

final codeSpan = spans[i];
expect(codeSpan.text, expectedText);
expect(codeSpan.style, MD$Style.monospace,
reason:
'Span for "$expectedText" should only have monospace style');
}
});

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