-
Notifications
You must be signed in to change notification settings - Fork 0
/
combine.js
99 lines (92 loc) · 3.01 KB
/
combine.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
require('./serve.js')
var fs = require('fs');
// The root directory
const root = './chatmd'
// Main file
const mainmd = 'main.md'
// Destination file
const destination = './docs/chat.md'
const colorStart = '\x1b[36m\x1b[1m\x1b[44m';
const colorEnd = '\x1b[0m';
console.log (colorStart, '😼 ChatMD', colorEnd)
console.log (colorStart, ' Un chatbot personnalisé à partir de fichiers en Markdown', colorEnd, '\n')
function nextFile(files, directory, options) {
const file = files.shift();
if (!file) {
if (directory === root) {
console.log(colorStart,
options.nb + ' files (' + options.nbdir + ' directories) ',
colorEnd
)
}
//finish
return
}
const isDir = fs.lstatSync(directory + '/' + file).isDirectory()
if (isDir) {
options.nbdir++;
// Process dir
const files2 = fs.readdirSync(directory + '/' + file);
nextFile(files2, directory + '/' + file, options)
} else {
options.nb++;
// Process file
let content = fs.readFileSync(directory + '/' + file, { encoding: 'utf8' });
// Process local ref [](./ref) or [](../ref)
const locals = content.matchAll(/\]\(\.(\.)?\/[^)]*(\.md)\)/g);
for (const match of locals) {
// File name without '.md' and '_'
let st = match[0].replace(/\]\(\.(\.)?\/(\.\.\/)?/,'').replace(/\.md\)$/,'').replace(/\)$/,'').replace(/_/g,' ')
// Path from the root
if (/\]\(\.\//.test(match[0])) {
st = (directory + '/').replace(root+'/', '') + st;
}
// Replace
content = content.replace(match[0], ']('+st+')')
}
// Process for anchor links
const anchors = content.matchAll(/\]\(#\.(\.)?\/[^)]*(\.md)\)/g);
for (const match of anchors) {
// File name without '.md' and '_'
let st = match[0].replace(/\]\(#\.(\.)?\/(\.\.\/)?/,'').replace(/\.md\)$/,'').replace(/\)$/,'').replace(/_/g,' ')
// Path from the root
if (/\]\(#\.\//.test(match[0])) {
st = (directory + '/').replace(root+'/', '') + st;
}
// Replace
content = content.replace(match[0], '](#'+st+')')
}
// Path for images ../img
content = content.replace(/(\.\.\/){1,}docs\/img\//g,'./img/');
// Path from the root
if (directory !== root) {
const name = file.replace(/_/g,' ').replace(/\.md$/,'');
content = '## ' + (directory + '/' + name).replace(root + '/', '') + '\n' + content;
}
// Write file
fs.appendFileSync(destination, content + '\n\n')
// console.log(directory + '/' + file)
}
nextFile(files, directory, options);
}
let delayTout
function combine(e) {
clearTimeout(delayTout)
delayTout = setTimeout(() => {
fs.copy
let files = fs.readdirSync(root);
// Place main on top
const index = files.indexOf(mainmd);
if (index > -1) {
files.splice(index, 1);
}
files.unshift(mainmd)
// Clear file
fs.writeFileSync(destination, '');
nextFile(files, root, { nb:0, nbdir:0 })
}, e ? 100 : 0)
}
// Start
combine();
// Watch updates
fs.watch(root, { recursive: true }, combine);