web: fix anchor detection

This fixes an issue where text was being hyperlinked when the "http"
pattern was detected in a character block. This looks that there is
whitespace or such to correctly assume it's a link. This may not be the
best implementation, but it seems to resolve issues for me so far. Unit
tests would be good.
This commit is contained in:
Thomas Mathews 2022-11-12 20:36:48 -08:00
parent 0fdde7b384
commit b214b6e2a0

View file

@ -1033,24 +1033,28 @@ function is_video_url(path) {
return VID_REGEX.test(path) return VID_REGEX.test(path)
} }
const URL_REGEX = /(https?:\/\/[^\s]+)[,:)]?(\w|$)/g; const URL_REGEX = /(^|\s)(https?:\/\/[^\s]+)[,:)]?(\w|$)/g;
function linkify(text, show_media) { function linkify(text, show_media) {
return text.replace(URL_REGEX, function(url) { return text.replace(URL_REGEX, function(match, p1, p2, p3) {
const url = p2
const parsed = new URL(url) const parsed = new URL(url)
if (show_media && is_img_url(parsed.pathname)) let html;
return ` if (show_media && is_img_url(parsed.pathname)) {
html = `
<a target="_blank" href="${url}"> <a target="_blank" href="${url}">
<img class="inline-img" src="${url}"/> <img class="inline-img" src="${url}"/>
</a> </a>
`; `;
else if (show_media && is_video_url(parsed.pathname)) } else if (show_media && is_video_url(parsed.pathname)) {
return ` html = `
<video controls class="inline-img" /> <video controls class="inline-img" />
<source src="${url}"> <source src="${url}">
</video> </video>
`; `;
else } else {
return `<a target="_blank" rel="noopener noreferrer" href="${url}">${url}</a>`; html = `<a target="_blank" rel="noopener noreferrer" href="${url}">${url}</a>`;
}
return p1+html;
}) })
} }