20 lines
621 B
TypeScript
20 lines
621 B
TypeScript
import { marked } from "marked";
|
|
|
|
function escapeHtml(source: string): string {
|
|
return source
|
|
.replaceAll("&", "&")
|
|
.replaceAll("<", "<")
|
|
.replaceAll(">", ">")
|
|
.replaceAll('"', """)
|
|
.replaceAll("'", "'");
|
|
}
|
|
|
|
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=""');
|
|
}
|