Files
gitlab-instance-0a899031_no…/app/community/components/renderMarkdown.ts
2026-03-15 04:25:45 -05:00

20 lines
621 B
TypeScript

import { marked } from "marked";
function escapeHtml(source: string): string {
return source
.replaceAll("&", "&")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll('"', "&quot;")
.replaceAll("'", "&#39;");
}
export function renderCommunityMarkdown(source: string): string {
const html = marked.parse(escapeHtml(source), { breaks: true, gfm: true }) as string;
return html
.replaceAll("<a href=", '<a target="_blank" rel="noopener noreferrer" href=')
.replace(/href="(?:javascript|data):[^"]*"/gi, 'href="#"')
.replace(/src="(?:javascript):[^"]*"/gi, 'src=""');
}