Files
directus/app/src/utils/md.ts
Nitwel 7639023a4f Emoji Picker & Comment improvements (#11946)
* add simple emoji picker and improve comments

* add v-md option to open in new tab

* fix styling when editing comment

* clean up code

* use script setup

* use different emoji lib

* fix inserting emojis into text

* fix search styling

* always show cancel button

* comment style tweaks

* clean up emoji picker

* add placeholder to v-template-input

* cleanup comments some more

* clean up comments sooooome more

* fix notify message on update

* update comment markdown styling

* button and icon hover colors

* move styling to global file

* clean up code

Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
Co-authored-by: Ben Haynes <ben@rngr.org>
2022-04-22 12:18:45 -04:00

32 lines
797 B
TypeScript

import { marked } from 'marked';
import dompurify from 'dompurify';
type Options = {
target: '_blank' | '_self' | '_parent' | '_top';
};
const renderer = new marked.Renderer();
/**
* Render and sanitize a markdown string
*/
export function md(str: string, options: Options = { target: '_self' }): string {
dompurify.addHook('afterSanitizeAttributes', (node) => {
if (node.tagName === 'A' && node.getAttribute('target') === '_blank') {
node.setAttribute('rel', 'noopener noreferrer');
}
});
renderer.link = function (href, title, text) {
const link = marked.Renderer.prototype.link.apply(this, [href, title, text]);
return link.replace('<a', `<a target="${options.target}"`);
};
return dompurify.sanitize(
marked(str, {
renderer,
}),
{ ADD_ATTR: ['target'] }
);
}