Rewrote automatic URL detection.
This commit is contained in:
parent
cf22e63ac5
commit
f4d422cb69
1 changed files with 42 additions and 7 deletions
|
@ -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, '&'), 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)));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue