1
0
Fork 0

Linkify twitter usernames

This is due to some folks cross-posting from Twitter.
This commit is contained in:
Lim Chee Aun 2022-12-23 00:40:25 +08:00
parent 7e84088d67
commit 1c98433333

View file

@ -57,6 +57,23 @@ function enhanceContent(content, opts = {}) {
block.replaceWith(pre);
});
// TWITTER USERNAMES
// =================
// Convert @username@twitter.com to <a href="https://twitter.com/username">@username@twitter.com</a>
textNodes = extractTextNodes(dom);
textNodes.forEach((node) => {
let html = node.nodeValue.replace(/</g, '&lt;').replace(/>/g, '&gt;');
if (/@[a-zA-Z0-9_]+@twitter\.com/g.test(html)) {
html = html.replaceAll(
/(@([a-zA-Z0-9_]+)@twitter\.com)/g,
'<a href="https://twitter.com/$2" rel="nofollow noopener noreferrer" target="_blank">$1</a>',
);
}
fauxDiv.innerHTML = html;
const nodes = Array.from(fauxDiv.childNodes);
node.replaceWith(...nodes);
});
if (postEnhanceDOM) {
postEnhanceDOM(dom); // mutate dom
}