md_resolve_links: Suppress bogus nested permissive autolink. Fixes #152.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
diff --git a/src/md4c.c b/src/md4c.c
index 2858592..7ed80b6 100644
--- a/src/md4c.c
+++ b/src/md4c.c
@@ -3611,6 +3611,33 @@ md_resolve_links(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
}
md_analyze_link_contents(ctx, lines, n_lines, opener_index+1, closer_index);
+
+ /* If the link text is formed by nothing but permissive autolink,
+ * suppress the autolink.
+ * See https://github.com/mity/md4c/issues/152 for more info. */
+ if(ctx->parser.flags & MD_FLAG_PERMISSIVEAUTOLINKS) {
+ MD_MARK* first_nested;
+ MD_MARK* last_nested;
+
+ first_nested = opener + 1;
+ while(first_nested->ch == _T('D') && first_nested < closer)
+ first_nested++;
+
+ last_nested = closer - 1;
+ while(first_nested->ch == _T('D') && last_nested > opener)
+ last_nested--;
+
+ if((first_nested->flags & MD_MARK_RESOLVED) &&
+ first_nested->beg == opener->end &&
+ ISANYOF_(first_nested->ch, _T("@:.")) &&
+ first_nested->next == (last_nested - ctx->marks))
+ {
+ first_nested->ch = _T('D');
+ first_nested->flags &= ~MD_MARK_RESOLVED;
+ last_nested->ch = _T('D');
+ last_nested->flags &= ~MD_MARK_RESOLVED;
+ }
+ }
}
opener_index = next_index;
diff --git a/test/permissive-url-autolinks.txt b/test/permissive-url-autolinks.txt
index f75830d..dfd6b5d 100644
--- a/test/permissive-url-autolinks.txt
+++ b/test/permissive-url-autolinks.txt
@@ -90,3 +90,10 @@ This is [link](http://github.com/)X
````````````````````````````````
+## [Issue 152](https://github.com/mity/md4c/issues/152)
+
+```````````````````````````````` example
+[http://example.com](http://example.com)
+.
+<p><a href="http://example.com">http://example.com</a></p>
+````````````````````````````````