Rewrote automatic URL detection.

This commit is contained in:
flash 2025-05-18 19:37:25 +00:00
parent cf22e63ac5
commit f4d422cb69
Signed by: flash
GPG key ID: 2C9C2C574D47FE3E

View file

@ -239,16 +239,51 @@ Umi.UI.Messages = (function() {
Umi.UI.Emoticons.Parse(eText, msgAuthor);
Umi.Parsing.Parse(eText, msg);
const textSplit = eText.innerText.split(' ');
for(const textPart of textSplit) {
const uri = Umi.URI.Parse(textPart);
const linkify = element => {
let childNode = element.firstChild;
while(childNode instanceof Node)
try {
if(childNode instanceof HTMLElement) {
linkify(childNode);
continue;
}
if(uri !== null && uri.Slashes !== null) {
const anchorElem = <a class="markup__link" href={textPart} target="_blank" rel="nofollow noreferrer noopener">{textPart}</a>;
eText.innerHTML = eText.innerHTML.replace(textPart.replace(/&/g, '&amp;'), anchorElem.outerHTML);
}
if(!(childNode instanceof Text))
continue;
let offset = 0;
const parts = childNode.data.split(/(\p{White_Space}+)/u);
for(const part of parts) {
if(!part.startsWith('//') && !part.includes('://')) {
offset += part.length;
continue;
}
try {
const url = (new URL(part)).toString();
if(offset > 0) {
childNode = childNode.splitText(offset);
offset = 0;
}
const replaceNode = childNode;
childNode = replaceNode.splitText(part.length);
replaceNode.replaceWith(<a class="markup__link" href={url} target="_blank" rel="nofollow noreferrer noopener">
{decodeURI(url)}
</a>);
} catch(ex) {
offset += part.length;
}
}
} finally {
childNode = childNode.nextSibling;
}
}
linkify(eText);
if(mami.settings.get('weeaboo')) {
eUser.appendChild($text(Weeaboo.getNameSuffix(msgAuthor)));
eText.appendChild($text(Weeaboo.getTextSuffix(msgAuthor)));