-
Notifications
You must be signed in to change notification settings - Fork 2
/
transpile-mdx.js
72 lines (64 loc) · 2.17 KB
/
transpile-mdx.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
import fs from 'fs';
import { marked } from 'marked';
const headers = [];
const walkTokens = (token) => {
if (token.type === 'heading' && token.depth === 1 && token.tokens[0].type === 'link') {
headers.push(token);
}
};
marked.use({ walkTokens });
function getWord(str) {
return str.match(/(^|\B)#(?![0-9_]+\b)([a-zA-Z0-9_/-]{1,30})(\b|\r)/)[2];
}
const renderer = new marked.Renderer();
renderer.heading = (text, level) => {
if (level === 1) {
return `<h${level} id=${getWord(text)}>${text}</h${level}>`;
} else {
return `<h${level}>${text}</h${level}>`;
}
}
renderer.table = (header, body) => {
while(header.indexOf('align') > -1) {
header = header.replace('align', 'class');
}
while(body.indexOf('align') > -1) {
body = body.replace('align', 'class');
}
return `<table><thead>${header}</thead> <tbody>${body}</tbody></table>`;
}
marked.setOptions({
renderer
});
function transpile() {
const files = [];
function getFiles(nextPath) {
if(fs.existsSync(nextPath) && fs.lstatSync(nextPath).isDirectory()) {
const nextDirPath = fs.readdirSync(nextPath);
nextDirPath.forEach(filePath => getFiles(`${nextPath}/${filePath}`, files));
} else {
if (nextPath.indexOf('.md') > -1) {
const markdown = fs.readFileSync(nextPath, 'utf8');
files.push(marked(markdown));
}
}
}
getFiles('./zoo-modules');
function getHeaderString (header) {
return `<a href="${header.tokens[0].href}">${header.tokens[0].text}</a>`;
}
const file = fs.readFileSync('./template.html', 'utf8');
const result = file.replace('<app-root>', `
<nav>
${headers.map(header => getHeaderString(header)).join('')}
</nav>
<div class="content">
${files.join('')}
</div>
`);
fs.writeFileSync('./docs/index.html', result);
console.log('Transpilation finished and files written!')
}
transpile();
const cmps = fs.readFileSync('./node_modules/@zooplus/zoo-web-components/dist/zoo-web-components.js', 'utf8');
fs.writeFileSync('./docs/zoo-web-components.js', cmps);