phanpy/src/utils/enhance-content.js

48 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-12-10 12:14:48 +03:00
import emojifyText from './emojify-text';
export default (content, opts = {}) => {
const { emojis, postEnhanceDOM = () => {} } = opts;
2022-12-10 12:14:48 +03:00
let enhancedContent = content;
const dom = document.createElement('div');
dom.innerHTML = enhancedContent;
2022-12-10 12:14:48 +03:00
// 1. Emojis
2022-12-10 12:14:48 +03:00
if (emojis) {
enhancedContent = emojifyText(enhancedContent, emojis);
}
// 2. Add target="_blank" to all links with no target="_blank"
// E.g. `note` in `account`
const links = Array.from(dom.querySelectorAll('a:not([target="_blank"])'));
links.forEach((link) => {
link.setAttribute('target', '_blank');
});
// 3. Code blocks
2022-12-10 12:14:48 +03:00
// Check for <p> with markdown-like content "```"
const blocks = Array.from(dom.querySelectorAll('p')).filter((p) =>
/^```[^]+```$/g.test(p.innerText.trim()),
);
blocks.forEach((block) => {
const pre = document.createElement('pre');
const code = document.createElement('code');
const breaks = block.querySelectorAll('br');
breaks.forEach((br) => br.replaceWith('\n'));
2022-12-10 12:14:48 +03:00
code.innerHTML = block.innerText
.trim()
// .replace(/^```/g, '')
// .replace(/```$/g, '')
.replace(/^[\n\r]+/, '');
pre.appendChild(code);
block.replaceWith(pre);
});
if (postEnhanceDOM) {
postEnhanceDOM(dom); // mutate dom
}
enhancedContent = dom.innerHTML;
2022-12-10 12:14:48 +03:00
return enhancedContent;
};