-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
/
parseMarkdown.js
302 lines (257 loc) · 9.58 KB
/
parseMarkdown.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import marked from 'marked/lib/marked';
import { LANGUAGES_IN_PROGRESS } from 'docs/src/modules/constants';
import kebabCase from 'lodash/kebabCase';
import { rewriteUrlForNextExport } from 'next/dist/next-server/lib/router/rewrite-url-for-export';
import textToHash from 'docs/src/modules/utils/textToHash';
import prism from 'docs/src/modules/utils/prism';
const headerRegExp = /---[\r\n]([\s\S]*)[\r\n]---/;
const titleRegExp = /# (.*)[\r\n]/;
const descriptionRegExp = /<p class="description">(.*)<\/p>[\r\n]/;
const headerKeyValueRegExp = /(.*): (.*)/g;
const emptyRegExp = /^\s*$/;
const notEnglishMarkdownRegExp = /-([a-z]{2})\.md$/;
/**
* Extract information from the top of the markdown.
* For instance, the following input:
*
* ---
* title: Backdrop React Component
* components: Backdrop
* ---
*
* # Backdrop
*
* should output:
* { title: 'Backdrop React Component', components: ['Backdrop'] }
*/
export function getHeaders(markdown) {
let header = markdown.match(headerRegExp);
if (!header) {
return {
components: [],
};
}
header = header[1];
let regexMatches;
const headers = {};
// eslint-disable-next-line no-cond-assign
while ((regexMatches = headerKeyValueRegExp.exec(header)) !== null) {
headers[regexMatches[1]] = regexMatches[2];
}
if (headers.components) {
headers.components = headers.components
.split(',')
.map((x) => x.trim())
.sort();
} else {
headers.components = [];
}
return headers;
}
export const demoRegexp = /^"demo": "(.*)"/;
export function getContents(markdown) {
return markdown
.replace(headerRegExp, '') // Remove header information
.split(/^{{("demo":[^}]*)}}$/gm) // Split markdown into an array, separating demos
.filter((content) => !emptyRegExp.test(content)); // Remove empty lines
}
export function getTitle(markdown) {
const matches = markdown.match(titleRegExp);
if (!matches || !matches[1]) {
throw new Error('Missing title in the page');
}
return matches[1];
}
export function getDescription(markdown) {
const matches = markdown.match(descriptionRegExp);
return matches?.[1];
}
/**
* Render markdown used in the Material-UI docs
*
* @param {string} markdown
* @param {object} [options]
* @param {function} [options.highlight] - https://marked.js.org/#/USING_ADVANCED.md#highlight
* @param {object} [options.rest] - properties from https://marked.js.org/#/USING_PRO.md#renderer
*/
export function render(markdown, options = {}) {
const { highlight, ...rendererOptions } = options;
const renderer = Object.assign(new marked.Renderer(), rendererOptions);
const markedOptions = {
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: false,
smartLists: true,
smartypants: false,
highlight,
renderer,
};
return marked(markdown, markedOptions);
}
const externs = [
'https://material.io/',
'https://getbootstrap.com/',
'https://www.amazon.com/',
'https://materialdesignicons.com/',
'https://www.w3.org/',
'https://devexpress.github.io/',
'https://ui-kit.co/',
];
/**
*
* @param {object} config
* @param {() => string} config.requireRaw - returnvalue of require.context
* @param {string} config.pageFilename - filename relative to nextjs pages directory
*/
export function prepareMarkdown(config) {
const { pageFilename, requireRaw } = config;
const demos = {};
const docs = {};
const headingHashes = {};
const headingHashesFallbackTranslated = {};
requireRaw
.keys()
.sort((file) => (file.match(notEnglishMarkdownRegExp) ? 0 : -1))
.forEach((filename) => {
if (filename.indexOf('.md') !== -1) {
const matchNotEnglishMarkdown = filename.match(notEnglishMarkdownRegExp);
const userLanguage =
matchNotEnglishMarkdown &&
LANGUAGES_IN_PROGRESS.indexOf(matchNotEnglishMarkdown[1]) !== -1
? matchNotEnglishMarkdown[1]
: 'en';
const markdown = requireRaw(filename);
const contents = getContents(markdown);
const headers = getHeaders(markdown);
const title = headers.title || getTitle(markdown);
const description = headers.description || getDescription(markdown);
if (headers.components.length > 0) {
contents.push(`
## API
${headers.components
.map(
(component) =>
`- [<${component} />](${rewriteUrlForNextExport(`/api/${kebabCase(component)}`)})`,
)
.join('\n')}
`);
}
const toc = [];
let englishHashIndex = -1;
const rendered = contents.map((content) => {
if (demos && demoRegexp.test(content)) {
try {
return JSON.parse(`{${content}}`);
} catch (err) {
console.error('JSON.parse fails with: ', `{${content}}`);
console.error(err);
return null;
}
}
return render(content, {
highlight: prism,
heading: (headingHtml, level) => {
// Small title. No need for an anchor.
// It's reducing the risk of duplicated id and it's fewer elements in the DOM.
if (level >= 4) {
return `<h${level}>${headingHtml}</h${level}>`;
}
const headingText = headingHtml
.replace(
/([\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF])\uFE0F?/g,
'',
) // remove emojis
.replace(/<\/?[^>]+(>|$)/g, '') // remove HTML
.trim();
// Standardizes the hash from the default location (en) to different locations
// Need english.md file parsed first
let hash;
if (matchNotEnglishMarkdown) {
englishHashIndex += 1;
hash = Object.keys(headingHashes)[englishHashIndex];
if (!hash) hash = textToHash(headingText, headingHashesFallbackTranslated);
} else hash = textToHash(headingText, headingHashes);
// enable splitting of long words from function name + first arg name
// Closing parens are less interesting since this would only allow breaking one character earlier.
// Applying the same mechanism would also allow breaking of non-function signatures like "Community help (free)".
// To detect that we enabled breaking of open/closing parens we'd need a context-sensitive parser.
const displayText = headingText.replace(/([^\s]\()/g, '$1​');
/**
* create a nested structure with 2 levels starting with level 2 e.g.
* [{...level2, children: [level3, level3, level3]}, level2]
*/
if (level === 2) {
toc.push({
text: displayText,
level,
hash,
children: [],
});
} else if (level === 3) {
if (!toc[toc.length - 1]) {
throw new Error(`Missing parent level for: ${headingText}`);
}
toc[toc.length - 1].children.push({
text: displayText,
level,
hash,
});
}
return [
`<h${level}>`,
`<a class="anchor-link" id="${hash}"></a>`,
headingHtml,
`<a class="anchor-link-style" aria-hidden="true" aria-label="anchor" href="#${hash}">`,
'<svg><use xlink:href="#anchor-link-icon" /></svg>',
'</a>',
`</h${level}>`,
].join('');
},
link: (href, linkTitle, linkText) => {
let more = '';
if (externs.some((domain) => href.indexOf(domain) !== -1)) {
more = ' target="_blank" rel="noopener nofollow"';
}
let finalHref = href;
if (
userLanguage !== 'en' &&
finalHref.indexOf('/') === 0 &&
finalHref !== '/size-snapshot'
) {
finalHref = `/${userLanguage}${finalHref}`;
}
return `<a href="${finalHref}"${more}>${linkText}</a>`;
},
});
});
// fragment link symbol
rendered.unshift(`<svg style="display: none;" xmlns="http://www.w3.org/2000/svg">
<symbol id="anchor-link-icon" viewBox="0 0 16 16">
<path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z" />
</symbol>
</svg>`);
const location = headers.filename || `/docs/src/pages/${pageFilename}/${filename}`;
const localized = { description, location, rendered, toc, title };
docs[userLanguage] = localized;
} else if (filename.indexOf('.tsx') !== -1) {
const demoName = `pages/${pageFilename}/${filename
.replace(/\.\//g, '')
.replace(/\.tsx/g, '.js')}`;
demos[demoName] = {
...demos[demoName],
moduleTS: filename,
rawTS: requireRaw(filename),
};
} else {
const demoName = `pages/${pageFilename}/${filename.replace(/\.\//g, '')}`;
demos[demoName] = {
...demos[demoName],
module: filename,
raw: requireRaw(filename),
};
}
});
return { demos, docs };
}