UNPKG

2.19 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
29 for(var k in data) {
30
31 }
32
33 data.forEach(item => {
34 let outFile = '';
35 item.files.forEach(f => {
36 outFile += `require('${f}'),\n`
37 })
38
39 let out = `module.exports = {${outFile}}`
40
41 fs.writeFileSync(__dirname + `/tmp/__${item.page}.js`, out);
42 });
43}
44
45
46module.exports = function walkMD(config, callback) {
47 const mdData = {};
48
49 const walker = walk.walk(config.root);
50 walker.on('file', function (root, fileStats, next) {
51 const name = fileStats.name;
52 const ext = path.extname(name);
53 const basename = path.basename(name, config.ext);
54
55 const file = path.resolve('', root + '/' + name);
56 // console.log(file);
57 if (ext === config.ext) {
58 const input = fs.readFileSync(file, 'utf-8');
59 const yaml = yamlFront.loadFront(input, 'content');
60 // console.log(yaml);
61 const page = yaml.page;
62 const rank = yaml.rank;
63 const category = yaml.category;
64
65 if (!mdData[category]) mdData[category] = [];
66 parseData(mdData[category], page, rank, file);
67 // if (!mdData[title]) mdData[title] = {};
68 // if (!mdData[title].files) mdData[title].files = [];
69 // mdData[title].files[rank] = file;
70 }
71
72 next();
73 });
74
75
76 walker.on('end', () => {
77 console.log(mdData);
78 writeFiles(mdData);
79 // fs.writeFileSync(__dirname + '/web/__md__.json', JSON.stringify(mdData));
80 if (callback) callback(mdData);
81 });
82
83 walker.on('error', (e) => {
84 console.log(e);
85 });
86}
\No newline at end of file