UNPKG

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