-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnavigation2menu.js
executable file
·63 lines (51 loc) · 1.8 KB
/
navigation2menu.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
#!/usr/bin/node
// Work on POSIX and Windows
const fs = require('fs');
const stdinBuffer = fs.readFileSync(0); // STDIN_FILENO = 0
const markdown = stdinBuffer.toString();
let weight = 10;
const parentStack = [];
const menu = [];
let lastContent = 'First line';
for (const line of markdown.split('\n')) {
if (!line.match(/^ *\* /)) {
continue;
}
const indentPart = line.replace(/(^ *\* ).*/, '$1');
const markdownLink = line.substring(indentPart.length)
.replace(/#[^)]*\)$/, ')');
const matched = markdownLink.match(/\[([^\]]+)\]\(([^)]+)\)/);
if (!matched) {
console.warn(`Warning: navigation.md menu has "${markdownLink}" without url near: "${lastContent}"`);
continue;
}
const [_, name, pageRef] = matched;
const level = (indentPart.length - 2)/4;
while (parentStack.length > level) {
parentStack.pop();
}
let identifier = pageRef;
if (pageRef.startsWith('http://') || pageRef.startsWith('https://') || fs.existsSync('./content/' + pageRef)) {
if (pageRef.indexOf('//') === -1) {
const md = fs.readFileSync('./content/' + pageRef).toString().split('\n');
for (const line of md) {
if (line.startsWith('id: ')) {
identifier = line.substring('id: '.length).replace(/['"]/g, '').trim() || identifier;
}
}
}
menu.push({
identifier,
name,
pageRef,
parent: parentStack[parentStack.length - 1],
weight
});
} else {
console.warn(`Warning: navigation.md menu has "${markdownLink}" without file: "${pageRef}"`);
}
weight += 10;
parentStack.push(identifier);
lastContent = markdownLink;
}
console.log(JSON.stringify({ main: menu }, null, 4));