Further improvements to autolinking.

This commit is contained in:
flash 2025-05-19 00:39:32 +00:00
parent 14f9eab077
commit b19490e367
Signed by: flash
GPG key ID: 2C9C2C574D47FE3E

View file

@ -251,30 +251,39 @@ Umi.UI.Messages = (function() {
continue;
let offset = 0;
const parts = childNode.data.split(/(\p{White_Space}+)/u);
const parts = childNode.data.split(/([\p{White_Space}(){}\[\]<>'"]+)/u);
for(const part of parts) {
if(!part.startsWith('//') && !part.includes('://')) {
offset += part.length;
continue;
}
let url;
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>);
url = new URL(part);
} catch(ex) {
offset += part.length;
offset += addToOffset;
continue;
}
if(offset > 0) {
childNode = childNode.splitText(offset);
offset = 0;
}
let text = part;
while(!text.endsWith('/') && /\p{P}$/u.test(text))
text = text.substring(0, text.length - 1);
if(text.length < part.length)
url = new URL(text);
const replaceNode = childNode;
childNode = replaceNode.splitText(text.length);
replaceNode.replaceWith(<a class="markup__link" href={url.toString()} target="_blank" rel="nofollow noreferrer noopener">
{decodeURI(text)}
</a>);
}
} finally {
childNode = childNode.nextSibling;