Skip to content

Commit 4656a72

Browse files
committed
Also do not allow nested links when the inner link is an autolink.
1 parent 9ddb6a1 commit 4656a72

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

markdown.awk

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,13 @@ function inline_autolink(s, i, no_links, replacements, result,
294294
s = substr(s, i)
295295

296296
if (match(s, /^<([a-zA-Z][a-zA-Z0-0+.-]+:[^>[:space:]]+)>/, x)) {
297-
# A URL
297+
# A URL: "...:..."
298298

299299
if (no_links) t = esc_html(x[1])
300300
else t = "<a href=\"" esc_html(esc_url(x[1])) "\">" esc_html(x[1]) "</a>"
301301

302302
} else if (match(s, /^<([a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*)>/, x)) {
303-
# An email address
303+
# An email address: "...@..."
304304

305305
if (no_links) t = esc_html(x[1])
306306
else t = "<a href=\"mailto:" esc_html(x[1]) "\">" esc_html(x[1]) "</a>"
@@ -374,8 +374,7 @@ function inline_link_or_image(s, i, no_links, replacements, result,
374374
# replacements: a stack of HTML fragments. (Passed by reference.)
375375
#
376376
# result: an array in which the function returns the result of
377-
# parsing the link and the remaining text of s after the
378-
# autolink.
377+
# parsing the link and the remaining text of s after the link.
379378
#
380379
# Returns 1 for success, 0 for failure.
381380

@@ -403,9 +402,14 @@ function inline_link_or_image(s, i, no_links, replacements, result,
403402
t = t substr(s, 1, j); s = substr(s, j + 1)
404403
} else if (x[0] ~ /^[`<]/) {
405404
if (inline_code_span(s, j, replacements, result1) ||
406-
inline_autolink(s, j, no_links, replacements, result1) ||
407405
inline_html_tag(s, j, replacements, result1)) {
408406
t = t substr(s, 1, j - 1) result1["html"]; s = result1["rest"]
407+
} else if (inline_autolink(s, j, 1, replacements, result1)) {
408+
if (! is_image) {
409+
return 0 # Nested link, so the outer link isn't one
410+
} else { # Allowed in image alt text (but as text)
411+
t = t substr(s, 1, j - 1) result1["html"]; s = result1["rest"]
412+
}
409413
} else { # delimiter that does not start anything
410414
t = t substr(s, 1, i-1) "\002" push(replacements, esc_html(x[0])) "\003"
411415
s = substr(s, i + length(x[0]))
@@ -420,18 +424,14 @@ function inline_link_or_image(s, i, no_links, replacements, result,
420424
s = substr(s, i + length(x[0]))
421425
}
422426
} else if (x[0] == "[") {
423-
if (! is_image) { # We're inside a link "[...](...)"
424-
if (inline_link_or_image(s, j)) {
425-
return 0
426-
} else { # "[" that does not start a link
427-
n++; t = t substr(s, i, j); s = substr(s, j + 1)
428-
}
429-
} else { # We're inside an image "![...](...)"
430-
if (inline_link_or_image(s, j, 1, replacements, result1)) {
427+
if (inline_link_or_image(s, j, 1, replacements, result1)) {
428+
if (! is_image) { # We're inside a link "[...](...)"
429+
return 0 # Nested link, so the outer link isn't a link
430+
} else { # We're inside an image "![...](...)"
431431
t = t substr(s, 1, j - 1) result1["html"]; s = result1["rest"]
432-
} else { # "[" that does not start a link
433-
n++; t = t substr(s, i, j); s = substr(s, j + 1)
434432
}
433+
} else { # "[" that does not start a link
434+
n++; t = t substr(s, i, j); s = substr(s, j + 1)
435435
}
436436
} else if (n != 0) { # Balanced "]"
437437
n--; t = t substr(s, i, j); s = substr(s, j + 1)

0 commit comments

Comments
 (0)