Skip to content

Commit

Permalink
[Bugfix] Fix the backticks bug (#26)
Browse files Browse the repository at this point in the history
* [Bugfix] Fix the backticks bug

* Add test for inline backtick parse

SR-15415
  • Loading branch information
Kyle-Ye authored Nov 10, 2021
1 parent 2190baa commit 7fc530e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
20 changes: 20 additions & 0 deletions api_test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,26 @@ static void source_pos_inlines(test_batch_runner *runner) {
free(xml);
cmark_node_free(doc);
}
{
static const char markdown[] =
"` It is one backtick\n"
"`` They are two backticks\n";

cmark_node *doc = cmark_parse_document(markdown, sizeof(markdown) - 1, CMARK_OPT_DEFAULT);
char *xml = cmark_render_xml(doc, CMARK_OPT_DEFAULT | CMARK_OPT_SOURCEPOS);
STR_EQ(runner, xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n"
"<document sourcepos=\"1:1-2:25\" xmlns=\"http://commonmark.org/xml/1.0\">\n"
" <paragraph sourcepos=\"1:1-2:25\">\n"
" <text sourcepos=\"1:1-1:20\" xml:space=\"preserve\">` It is one backtick</text>\n"
" <softbreak />\n"
" <text sourcepos=\"2:1-2:25\" xml:space=\"preserve\">`` They are two backticks</text>\n"
" </paragraph>\n"
"</document>\n",
"sourcepos are as expected");
free(xml);
cmark_node_free(doc);
}
}

static void ref_source_pos(test_batch_runner *runner) {
Expand Down
3 changes: 2 additions & 1 deletion src/inlines.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,14 @@ static void S_normalize_code(cmark_strbuf *s) {
// Parse backtick code section or raw backticks, return an inline.
// Assumes that the subject has a backtick at the current position.
static cmark_node *handle_backticks(subject *subj, int options) {
bufsize_t initpos = subj->pos;
cmark_chunk openticks = take_while(subj, isbacktick);
bufsize_t startpos = subj->pos;
bufsize_t endpos = scan_to_closing_backticks(subj, openticks.len);

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

Expand Down

0 comments on commit 7fc530e

Please sign in to comment.