fix the html link parsing

This commit is contained in:
Ray Andrew 2025-12-04 14:57:05 -06:00
parent e6dfd840c9
commit b710162ba8
Signed by: rayandrew
SSH key fingerprint: SHA256:XYrYrxF0Z3A72n8P/p6mqPRNQZT22F88XcLsG+kX4xw
2 changed files with 53 additions and 1 deletions

View file

@ -193,8 +193,58 @@ else
quotebuf = quotebuf $0 quotebuf = quotebuf $0
next next
} }
function linkify(line, result, pos, url, email, before, after, linktext) {
result = line
# First: Handle email<mailto:email> pattern - keep only one email as link
while (match(result, /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}&lt;mailto:[^&]*&gt;/)) {
before = substr(result, 1, RSTART-1)
# Extract just the email part (before &lt;mailto:)
pos = index(substr(result, RSTART), "&lt;mailto:")
email = substr(result, RSTART, pos-1)
after = substr(result, RSTART+RLENGTH)
result = before "<a href=\"mailto:" email "\">" email "</a>" after
}
# Second: Handle standalone &lt;mailto:email&gt;
while (match(result, /&lt;mailto:[^&]+&gt;/)) {
before = substr(result, 1, RSTART-1)
email = substr(result, RSTART+11, RLENGTH-15)
after = substr(result, RSTART+RLENGTH)
result = before "<a href=\"mailto:" email "\">" email "</a>" after
}
# Third: Handle text&lt;https://url&gt; - text becomes link text
while (match(result, /[A-Za-z0-9_-]+&lt;https?:\/\/[^&]+&gt;/)) {
before = substr(result, 1, RSTART-1)
full = substr(result, RSTART, RLENGTH)
# Find where &lt; starts
pos = index(full, "&lt;")
linktext = substr(full, 1, pos-1)
# Extract URL: after &lt; (4 chars) until &gt; (4 chars at end)
url = substr(full, pos+4, length(full)-pos-7)
after = substr(result, RSTART+RLENGTH)
result = before "<a href=\"" url "\" target=\"_blank\">" linktext "</a>" after
}
# Fourth: Handle standalone &lt;https://url&gt;
while (match(result, /&lt;https?:\/\/[^&]+&gt;/)) {
before = substr(result, 1, RSTART-1)
url = substr(result, RSTART+4, RLENGTH-8)
after = substr(result, RSTART+RLENGTH)
result = before "<a href=\"" url "\" target=\"_blank\">" url "</a>" after
}
# Fifth: Handle plain URLs (not already in href)
while (match(result, /https?:\/\/[^ &<>"\n\t]+/)) {
before = substr(result, 1, RSTART-1)
if (before ~ /href="$/) break
url = substr(result, RSTART, RLENGTH)
after = substr(result, RSTART+RLENGTH)
# Clean trailing punctuation
sub(/[.,;:!?)]+$/, "", url)
result = before "<a href=\"" url "\" target=\"_blank\">" url "</a>" after
}
return result
}
{ {
flush_quote() flush_quote()
# HTML escape first
gsub(/</, "\\&lt;") gsub(/</, "\\&lt;")
gsub(/>/, "\\&gt;") gsub(/>/, "\\&gt;")
if (!incode) { if (!incode) {
@ -205,6 +255,8 @@ else
after = substr($0, RSTART+RLENGTH) after = substr($0, RSTART+RLENGTH)
$0 = before "<code>" code "</code>" after $0 = before "<code>" code "</code>" after
} }
# Linkify URLs and emails
$0 = linkify($0)
} }
print print
} }

View file

@ -11,7 +11,7 @@ bind editor ^T complete
# Pager # Pager
bind index,pager V edit-raw-message bind index,pager V edit-raw-message
macro index,pager H "<pipe-message>~/dotfiles/bin/view-email-html<enter>" "View email in browser" macro index,pager H "<pipe-message>~/dotfiles/bin/view-email-html<enter><clear-flag>N<sync-mailbox>" "View email in browser"
bind pager c imap-fetch-mail bind pager c imap-fetch-mail
bind pager j next-line bind pager j next-line
bind pager k previous-line bind pager k previous-line