-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
169 lines (146 loc) · 4.21 KB
/
index.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
import { writeFile } from 'node:fs/promises'
import { dirname, resolve, sep } from 'node:path'
import { fileURLToPath } from 'node:url'
import {
Application,
Converter,
PageEvent,
ReflectionKind,
TSConfigReader
} from 'typedoc'
const __dirname = dirname(fileURLToPath(import.meta.url))
const objectToFrontmatter = (object = {}) =>
Object.entries(object)
.map(([key, value]) => `${key}: ${value}`)
.join('\n')
const onRendererPageEnd = frontmatterObject => event => {
if (!event.contents) {
return
} else if (/README\.md$/.test(event.url)) {
event.preventDefault()
return
}
let prependix = `---
title: '${event.model.name}'
${objectToFrontmatter(frontmatterObject)}
---
`
event.contents = prependix + event.contents
}
const buildNavigationFromProjectReflection = (baseUrl = '', project) => {
let baseUrlWithoutTrailingSlash = baseUrl.replace(/\/$/gm, '')
let result = { type: 'flat' }
let isGroupOfModules = group => group.title === 'Modules'
let reflectionToNavItem = reflection => {
return {
title: reflection.name,
url: `${baseUrlWithoutTrailingSlash}/${reflection.url}`.replace(
/\.md$/,
''
)
}
}
let modulesGroupToNavigationGroup = module => ({
items: module.groups.flatMap(group =>
group.children.map(reflectionToNavItem)
),
name: module.name
})
let navFromReflectionGroups = (groups, nav = {}) => {
groups.forEach(group => {
if (isGroupOfModules(group)) {
nav.type = 'modular'
nav.modules = group.children.map(modulesGroupToNavigationGroup)
} else {
nav.items = nav?.items?.length ? nav.items : []
nav.items = nav.items.concat(
group.children.flatMap(reflectionToNavItem)
)
}
})
return nav
}
return navFromReflectionGroups(project.groups, result)
}
const onDeclaration =
(entryPoints = []) =>
(context, reflection) => {
if (reflection.kind === ReflectionKind.Module) {
let matchingEntryPoint = entryPoints.find(
entryPoint => entryPoint.path === reflection.sources[0].fullFileName
)
reflection.name = matchingEntryPoint?.name ?? reflection.name
}
}
const typedocConfig = {
excludeExternals: true,
excludeInternal: true,
excludePrivate: true,
excludeProtected: true,
githubPages: false
}
const markdownPluginConfig = {
hideBreadcrumbs: true,
hideInPageTOC: true,
hidePageHeader: true,
hidePageTitle: true
}
const removeTrailingSlash = (pathString = '') =>
pathString.endsWith(sep)
? pathString.slice(0, pathString.length - 1)
: pathString
export const initAstroTypedoc = async ({ baseUrl = '/docs/', entryPoints }) => {
// Hack to make sure entrypoints will be loaded
await writeFile(
resolve(__dirname, './tsconfig.generic.json'),
JSON.stringify({
compilerOptions: {
baseUrl: '.',
paths: entryPoints.reduce((paths, { name, path }) => {
if (name) {
paths[name] = [path]
}
return paths
}, {})
},
include: entryPoints.map(e => e.path)
})
)
let app = await Application.bootstrapWithPlugins({
...typedocConfig,
...markdownPluginConfig,
basePath: baseUrl,
entryPoints: entryPoints.map(e => e.path),
plugin: ['typedoc-plugin-markdown', resolve(__dirname, './theme.js')],
readme: 'none',
theme: 'custom-markdown-theme',
tsconfig: resolve(__dirname, './tsconfig.generic.json')
})
app.options.addReader(new TSConfigReader())
app.converter.on(
Converter.EVENT_CREATE_DECLARATION,
onDeclaration(entryPoints)
)
let getReflections = async () => await app.convert()
let generateDocs = async ({
frontmatter,
outputFolder = 'src/pages/docs',
project
}) => {
app.renderer.on(PageEvent.END, onRendererPageEnd(frontmatter))
await app.generateDocs(project, outputFolder)
}
let generateNavigationJSON = async (project, outputFolder) => {
let navigation = buildNavigationFromProjectReflection(baseUrl, project)
await writeFile(
`${removeTrailingSlash(outputFolder)}/nav.json`,
JSON.stringify(navigation)
)
}
return {
generateDocs,
generateNavigationJSON,
getReflections
}
}
export default initAstroTypedoc