Commit 67401e7019024b99368fead4f0748ad257282f6a

Martin Mitas 2019-02-06T04:31:25

md_analyze_inlines: Resolve table cell boundaries before links. This brings some corner cases closer to cmark-gfm. Also fixes #51.

diff --git a/md4c/md4c.c b/md4c/md4c.c
index f584f55..8c4549f 100644
--- a/md4c/md4c.c
+++ b/md4c/md4c.c
@@ -3793,15 +3793,9 @@ md_analyze_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mod
     BACKTICK_OPENERS.tail = -1;
     LOWERTHEN_OPENERS.head = -1;
     LOWERTHEN_OPENERS.tail = -1;
-    /* (2) Links. */
-    md_analyze_marks(ctx, lines, n_lines, 0, ctx->n_marks, _T("[]!"));
-    MD_CHECK(md_resolve_links(ctx, lines, n_lines));
-    BRACKET_OPENERS.head = -1;
-    BRACKET_OPENERS.tail = -1;
-    ctx->unresolved_link_head = -1;
-    ctx->unresolved_link_tail = -1;
+
     if(table_mode) {
-        /* (3a) Analyze table cell boundaries.
+        /* (2) Analyze table cell boundaries.
          * Note we reset TABLECELLBOUNDARIES chain prior to the call md_analyze_marks(),
          * not after, because caller may need it. */
         MD_ASSERT(n_lines == 1);
@@ -3809,8 +3803,18 @@ md_analyze_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mod
         TABLECELLBOUNDARIES.tail = -1;
         ctx->n_table_cell_boundaries = 0;
         md_analyze_marks(ctx, lines, n_lines, 0, ctx->n_marks, _T("|"));
-    } else {
-        /* (3b) Emphasis and strong emphasis; permissive autolinks. */
+    }
+
+    /* (3) Links. */
+    md_analyze_marks(ctx, lines, n_lines, 0, ctx->n_marks, _T("[]!"));
+    MD_CHECK(md_resolve_links(ctx, lines, n_lines));
+    BRACKET_OPENERS.head = -1;
+    BRACKET_OPENERS.tail = -1;
+    ctx->unresolved_link_head = -1;
+    ctx->unresolved_link_tail = -1;
+
+    if(!table_mode) {
+        /* (4) Emphasis and strong emphasis; permissive autolinks. */
         md_analyze_link_contents(ctx, lines, n_lines, 0, ctx->n_marks);
     }
 
diff --git a/test/tables.txt b/test/tables.txt
index c06a4ce..ffda75d 100644
--- a/test/tables.txt
+++ b/test/tables.txt
@@ -248,11 +248,11 @@ quux     | [quuz](/url2)
 </table>
 ````````````````````````````````
 
-However pipes which are inside a link, an image or a code span are not
-recognized as cell boundaries.
+However pipes which are inside a code span are not recognized as cell
+boundaries.
 
 ```````````````````````````````` example
-Column 1 | [|](/url)
+Column 1 | Column 2
 ---------|---------
 `foo     | bar`
 baz      | qux
@@ -260,7 +260,7 @@ quux     | quuz
 .
 <table>
 <thead>
-<tr><th>Column 1</th><th><a href="/url">|</a></th></tr>
+<tr><th>Column 1</th><th>Column 2</th></tr>
 </thead>
 <tbody>
 </tbody>