/
build-guides.js
252 lines (231 loc) · 6.89 KB
/
build-guides.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
const fs = require('fs-extra');
const path = require('path');
const matter = require('gray-matter');
// codemirror/5.65.11
const CODE_MIRROR_VERSION = '5.65.11'; // 2022-12-20
const INJECTED_STYLESHEET = `
.output:focus-within {
outline: Highlight auto 1px;
outline: -webkit-focus-ring-color auto 1px
}
.output math-field:focus, .output math-field:focus-within {
outline: none;
}
.zebra-zone {
display: flex;
align-items: center;
justify-content: center;
height: 6em;
font-size: 1.5em;
font-weight: 600;
color: #999;
background: repeating-linear-gradient(
-45deg,
#ffffff,
#fff 40px,
#eeeeee 40px,
#eee 80px
);
}
`;
/** Walk all the files and directory in `dir` */
async function* walk(dir) {
if (fs.lstatSync(dir).isFile()) yield dir;
if (fs.lstatSync(dir).isDirectory()) {
for await (const d of await fs.promises.opendir(dir)) {
const entry = path.join(dir, d.name);
if (d.isDirectory()) yield* await walk(entry);
else if (d.isFile()) yield entry;
}
}
}
function escapeHTML(s) {
if (s === '<!-- -->') return s;
return s
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
/** Parse a single ``` block */
function parseCodeFence(lines, index) {
if (index >= lines.length) return undefined;
const fenceChar = lines[index][0];
if (fenceChar !== '`' && fenceChar !== '~') return undefined;
let fence = fenceChar;
let i = 1;
while (i < lines[index].length && lines[index][i] === fenceChar) {
fence += fenceChar;
i += 1;
}
if (fence.length < 3) return undefined;
const qualifiedType = lines[index].substring(fence.length).split(' ');
const type = qualifiedType[0];
const qualifier = qualifiedType[1];
let content = [];
index += 1;
while (index < lines.length && lines[index] !== fence) {
if (!lines[index] && qualifier) {
// Replace empty lines with some spaces, to prevent
// eleventy from getting confused
content.push('<!-- -->');
} else {
content.push(lines[index]);
}
index++;
}
return {
type: type.split(' ')[0],
isPlayground: qualifier === 'playground',
isExample: type === 'html' && qualifier === 'example',
index: Math.min(index + 1, lines.length - 1),
content,
};
}
/** Parse a sequence of code fences and turn them into a
* playground or example if necessary
*/
function parseCodeFences(lines, i) {
const fence1 = parseCodeFence(lines, i);
if (!fence1) return undefined;
i = fence1.index;
while (i < lines.length && lines[i].trim() === '') {
i++;
}
let result = [];
if (fence1.isPlayground) {
const fence2 = parseCodeFence(lines, i);
if (fence2) {
i = fence2.index;
}
while (i < lines.length && lines[i].trim() === '') {
i++;
}
const fence3 = parseCodeFence(lines, i);
if (fence3) {
i = fence3.index;
}
result = [
...result,
'\n',
'\n',
'<code-playground layout="stack" show-line-numbers>',
'<style slot="style">' + INJECTED_STYLESHEET + '</style>',
];
result.push('<div slot="' + fence1.type + '">');
fence1.content.forEach((x) => result.push(escapeHTML(x)));
result.push('</div>');
if (fence2) {
result.push('<div slot="' + fence2.type + '">');
fence2.content.forEach((x) => result.push(escapeHTML(x)));
result.push('</div>');
}
if (fence3) {
result.push('<div slot="' + fence3.type + '">');
fence3.content.forEach((x) => result.push(escapeHTML(x)));
result.push('</div>');
}
result = [...result, '</code-playground>', '\n', '\n'];
} else if (fence1.isExample) {
result = [
...result,
'\n',
'\n',
...fence1.content,
'\n',
'\n',
'```html',
...fence1.content,
'```',
];
} else {
result = [
...result,
'\n',
'\n',
'```' + fence1.type,
...fence1.content,
'```',
'\n',
'\n',
];
}
return {
content: result,
index: i - 1,
};
}
function upgradeCodeFences(content) {
const lines = content.split('\n');
let result = [];
let usesCodeMirror = false;
for (let i = 0; i < lines.length; i++) {
const fence = parseCodeFences(lines, i);
if (!fence) {
result.push(lines[i]);
} else {
usesCodeMirror = true;
i = fence.index;
result.push(...fence.content);
}
}
return [usesCodeMirror, result.join('\n')];
}
const run = async (dir, sidebar) => {
for await (const x of walk(dir)) {
if (x.endsWith('.png') || x.endsWith('.jpeg') || x.endsWith('.jpg')) {
fs.copy(x, path.resolve('./src/build/assets', path.basename(x)));
} else if (x.endsWith('.md')) {
fs.readFile(x, 'utf8', (err, file) => {
const graymatter = matter(file);
const frontmatter = graymatter.data;
if (!frontmatter || !frontmatter.permalink) {
// console.warn('Skipping ' + x + ': no frontmatter permalink.');
} else {
try {
const [usesCodeMirror, content] = upgradeCodeFences(
graymatter.content
);
let permalink = frontmatter.permalink.slice(1);
if (permalink.length === 0) permalink = 'index';
const dest = path.resolve('./src/build/', permalink);
fs.mkdirSync(path.parse(dest).dir, { recursive: true });
if (!frontmatter.head) {
frontmatter.head = {};
}
if (!frontmatter.layout) {
frontmatter.layout = 'sdk-documentation-layout';
}
if (sidebar) {
if (!frontmatter.sidebar) {
frontmatter.sidebar = [];
}
frontmatter.sidebar.push({ nav: sidebar });
}
if (usesCodeMirror) {
frontmatter.head.stylesheets = [
...(frontmatter.stylesheets ?? []),
`https://cdnjs.cloudflare.com/ajax/libs/codemirror/${CODE_MIRROR_VERSION}/codemirror.min.css`,
];
frontmatter.head.scripts = [
...(frontmatter.scripts ?? []),
`https://cdnjs.cloudflare.com/ajax/libs/codemirror/${CODE_MIRROR_VERSION}/codemirror.min.js`,
`https://cdnjs.cloudflare.com/ajax/libs/codemirror/${CODE_MIRROR_VERSION}/mode/javascript/javascript.min.js`,
`https://cdnjs.cloudflare.com/ajax/libs/codemirror/${CODE_MIRROR_VERSION}/mode/xml/xml.min.js`,
];
}
// frontmatter.head.modules = [
// ...(frontmatter.modules ?? []),
// '/assets/js/code-playground.min.js',
// ];
fs.writeFile(dest + '.md', matter.stringify(content, frontmatter));
} catch (e) {
console.error('Error parsing', x, e);
}
}
});
}
}
};
run(process.argv[2], process.argv[3]);