Skip to content

Commit 1a118cf

Browse files
committed
feat(front): allow some HTML tags in markdown reports
1 parent 027dba9 commit 1a118cf

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

front/assets/js/report/index.tsx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ import MarkdownIt, { PluginSimple } from "markdown-it";
44
import markdownItTextualUml from "markdown-it-textual-uml";
55
import Mermaid from "mermaid";
66
import "github-markdown-css/github-markdown-light.css";
7+
import DOMPurify from 'dompurify';
78

89
import * as toolbox from "js/toolbox";
910
import { useEffect, useState } from "preact/hooks";
1011

1112
Mermaid.initialize({ startOnLoad: false, theme: `default`, securityLevel: `strict` });
12-
const md = MarkdownIt().use(markdownItTextualUml as PluginSimple);
13+
const md = MarkdownIt({
14+
html: true,
15+
linkify: false,
16+
typographer: true
17+
}).use(markdownItTextualUml as PluginSimple);
1318

1419
export default function ({ config, dom }: { dom: HTMLElement, config: any, }) {
1520
render(<App reportUrl={config.reportUrl} context={config.reportContext}/>, dom);
@@ -77,10 +82,37 @@ const MarkdownBody = (props: { markdown: string, }) => {
7782
}
7883
}, [props.markdown]);
7984

85+
const renderedHtml = md.render(props.markdown);
86+
const sanitizedHtml = DOMPurify.sanitize(renderedHtml, {
87+
ALLOWED_TAGS: [
88+
// Basic
89+
`details`, `summary`, `p`, `br`, `h1`, `h2`, `h3`, `h4`, `h5`, `h6`,
90+
`ul`, `ol`, `li`, `blockquote`, `pre`, `hr`, `div`,
91+
// Tables
92+
`table`, `thead`, `tbody`, `tr`, `td`, `th`,
93+
//Formating
94+
`strong`, `b`, `em`, `i`, `u`, `code`, `span`,
95+
`del`, `s`,
96+
`sup`, `sub`,
97+
`kbd`,
98+
`mark`,
99+
`ins`,
100+
`small`,
101+
`abbr`
102+
],
103+
ALLOWED_ATTR: [
104+
`title`,
105+
`open`
106+
],
107+
FORBID_TAGS: [`a`, `img`, `script`, `object`, `embed`, `iframe`, `link`],
108+
FORBID_ATTR: [`href`, `src`, `class`, `id`, `style`, `target`],
109+
ALLOW_DATA_ATTR: false
110+
});
111+
80112
return (
81113
<div
82114
className="markdown-body"
83-
dangerouslySetInnerHTML={{ __html: md.render(props.markdown) }}
115+
dangerouslySetInnerHTML={{ __html: sanitizedHtml }}
84116
/>
85117
);
86118
};

0 commit comments

Comments
 (0)