mirror of
https://github.com/cheeaun/phanpy.git
synced 2024-11-24 18:25:41 +03:00
Linkify twitter usernames
This is due to some folks cross-posting from Twitter.
This commit is contained in:
parent
7e84088d67
commit
1c98433333
1 changed files with 17 additions and 0 deletions
|
@ -57,6 +57,23 @@ function enhanceContent(content, opts = {}) {
|
||||||
block.replaceWith(pre);
|
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, '<').replace(/>/g, '>');
|
||||||
|
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) {
|
if (postEnhanceDOM) {
|
||||||
postEnhanceDOM(dom); // mutate dom
|
postEnhanceDOM(dom); // mutate dom
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue