-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
81 lines (76 loc) · 2.42 KB
/
index.mjs
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
import { resolve } from 'path'
import lodash from 'lodash'
import writeFile from './lib/write-file.mjs'
import removeNull from './lib/remove-null.mjs'
import readMarkdownFiles from './lib/read-markdown-files.mjs'
const options = {
docsFolder: '/docs',
prettier: {
parser: 'markdown',
printWidth: 80,
semi: false,
proseWrap: 'always'
}
}
const [docsFolder, outPath] = process.argv.slice(2)
const folderPath = resolve(process.cwd(), docsFolder)
const writeFolder = resolve(process.cwd(), outPath)
const { docs, substitutions } = readMarkdownFiles(folderPath, options)
/*
* Loop through documents obj-array
* Folders are substituted with the weight from the containing _index.md
* Build lodash paths from "weight", ie. routes.10.routes.20
*/
const manifest = {}
for (const key in docs) {
const doc = docs[key]
const parts = doc.relativePath.split('/').slice(0, -1)
const path = []
for (let i = parts.length; i >= 0; i--) {
const part = parts.slice(0, i).join('/')
if (substitutions[part]) {
path.push(substitutions[part])
path.push('routes')
}
}
path.unshift(doc.weight, 'routes')
const setPath = path.reverse()
if (doc.name === '_index.md') {
if (doc.isSingleFile === true) {
const newPath = doc.relativePath.split('/').slice(0, -1).join('/')
writeFile(writeFolder, newPath + '.md', doc.pretty)
lodash.set(manifest, setPath.slice(0, -2), {
title: doc.fm.linkTitle ? doc.fm.linkTitle : doc.fm.title,
path: `${options.docsFolder}/${newPath}.md`
})
} else {
lodash.set(manifest, setPath.slice(0, -2), {
title: doc.fm.linkTitle ? doc.fm.linkTitle : doc.fm.title
})
if (doc.pretty?.length > 0) {
writeFile(
writeFolder,
doc.relativePath.replace('_index.md', 'README.md'),
doc.pretty
)
setPath.pop()
setPath.push(0)
lodash.set(manifest, setPath, {
title: 'Introduction',
path: `${options.docsFolder}/${doc.relativePath.replace(
'_index.md',
'README.md'
)}`
})
}
}
} else {
writeFile(writeFolder, doc.relativePath, doc.pretty)
lodash.set(manifest, setPath, {
title: doc.fm.linkTitle ? doc.fm.linkTitle : doc.fm.title,
path: `${options.docsFolder}/${doc.relativePath}`
})
}
}
const cleanManifest = removeNull(manifest)
writeFile(writeFolder, 'manifest.json', cleanManifest)