From 1c9843333397b6540924633c09417e3e04181fab Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Fri, 23 Dec 2022 00:40:25 +0800 Subject: [PATCH] Linkify twitter usernames This is due to some folks cross-posting from Twitter. --- src/utils/enhance-content.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/utils/enhance-content.js b/src/utils/enhance-content.js index 4783f3c3..417c03c2 100644 --- a/src/utils/enhance-content.js +++ b/src/utils/enhance-content.js @@ -57,6 +57,23 @@ function enhanceContent(content, opts = {}) { block.replaceWith(pre); }); + // TWITTER USERNAMES + // ================= + // Convert @username@twitter.com to @username@twitter.com + textNodes = extractTextNodes(dom); + textNodes.forEach((node) => { + let html = node.nodeValue.replace(//g, '>'); + if (/@[a-zA-Z0-9_]+@twitter\.com/g.test(html)) { + html = html.replaceAll( + /(@([a-zA-Z0-9_]+)@twitter\.com)/g, + '$1', + ); + } + fauxDiv.innerHTML = html; + const nodes = Array.from(fauxDiv.childNodes); + node.replaceWith(...nodes); + }); + if (postEnhanceDOM) { postEnhanceDOM(dom); // mutate dom }