From 7fc530e5f12432db9768326ff74dc46de72a24e2 Mon Sep 17 00:00:00 2001 From: Kyle <43724855+Kyle-Ye@users.noreply.github.com> Date: Thu, 11 Nov 2021 06:45:19 +0800 Subject: [PATCH] [Bugfix] Fix the backticks bug (#26) * [Bugfix] Fix the backticks bug * Add test for inline backtick parse SR-15415 --- api_test/main.c | 20 ++++++++++++++++++++ src/inlines.c | 3 ++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/api_test/main.c b/api_test/main.c index 9897c2d85..1051c2904 100644 --- a/api_test/main.c +++ b/api_test/main.c @@ -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, "\n" + "\n" + "\n" + " \n" + " ` It is one backtick\n" + " \n" + " `` They are two backticks\n" + " \n" + "\n", + "sourcepos are as expected"); + free(xml); + cmark_node_free(doc); + } } static void ref_source_pos(test_batch_runner *runner) { diff --git a/src/inlines.c b/src/inlines.c index eafd5d71b..ddfb7814c 100644 --- a/src/inlines.c +++ b/src/inlines.c @@ -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);