-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateTable.js
117 lines (84 loc) · 2.57 KB
/
generateTable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const fs = require('fs');
const path = require('path');
const current_dir = path.basename(__dirname);
var arrPages = [];
function readMd(dir, file) {
let text = fs.readFileSync(dir + file, 'utf8', function (err, data) {
if (err) {
return console.log('Unable to scan file: ' + err);
}
return data
});
let image = '';
if (!text.includes('![Computer](../images/prints/computer.png)')) {
image =
`- ![Computer](../images/prints/computer.png)
- ![Print](../images/prints/${ file.replace('.md', '').replace(' ', '-').toLowerCase() }.png)
`;
}
let newText = image + text.replace(/(\r?\n\*\*\*\r?\n)/, '').replace(/(\-.\-.\-\r\n\r\n\r\n)/, '') + '\r\n***\r\n';
fs.writeFile(dir + file, newText, function (err) {
if (err) return console.log(err);
});
text = text.toString();
let arrH2 = [];
let h1 = /[^#]#\s(.*?)[\n\r].*/g.exec(text) || [];
var regex = /[^#]##\s(.*?)[\n\r].*/g;
var myArray;
while ((myArray = regex.exec(text)) !== null) {
var msg = 'Found ' + myArray[1] + '. ';
msg += 'Next match starts at ' + regex.lastIndex;
arrH2.push(myArray[1])
}
let arrPage = []
arrPage.push(h1[1])
arrPage.push(arrH2)
arrPages.push(arrPage)
}
fs.readdir(`../${current_dir}/docs/`, function (err, files) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
files.sort();
files.forEach(async function (file) {
let isDir = fs.statSync(`../${current_dir}/docs/${ file }`).isDirectory();
if (isDir) {
fs.readdir(`../${current_dir}/docs/${ file }/`, function (err, files2) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
files2.sort();
files2.forEach(async function (file2) {
if (file2.includes('md')) {
readMd(`../${current_dir}/docs/${ file }/`, file2);
}
})
})
}
if (file.includes('md') && !file.includes('TABLE') && !file.includes('index')) {
readMd(`../${current_dir}/docs/`, file);
}
});
});
setTimeout(() => {
let md = `
<!-- _class: table-of-contents -->
# Orientações de cadastro
![Logo Vnda](../images/prints/vnda.svg)
## [Tabela de conteúdos](#1)
\n`
arrPages.forEach((pg, index) => {
md += `- ### [${ pg[0] }](#${ index + 2 }) \r`;
for (var i = 0; i < pg[1].length; i++) {
md += ` - [${ i + 1 } ${ pg[1][i] }](#${ index + 2 }) \r`;
}
md += `\r\r`;
})
md += `
\r
***
\r`;
fs.writeFile(`../${current_dir}/docs/00-TABLE.md`, md, function (err) {
if (err) return console.log(err);
});
}, 2000)