office.sejong.dev/pipeline/head This commit looks good
Render GitHub-flavored markdown in the browser with marked and sanitize HTML with DOMPurify. Co-authored-by: Cursor <cursoragent@cursor.com>
30 lines
744 B
TypeScript
30 lines
744 B
TypeScript
import { marked } from 'marked';
|
|
import DOMPurify from 'dompurify';
|
|
|
|
marked.setOptions({
|
|
gfm: true,
|
|
breaks: true,
|
|
});
|
|
|
|
DOMPurify.addHook('afterSanitizeAttributes', (node) => {
|
|
if (node instanceof HTMLAnchorElement) {
|
|
node.setAttribute('target', '_blank');
|
|
node.setAttribute('rel', 'noopener noreferrer');
|
|
}
|
|
});
|
|
|
|
export function renderMarkdown(source: string): string {
|
|
const html = marked.parse(source ?? '', { async: false });
|
|
return DOMPurify.sanitize(typeof html === 'string' ? html : '', {
|
|
USE_PROFILES: { html: true },
|
|
});
|
|
}
|
|
|
|
export function isMarkdownFile(file: File) {
|
|
return (
|
|
file.type === 'text/markdown' ||
|
|
file.type === 'text/x-markdown' ||
|
|
/\.(?:md|markdown|mdown)$/i.test(file.name)
|
|
);
|
|
}
|