cmark

My personal build of CMark ✏️

Commit
b1556b3407a4349b9a1f4f58d8677eda0ead09c1
Parent
3a440af3ba874289ef33ac6bbf2da90040c123d6
Author
John MacFarlane <jgm@berkeley.edu>
Date

Merge pull request #275 from github/inline-sourcepos-off

correct sourcepos for emphasis inlines

Diffstat

2 files changed, 52 insertions, 3 deletions

Status File Name N° Changes Insertions Deletions
Modified api_test/main.c 48 48 0
Modified src/inlines.c 7 4 3
diff --git a/api_test/main.c b/api_test/main.c
@@ -952,6 +952,53 @@ static void source_pos(test_batch_runner *runner) {
   cmark_node_free(doc);
 }
 
+static void source_pos_inlines(test_batch_runner *runner) {
+  {
+    static const char markdown[] =
+      "*first*\n"
+      "second\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:6\" xmlns=\"http://commonmark.org/xml/1.0\">\n"
+                        "  <paragraph sourcepos=\"1:1-2:6\">\n"
+                        "    <emph sourcepos=\"1:1-1:7\">\n"
+                        "      <text sourcepos=\"1:2-1:6\" xml:space=\"preserve\">first</text>\n"
+                        "    </emph>\n"
+                        "    <softbreak />\n"
+                        "    <text sourcepos=\"2:1-2:6\" xml:space=\"preserve\">second</text>\n"
+                        "  </paragraph>\n"
+                        "</document>\n",
+                        "sourcepos are as expected");
+    free(xml);
+    cmark_node_free(doc);
+  }
+  {
+    static const char markdown[] =
+      "*first\n"
+      "second*\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:7\" xmlns=\"http://commonmark.org/xml/1.0\">\n"
+                        "  <paragraph sourcepos=\"1:1-2:7\">\n"
+                        "    <emph sourcepos=\"1:1-2:7\">\n"
+                        "      <text sourcepos=\"1:2-1:6\" xml:space=\"preserve\">first</text>\n"
+                        "      <softbreak />\n"
+                        "      <text sourcepos=\"2:1-2:6\" xml:space=\"preserve\">second</text>\n"
+                        "    </emph>\n"
+                        "  </paragraph>\n"
+                        "</document>\n",
+                        "sourcepos are as expected");
+    free(xml);
+    cmark_node_free(doc);
+  }
+}
+
 static void ref_source_pos(test_batch_runner *runner) {
   static const char markdown[] =
     "Let's try [reference] links.\n"
@@ -1002,6 +1049,7 @@ int main() {
   test_safe(runner);
   test_feed_across_line_ending(runner);
   source_pos(runner);
+  source_pos_inlines(runner);
   ref_source_pos(runner);
 
   test_print_summary(runner);
diff --git a/src/inlines.c b/src/inlines.c
@@ -732,9 +732,10 @@ static delimiter *S_insert_emph(subject *subj, delimiter *opener,
   }
   cmark_node_insert_after(opener_inl, emph);
 
-  emph->start_line = emph->end_line = subj->line;
-  emph->start_column = opener_inl->start_column + subj->column_offset;
-  emph->end_column = closer_inl->end_column + subj->column_offset;
+  emph->start_line = opener_inl->start_line;
+  emph->end_line = closer_inl->end_line;
+  emph->start_column = opener_inl->start_column;
+  emph->end_column = closer_inl->end_column;
 
   // if opener has 0 characters, remove it and its associated inline
   if (opener_num_chars == 0) {