forked from readmeio/markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
267 lines (230 loc) · 7.87 KB
/
index.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* eslint-disable no-param-reassign */
require('./styles/main.scss');
const Variable = require('@readme/variable');
const generateTOC = require('mdast-util-toc');
const React = require('react');
const rehypeRaw = require('rehype-raw');
const rehypeReact = require('rehype-react');
const rehypeSanitize = require('rehype-sanitize');
const rehypeStringify = require('rehype-stringify');
const remarkBreaks = require('remark-breaks');
const remarkDisableTokenizers = require('remark-disable-tokenizers');
const remarkFrontmatter = require('remark-frontmatter');
const remarkParse = require('remark-parse');
const remarkRehype = require('remark-rehype');
const remarkSlug = require('remark-slug');
const remarkStringify = require('remark-stringify');
const unified = require('unified');
const { map: mapNodes } = require('unist-util-map');
const { selectAll } = require('unist-util-select');
const Components = require('./components');
const { getHref } = require('./components/Anchor');
const BaseUrlContext = require('./contexts/BaseUrl');
const CustomParsers = Object.values(require('./processor/parse'));
const customCompilers = Object.values(require('./processor/compile'));
const registerCustomComponents = require('./lib/registerCustomComponents');
const { options, parseOptions } = require('./options');
const { icons: calloutIcons } = require('./processor/parse/flavored/callout');
const toPlainText = require('./processor/plugin/plain-text');
const sectionAnchorId = require('./processor/plugin/section-anchor-id');
const tableFlattening = require('./processor/plugin/table-flattening');
const createSchema = require('./sanitize.schema');
const {
GlossaryItem,
Code,
Table,
Anchor,
Heading,
Callout,
CodeTabs,
Image,
Embed,
HTMLBlock,
Style,
TableOfContents,
} = Components;
export { Components };
/**
* Setup Options
* !Normalize Magic Block Raw Text!
*/
export function setup(blocks, opts = {}) {
// merge default and user options
opts = parseOptions(opts);
if (!opts.sanitize) {
opts.sanitize = createSchema(opts);
Object.values(Components).forEach(Component => Component.sanitize && Component.sanitize(opts.sanitize));
}
// normalize magic block linebreaks
if (opts.normalize && blocks) {
blocks = blocks.replace(/\[block:/g, '\n\n[block:').replace(/\[\/block\]/g, '[/block]\n');
}
return [`${blocks}\n\n `, opts];
}
export const utils = {
BaseUrlContext,
getHref,
GlossaryContext: GlossaryItem.GlossaryContext,
options,
VariablesContext: Variable.VariablesContext,
calloutIcons,
};
/**
* Core markdown text processor
*/
export function processor(opts = {}) {
/*
* This is kinda complicated: "markdown" within ReadMe is
* often more than just markdown. It can also include HTML,
* as well as custom syntax constructs such as <<variables>>,
* and other special features.
*
* We use the Unified text processor to parse and transform
* Markdown to various output formats, such as a React component
* tree. (See https://github.com/unifiedjs/unified for more.)
*
* The order for processing ReadMe-flavored markdown is as follows:
* - parse markdown
* - parse custom syntaxes add-ons using custom compilers
* - convert from a remark mdast (markdown ast) to a rehype hast (hypertext ast)
* - extract any raw HTML elements
* - sanitize and remove any disallowed attributes
* - output the hast to a React vdom with our custom components
*/
[, opts] = setup('', opts);
const { sanitize } = opts;
return unified()
.use(remarkParse, opts.markdownOptions)
.use(remarkFrontmatter, ['yaml', 'toml'])
.data('settings', opts.settings)
.data('compatibilityMode', opts.compatibilityMode)
.data('alwaysThrow', opts.alwaysThrow)
.use(!opts.correctnewlines ? remarkBreaks : () => {})
.use(CustomParsers.map(parser => parser.sanitize?.(sanitize) || parser))
.use(remarkSlug)
.use(remarkDisableTokenizers, opts.disableTokenizers)
.use(remarkRehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeSanitize, sanitize);
}
export function plain(text, opts = {}, components = {}) {
if (!text) return null;
[text, opts] = setup(text, opts);
return processor(opts)
.use(rehypeReact, {
createElement: React.createElement,
Fragment: React.Fragment,
components,
})
.processSync(text).contents;
}
/**
* return a React VDOM component tree
*/
// eslint-disable-next-line react/prop-types
const PinWrap = ({ children }) => <div className="pin">{children}</div>; // @todo: move this to it's own component
const count = {};
export function reactProcessor(opts = {}, components = {}) {
[, opts] = setup('', opts);
const { sanitize } = opts;
return processor({ sanitize, ...opts })
.use(sectionAnchorId)
.use(rehypeReact, {
createElement: React.createElement,
Fragment: React.Fragment,
components: {
'code-tabs': CodeTabs(opts),
'html-block': HTMLBlock(opts),
'rdme-callout': Callout,
'readme-variable': Variable,
'readme-glossary-item': GlossaryItem,
'rdme-embed': Embed(opts),
'rdme-pin': PinWrap,
table: Table,
a: Anchor,
h1: Heading(1, count, opts),
h2: Heading(2, count, opts),
h3: Heading(3, count, opts),
h4: Heading(4, count, opts),
h5: Heading(5, count, opts),
h6: Heading(6, count, opts),
code: Code(opts),
img: Image(opts),
style: Style(opts),
...registerCustomComponents(components, sanitize, opts.customComponentPrefix),
},
});
}
export function react(content, opts = {}, components = {}) {
if (!content) return null;
else if (typeof content === 'string') [content, opts] = setup(content, opts);
else [, opts] = setup('', opts);
const proc = reactProcessor(opts, components);
if (typeof content === 'string') content = proc.parse(content);
return proc.stringify(proc.runSync(content));
}
export function reactTOC(tree, opts = {}) {
if (!tree) return null;
[, opts] = setup('', opts);
const proc = processor(opts).use(rehypeReact, {
createElement: React.createElement,
components: {
p: React.Fragment,
'readme-variable': Variable,
'readme-glossary-item': GlossaryItem,
},
});
// Normalize Heading Levels
const minLevel = selectAll('heading', tree).reduce((i, { depth }) => (!i || depth <= i ? depth : i), false); // determine "root" depth
tree = mapNodes(tree, n => {
if (n.type === 'heading') n.depth -= minLevel - 1;
return n;
});
const toc = generateTOC(tree, { maxDepth: 2 }).map;
const ast = toc ? proc.stringify(proc.runSync(toc)) : false;
return ast ? React.createElement(TableOfContents, {}, ast) : null;
}
/**
* transform markdown in to HTML
*/
export function html(text, opts = {}) {
if (!text) return null;
[text, opts] = setup(text, opts);
return processor(opts).use(rehypeStringify).processSync(text).contents;
}
/**
* convert markdown to an hast object
*/
export function hast(text, opts = {}) {
if (!text) return null;
[text, opts] = setup(text, opts);
const rdmd = processor(opts).use(tableFlattening);
const node = rdmd.parse(text);
return rdmd.runSync(node);
}
/**
* convert markdown to an mdast object
*/
export function mdast(text, opts = {}) {
if (!text) return null;
[text, opts] = setup(text, opts);
return processor(opts).parse(text);
}
/**
* Converts an AST node to plain text
*/
export function astToPlainText(node, opts = {}) {
if (!node) return '';
[, opts] = setup('', opts);
return processor(opts).use(toPlainText).stringify(node);
}
/**
* compile mdast to ReadMe-flavored markdown
*/
export function md(tree, opts = {}) {
if (!tree) return null;
[, opts] = setup('', opts);
return processor(opts).use(remarkStringify, opts.markdownOptions).use(customCompilers).stringify(tree);
}
const ReadMeMarkdown = (text, opts = {}) => react(text, opts);
export default ReadMeMarkdown;