1 | const React = require('react');
|
2 | const unified = require('unified');
|
3 | const sanitize = require('hast-util-sanitize/lib/github.json');
|
4 | const Variable = require('@readme/variable');
|
5 | const remarkRehype = require('remark-rehype');
|
6 | const rehypeRaw = require('rehype-raw');
|
7 | const remarkParse = require('remark-parse');
|
8 | const rehypeSanitize = require('rehype-sanitize');
|
9 | const rehypeReact = require('rehype-react');
|
10 | const breaks = require('remark-breaks');
|
11 |
|
12 | const variableParser = require('./variable-parser');
|
13 | const gemojiParser = require('./gemoji-parser');
|
14 |
|
15 | const table = require('./components/Table');
|
16 | const heading = require('./components/Heading');
|
17 | const anchor = require('./components/Anchor');
|
18 | const code = require('./components/Code');
|
19 |
|
20 |
|
21 | sanitize.tagNames.push('input');
|
22 | sanitize.ancestors.input = ['li'];
|
23 |
|
24 | const GlossaryItem = require('./GlossaryItem');
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | module.exports = function markdown(text, opts = {}) {
|
45 | if (!text) return null;
|
46 |
|
47 | return unified()
|
48 | .use(remarkParse)
|
49 | .use(variableParser.sanitize(sanitize))
|
50 | .use(!opts.correctnewlines ? breaks : () => {})
|
51 | .use(gemojiParser.sanitize(sanitize))
|
52 | .use(remarkRehype, { allowDangerousHtml: true })
|
53 | .use(rehypeRaw)
|
54 | .use(rehypeSanitize)
|
55 | .use(rehypeReact, {
|
56 | createElement: React.createElement,
|
57 | components: {
|
58 | 'readme-variable': Variable,
|
59 | 'readme-glossary-item': GlossaryItem,
|
60 | table: table(sanitize),
|
61 | h1: heading('h1', sanitize),
|
62 | h2: heading('h2', sanitize),
|
63 | h3: heading('h3', sanitize),
|
64 | h4: heading('h4', sanitize),
|
65 | h5: heading('h5', sanitize),
|
66 | h6: heading('h6', sanitize),
|
67 | a: anchor(sanitize),
|
68 | code: code(sanitize),
|
69 |
|
70 |
|
71 |
|
72 | div: props => React.createElement(React.Fragment, props),
|
73 | },
|
74 | })
|
75 | .processSync(text).contents;
|
76 | };
|