-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-readme.js
More file actions
51 lines (40 loc) · 1.4 KB
/
make-readme.js
File metadata and controls
51 lines (40 loc) · 1.4 KB
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
const fs = require('fs')
const path = require('path')
async function getMarkdownFiles (dirPath = '.') {
const dir = await fs.promises.opendir(dirPath)
let fileEnts = []
for await (const dirent of dir) {
if (dirent.isDirectory() && !dirent.name.startsWith('.')) {
const filePaths = await getMarkdownFiles(path.join(dir.path, dirent.name))
fileEnts.push(filePaths)
} else if (dirent.isFile() && path.extname(dirent.name) === '.md' && dirent.name !== 'README.md') {
fileEnts.push(path.join(dir.path, dirent.name))
}
}
return fileEnts
}
// path = some/slash/delimited/path
function genSubHeadings (path) {
return path.split('/').map((heading, i) => `#${'#'.repeat(i + 1)} ${heading}`)
}
async function getTitle (path) {
const title = (await fs.promises.readFile(path)).toString().split('\n')[0]
return title.slice(2, title.length)
}
async function makeReadme () {
const markdownFiles = (await getMarkdownFiles('.')).flat(Infinity).sort()
let readme = ['# TIL']
for (const dirent of markdownFiles) {
const headings = genSubHeadings(path.parse(dirent).dir).sort()
for (const heading of headings) {
if (!readme.includes(heading)) {
readme.push(heading)
}
}
const title = await getTitle(dirent)
const link = `[${title}](${dirent})`
readme.push(link)
}
await fs.promises.writeFile('README.md', readme.join('\n\n'))
}
makeReadme()