UNPKG

1.57 kBJavaScriptView Raw
1const fs = require('fs');
2const path = require('path');
3const walk = require('walk');
4const yamlFront = require('yaml-front-matter')
5
6
7function verifyExist(arr, page, rank) {
8 arr.every(item => {
9 if (item.page == page) {
10 item.files[rank] =
11 }
12 });
13}
14
15
16module.exports = function walkMD(config, callback) {
17 const mdData = {};
18
19 const walker = walk.walk(config.root);
20 walker.on('file', function (root, fileStats, next) {
21 const name = fileStats.name;
22 const ext = path.extname(name);
23 const basename = path.basename(name, config.ext);
24
25 const file = path.resolve('', root + '/' + name);
26 // console.log(file);
27 if (ext === config.ext) {
28 const input = fs.readFileSync(file, 'utf-8');
29 const yaml = yamlFront.loadFront(input, 'content');
30 // console.log(yaml);
31 const page = yaml.page;
32 const rank = yaml.rank;
33 const category = yaml.category;
34
35 if (!mdData[category]) mdData[category] = [];
36 verifyExist(mdData[category], page, rank, file);
37
38
39
40
41 if (!mdData[title]) mdData[title] = {};
42 if (!mdData[title].files) mdData[title].files = [];
43 mdData[title].files[rank] = file;
44 }
45
46 next();
47 });
48
49
50 walker.on('end', () => {
51 console.log(mdData);
52 fs.writeFileSync(__dirname + '/web/__md__.json', JSON.stringify(mdData));
53 if (callback) callback(mdData);
54 });
55
56 walker.on('error', (e) => {
57 console.log(e);
58 });
59}
\No newline at end of file