Skip to content

Commit 7fc530e

Browse files
authored
[Bugfix] Fix the backticks bug (#26)
* [Bugfix] Fix the backticks bug * Add test for inline backtick parse SR-15415
1 parent 2190baa commit 7fc530e

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

api_test/main.c

+20
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,26 @@ static void source_pos_inlines(test_batch_runner *runner) {
11261126
free(xml);
11271127
cmark_node_free(doc);
11281128
}
1129+
{
1130+
static const char markdown[] =
1131+
"` It is one backtick\n"
1132+
"`` They are two backticks\n";
1133+
1134+
cmark_node *doc = cmark_parse_document(markdown, sizeof(markdown) - 1, CMARK_OPT_DEFAULT);
1135+
char *xml = cmark_render_xml(doc, CMARK_OPT_DEFAULT | CMARK_OPT_SOURCEPOS);
1136+
STR_EQ(runner, xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1137+
"<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n"
1138+
"<document sourcepos=\"1:1-2:25\" xmlns=\"http://commonmark.org/xml/1.0\">\n"
1139+
" <paragraph sourcepos=\"1:1-2:25\">\n"
1140+
" <text sourcepos=\"1:1-1:20\" xml:space=\"preserve\">` It is one backtick</text>\n"
1141+
" <softbreak />\n"
1142+
" <text sourcepos=\"2:1-2:25\" xml:space=\"preserve\">`` They are two backticks</text>\n"
1143+
" </paragraph>\n"
1144+
"</document>\n",
1145+
"sourcepos are as expected");
1146+
free(xml);
1147+
cmark_node_free(doc);
1148+
}
11291149
}
11301150

11311151
static void ref_source_pos(test_batch_runner *runner) {

src/inlines.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -373,13 +373,14 @@ static void S_normalize_code(cmark_strbuf *s) {
373373
// Parse backtick code section or raw backticks, return an inline.
374374
// Assumes that the subject has a backtick at the current position.
375375
static cmark_node *handle_backticks(subject *subj, int options) {
376+
bufsize_t initpos = subj->pos;
376377
cmark_chunk openticks = take_while(subj, isbacktick);
377378
bufsize_t startpos = subj->pos;
378379
bufsize_t endpos = scan_to_closing_backticks(subj, openticks.len);
379380

380381
if (endpos == 0) { // not found
381382
subj->pos = startpos; // rewind
382-
return make_str(subj, subj->pos, subj->pos, openticks);
383+
return make_str(subj, initpos, initpos + openticks.len - 1, openticks);
383384
} else {
384385
cmark_strbuf buf = CMARK_BUF_INIT(subj->mem);
385386

0 commit comments

Comments
 (0)