add target=blank to each anchor

This commit is contained in:
Julien Fastré 2024-02-19 13:08:11 +01:00
parent 95ee573dc5
commit d0af191a00
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB

View File

@ -120,10 +120,28 @@ const truncateContent = (content: string): string => {
return truncatedContent;
};
const preprocess = (markdown: string): string => {
return markdown;
}
const postprocess = (html: string): string => {
DOMPurify.addHook('afterSanitizeAttributes', (node) => {
if ('target' in node) {
node.setAttribute('target', '_blank');
node.setAttribute('rel', 'noopener noreferrer');
}
if (!node.hasAttribute('target') && (node.hasAttribute('xlink:href') || node.hasAttribute('href'))) {
node.setAttribute('xlink:show', 'new');
}
})
return DOMPurify.sanitize(html);
}
const convertMarkdownToHtml = (markdown: string): string => {
marked.use({'hooks': {postprocess, preprocess}});
const rawHtml = marked(markdown);
// console.log(rawHtml)
return DOMPurify.sanitize(rawHtml)
return rawHtml;
};
const prepareContent = (content: string): string => {