-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathnav.js
81 lines (64 loc) · 2.66 KB
/
nav.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
const glob = require('glob');
const path = require('path');
const fs = require('fs');
const fm = require('front-matter');
const yaml = require('js-yaml');
const ignore = [ 'catalog/**', '_*/**', '*.md' ];
const capitalize = (str) => str.charAt(0).toLocaleUpperCase() + str.slice(1);
const getTitle = (str) => str.split('-').map(capitalize).join(' ');
const getSection = (accum, paths) => {
const slug = paths.join('/');
if (accum[slug]) return accum[slug];
const section_title = getTitle(paths[paths.length - 1]);
const section = [];
const subsection = paths.length > 1
? { section_title, slug, section }
: { section_title, section };
accum[slug] = subsection;
if (paths.length > 1) getSection(accum, paths.slice(0, -1)).section.push(subsection);
return subsection;
};
const files = glob.sync('**/*.md', { ignore, cwd: path.resolve(__dirname, '../src') });
const sections = files.reduce((accum, file) => {
const paths = file.split('/');
// Not a valid path or some deep-nested directory structure we don't support
if (paths.length < 2 || paths.length > 3) {
console.log(`skipping ${file}`);
return accum;
}
// read in the .md file, parse the frontmatter attributes
const f = fm(fs.readFileSync(path.resolve(__dirname, '../src', file), 'utf8'));
// if file has hidden attribute don't add to nav
if (!f.attributes.hidden) {
const title = f.attributes.title || getTitle(paths[paths.length - 1].slice(0, -3));
const s = getSection(accum, paths.slice(0, -1));
if (paths[paths.length - 1] === 'index.md') {
s.section.unshift({ path: `/${paths.slice(0, -1).join('/')}`, title });
} else {
s.section.push({ path: `/${paths.join('/').slice(0, -3)}`, title });
}
}
return accum;
}, {});
const mainSections = {};
const legalSections = {};
const apiSections = {};
const partnerSections = {};
Object.keys(sections).filter(key => !key.includes('/')).forEach((key) => {
const value = sections[key];
switch (key) {
case 'legal': legalSections[key] = value; break;
case 'config-api': apiSections[key] = value; break;
case 'partners': partnerSections[key] = value; break;
default: mainSections[key] = value; break;
}
});
[ ['main', mainSections],
['legal', legalSections],
['config-api', apiSections],
['partners', partnerSections]
].forEach(([ name, sections ]) => {
const options = { noArrayIndent: true };
const output = yaml.safeDump({ sections: Object.values(sections) }, options);
fs.writeFileSync(path.resolve(__dirname, `../src/_data/sidenav/_auto/${name}.yml`), output);
});