-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-sidebar.js
187 lines (141 loc) · 4.46 KB
/
generate-sidebar.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const fs = require('fs')
const path = require('path')
const nanoMd = require('nano-markdown')
const watch = require('watch')
const PATH_TO_IGNORE = ['_sidebar', 'node_modules', '.git', '.history', '.vscode']
const TOP_LEVEL = __dirname
/**
*
* File Object
*
*/
function File (filePath, level = 0) {
this.level = level
this.filePath = filePath
this.infos = path.parse(this.filePath)
this.datas = {
level: this.level
}
this.parse = () => {
this.setRoute()
this.setName()
if (this.isDirectory() && this.hasOtherThanIndexChildren()) {
this.datas.children = parseDirectory(this.filePath, this.level + 1)
}
}
this.setRoute = () => {
if (this.isDirectory() && !this.hasIndexChildren()) return
this.route = path.relative(TOP_LEVEL, this.filePath)
this.route = this.route.replace(/README\.md/i, '')
this.route = this.route.replace(/\.md/i, '/')
// home fix
if (this.route.length === 0) this.route = '/'
// Ensure all routes end with /
if (this.isDirectory() && !this.route.endsWith('/'))
this.route += '/'
if (!this.isDirectory() && !this.isReadmeFile() && this.route.endsWith('/'))
this.route = this.route.slice(0, -1)
this.datas.route = this.route
}
this.setNameFromFileName = () => {
if (this.isHome()) {
this.name = 'Accueil'
} else {
this.name = this.infos.name
this.name = this.name.replace('.md', '')
this.name = this.name.charAt(0).toUpperCase() + this.name.slice(1)
}
}
this.setNameFromH1 = () => {
let fileToRead = null
if (!this.isDirectory()) fileToRead = this.filePath
if (this.isDirectory() && this.hasIndexChildren()) fileToRead = this.getIndexChildren()
if (!fileToRead) return
let fileContent = fs.readFileSync(fileToRead, 'utf8')
let fileContentMd = nanoMd(fileContent)
let regex = new RegExp(/<h1>(.*?)<\/h1>/)
let firstH1 = regex.exec(fileContentMd)
if (firstH1) this.name = firstH1[1]
}
this.setName = () => {
this.setNameFromH1()
if (!this.name) this.setNameFromFileName()
this.datas.name = this.name
}
this.isDirectory = () => {
return fs.statSync(this.filePath).isDirectory()
}
this.isMdFile = () => {
return this.infos.ext === '.md'
}
this.isReadmeFile = () => {
return /readme/i.test(this.infos.name)
}
this.isIgnored = () => {
return (PATH_TO_IGNORE.indexOf(this.infos.name) > -1) || (!this.isMdFile() && !this.isDirectory()) || (this.level > 0 && this.isReadmeFile()) || (this.isDirectory() && !this.hasChildren())
}
this.isHome = () => {
return this.route === '/'
}
this.hasIndexChildren = () => {
return fs.readdirSync(this.filePath).some(i => /readme\.md/i.test(i))
}
this.getIndexChildren = () => {
let index = fs.readdirSync(this.filePath).find(i => /readme\.md/i.test(i))
if (index) index = path.resolve(this.filePath, index)
return index
}
this.hasOtherThanIndexChildren = () => {
return fs.readdirSync(this.filePath).some(i => !/readme\.md/i.test(i))
}
this.hasChildren = () => {
return fs.readdirSync(this.filePath).length > 0
}
this.getDatas = () => {
return this.datas
}
}
const parseDirectory = (dirPath, level = 0) => {
return fs.readdirSync(dirPath).reduce((acc, fileName) => {
const filePath = path.join(dirPath, fileName)
const file = new File(filePath, level)
if (file.isIgnored()) return acc
file.parse()
acc.push(file.getDatas())
return acc
}, [])
}
// Create text to insert
const createText = (a, txt = '') => {
return a.reduce((acc, curr) => {
let text = (curr.route) ? `- [${curr.name}](${curr.route})` : `- ${curr.name}`
acc += ' '.repeat(curr.level) + text + '\n'
if (curr.children) {
acc += createText(curr.children)
}
return acc
}, txt)
}
const execute = () => {
let directoryStructure = parseDirectory(path.join(__dirname))
let text = createText(directoryStructure)
fs.writeFileSync(path.join(__dirname, '_sidebar.md'), text)
}
watch.watchTree(__dirname, {
filter: (file) => {
return (fs.statSync(file).isDirectory() && PATH_TO_IGNORE.indexOf(file) === -1) || (path.extname(file) === '.md' && path.basename(file, '.md') !== '_sidebar')
}
}, (f, curr, prev) => {
if (typeof f == 'object' && prev === null && curr === null) {
} else if (prev === null) {
// file creation
execute()
} else if (curr.nlink === 0) {
// file deletion
execute()
} else {
// file change
execute()
}
})
execute()