blob: 666ed7363b9f72200ceee25898db85110158ec70 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
import { marked } from "marked";
import { JSDOM } from "jsdom";
import DOMPurify from "dompurify";
// ? to ? helper
function htmlEscapeToText(text: string) {
return text.replace(/\&\#[0-9]*;|&/g, function (escapeCode) {
if (escapeCode.match(/amp/)) {
return "&";
}
const code = escapeCode.match(/[0-9]+/);
return String.fromCharCode(Number(code));
});
}
// Extra marked renderer (used to render plaintext event description for page metadata)
// Adapted from https://dustinpfister.github.io/2017/11/19/nodejs-marked/
export const renderPlain = () => {
var render = new marked.Renderer();
// render just the text of a link, strong, em
render.link = function (href, title, text) {
return text;
};
render.strong = function (text) {
return text;
};
render.em = function (text) {
return text;
};
// render just the text of a paragraph
render.paragraph = function (text) {
return htmlEscapeToText(text) + "\r\n";
};
// render nothing for headings, images, and br
render.heading = function (text, level) {
return "";
};
render.image = function (href, title, text) {
return "";
};
render.br = function () {
return "";
};
return render;
};
export const markdownToSanitizedHTML = (markdown: string) => {
const html = marked.parse(markdown) as string;
const window = new JSDOM("").window;
const purify = DOMPurify(window);
const clean = purify.sanitize(html);
return clean;
};
|