Skip to content

Commit 4dc4f20

Browse files
authored
feat(front): allow some HTML tags in markdown reports (#388)
## 📝 Description renderedtext/project-tasks#2650 ## ✅ Checklist - [x] I have tested this change - [ ] This change requires documentation update
1 parent 5389337 commit 4dc4f20

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

front/assets/js/report/index.tsx

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ import { render } from "preact";
33
import MarkdownIt, { PluginSimple } from "markdown-it";
44
import markdownItTextualUml from "markdown-it-textual-uml";
55
import Mermaid from "mermaid";
6+
import "github-markdown-css/github-markdown-light.css";
7+
import DOMPurify from 'dompurify';
68

79
import * as toolbox from "js/toolbox";
810
import { useEffect, useState } from "preact/hooks";
911

1012
Mermaid.initialize({ startOnLoad: false, theme: `default`, securityLevel: `strict` });
11-
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);
1218

1319
export default function ({ config, dom }: { dom: HTMLElement, config: any, }) {
1420
render(<App reportUrl={config.reportUrl} context={config.reportContext}/>, dom);
@@ -76,10 +82,37 @@ const MarkdownBody = (props: { markdown: string, }) => {
7682
}
7783
}, [props.markdown]);
7884

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+
79112
return (
80113
<div
81114
className="markdown-body"
82-
dangerouslySetInnerHTML={{ __html: md.render(props.markdown) }}
115+
dangerouslySetInnerHTML={{ __html: sanitizedHtml }}
83116
/>
84117
);
85118
};

0 commit comments

Comments
 (0)