Commit 70b247cf7da02e927a9ba05b52d10fd0109f8d99

Martin Mitas 2024-01-19T13:59:45

md_analyze_permissive_autolink: Accept path ending with '/'. Fixes #226.

diff --git a/src/md4c.c b/src/md4c.c
index 104e87d..47b392c 100644
--- a/src/md4c.c
+++ b/src/md4c.c
@@ -3888,11 +3888,12 @@ md_analyze_permissive_autolink(MD_CTX* ctx, int mark_index)
         const MD_CHAR delim_char;
         const MD_CHAR* allowed_nonalnum_chars;
         int min_components;
+        const MD_CHAR optional_end_char;
     } URL_MAP[] = {
-        { _T('\0'), _T('.'),  _T(".-_"),      2 },    /* host, mandatory */
-        { _T('/'),  _T('/'),  _T("/.-_"),     0 },    /* path */
-        { _T('?'),  _T('&'),  _T("&.-+_=()"), 1 },    /* query */
-        { _T('#'),  _T('\0'), _T(".-+_") ,    1 }     /* fragment */
+        { _T('\0'), _T('.'),  _T(".-_"),      2, _T('\0') },    /* host, mandatory */
+        { _T('/'),  _T('/'),  _T("/.-_"),     0, _T('/') },     /* path */
+        { _T('?'),  _T('&'),  _T("&.-+_=()"), 1, _T('\0') },    /* query */
+        { _T('#'),  _T('\0'), _T(".-+_") ,    1, _T('\0') }     /* fragment */
     };
 
     MD_MARK* opener = &ctx->marks[mark_index];
@@ -3933,7 +3934,9 @@ md_analyze_permissive_autolink(MD_CTX* ctx, int mark_index)
         int n_open_brackets = 0;
 
         if(URL_MAP[i].start_char != _T('\0')) {
-            if(end + 1 >= line_end  ||  CH(end) != URL_MAP[i].start_char  ||  !ISALNUM(end+1))
+            if(end >= line_end  ||  CH(end) != URL_MAP[i].start_char)
+                continue;
+            if(URL_MAP[i].min_components > 0  &&  (end+1 >= line_end  ||  !ISALNUM(end+1)))
                 continue;
             end++;
         }
@@ -3967,6 +3970,10 @@ md_analyze_permissive_autolink(MD_CTX* ctx, int mark_index)
             }
         }
 
+        if(end < line_end  &&  URL_MAP[i].optional_end_char != _T('\0')  &&
+                CH(end) == URL_MAP[i].optional_end_char)
+            end++;
+
         if(n_components < URL_MAP[i].min_components  ||  n_open_brackets != 0)
             return;
 
diff --git a/test/regressions.txt b/test/regressions.txt
index d259476..b251443 100644
--- a/test/regressions.txt
+++ b/test/regressions.txt
@@ -713,3 +713,15 @@ foo
 .
 <p><code>foo</code></p>
 ````````````````````````````````
+
+## [Issue 226](https://github.com/mity/md4c/issues/226)
+
+```````````````````````````````` example
+https://example.com/
+https://example.com/dir/
+.
+<p><a href="https://example.com/">https://example.com/</a>
+<a href="https://example.com/dir/">https://example.com/dir/</a></p>
+.
+--fpermissive-url-autolinks
+````````````````````````````````