~james/core

core/web/src/lib/markup/inline.ts -rw-r--r-- 625 bytes
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import MarkdownIt from "markdown-it";
import sanitizeHtml from "sanitize-html";

const md = new MarkdownIt({ html: false, linkify: true });

const TAGS = ["strong", "em", "s", "code"];

const optionsFor = (links: boolean): sanitizeHtml.IOptions => ({
	allowedTags: links ? [...TAGS, "a"] : TAGS,
	allowedAttributes: { a: ["href", "rel"] },
	allowedSchemes: ["http", "https", "mailto"],
	transformTags: {
		a: sanitizeHtml.simpleTransform("a", { rel: "nofollow ugc noopener noreferrer" })
	}
});

export const renderInline = (source: string, links = true): string =>
	sanitizeHtml(md.renderInline(source), optionsFor(links));