UNPKG

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