UNPKG

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