Skip to content

Commit 35c6034

Browse files
committed
Markdown: Detect ending ``` on the same line
This isn't quite correct check as there may be multiple code spans on a single line and they can go over multiple lines but at least it fixes the case the previous regex parser handled.
1 parent 2460571 commit 35c6034

File tree

6 files changed

+20
-10
lines changed

6 files changed

+20
-10
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--sort=no
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A input.md /^# A$/;" c
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
`O`
2+
# A
3+

Units/parser-markdown.r/simple-markdown.d/expected.tags

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ F input.md /^F$/;" s chapter:C end:106 sectionMarker:-
2828
G input.md /^ G$/;" s chapter:C end:109 sectionMarker:-
2929
H input.md /^ H$/;" s chapter:C end:112 sectionMarker:-
3030
I input.md /^ I$/;" s chapter:C end:128 sectionMarker:-
31-
C\\# input.md /^# C\\#$/;" c end:141 sectionMarker:#
31+
C\\# input.md /^# C\\#$/;" c end:143 sectionMarker:#
3232
J input.md /^J$/;" s chapter:C\\# end:133 sectionMarker:-
3333
K input.md /^K$/;" s chapter:C\\# end:136 sectionMarker:-
34-
L input.md /^L$/;" s chapter:C\\# end:141 sectionMarker:-
34+
L input.md /^L$/;" s chapter:C\\# end:143 sectionMarker:-
3535
x input.md /^function x$/;" f
3636
y input.md /^function y$/;" f
3737
z input.md /^z()$/;" f

Units/parser-markdown.r/simple-markdown.d/input.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,5 @@ L
139139

140140
ignored
141141
-
142+
143+
```foo```

parsers/markdown.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,22 +213,25 @@ static void findMarkdownTags(void)
213213
if (inPreambule)
214214
continue;
215215

216-
for (int i = 0; i < 2 && !indented; i++)
216+
/* fenced code block */
217+
if (line[pos] == '`' || line[pos] == '~')
217218
{
218-
char code_chars[] = { '`', '~' };
219-
char c = code_chars[i % 2];
220-
char other_c = code_chars[(i + 1) % 2];
219+
char c = line[pos];
220+
char other_c = c == '`' ? '~' : '`';
221+
int n_same;
222+
for (n_same = 1; line[n_same] == line[pos]; ++n_same);
221223

222-
if (in_code_char != other_c && line_len >= 3 &&
223-
line[pos] == c && line[pos+1] == c && line[pos+2] == c)
224+
if (in_code_char != other_c && n_same >= 3)
224225
{
225226
in_code_char = in_code_char ? 0 : c;
226-
if (in_code_char)
227+
if (in_code_char == c && strstr((const char *)(line + pos + n_same), "```") != NULL)
228+
in_code_char = 0;
229+
else if (in_code_char)
227230
{
228231
startSourceLineNumber = getSourceLineNumber ();
229232
startLineNumber = getInputLineNumber ();
230233
vStringClear(codeLang);
231-
vStringCatS(codeLang, (const char *)(line + pos + 3));
234+
vStringCatS(codeLang, (const char *)(line + pos + n_same));
232235
vStringStripLeading(codeLang);
233236
vStringStripTrailing(codeLang);
234237
}

0 commit comments

Comments
 (0)