Repository navigation
Expand file tree
/
Copy pathconvert-markdown-for-docs.ts
More file actions
278 lines (243 loc) · 9.56 KB
/
Copy pathconvert-markdown-for-docs.ts
File metadata and controls
278 lines (243 loc) · 9.56 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import { readFile } from 'fs/promises'
import path from 'path'
import { fromMarkdown } from 'mdast-util-from-markdown'
import { toMarkdown } from 'mdast-util-to-markdown'
import { visitParents } from 'unist-util-visit-parents'
import { visit, SKIP } from 'unist-util-visit'
import { remove } from 'unist-util-remove'
import { languageKeys } from '@/languages/lib/languages-server'
import { MARKDOWN_OPTIONS } from '../../content-linter/lib/helpers/unified-formatter-options'
interface Config {
targetDirectory: string
removeKeywords: string[]
}
interface FrontmatterDefaults {
[key: string]: string
}
interface Frontmatter {
title: string
intro?: string
[key: string]: string | undefined
}
interface ConversionResult {
content: string
data: Frontmatter
}
// The conversion mutates the mdast tree as loosely typed node bags,
// changing node.type, value, url and so on.
// Model only the fields we touch.
interface MdNode {
type: string
value: string
depth: number
lang?: string
meta?: string
url: string
title?: string
children: MdNode[]
}
const config: Config = JSON.parse(
await readFile(path.join('src/codeql-cli/lib/config.json'), 'utf-8'),
)
const { targetDirectory, removeKeywords } = config
const RELATIVE_LINK_PATH = targetDirectory.replace('content', '')
const LAST_PRIMARY_HEADING = 'Primary options'
const HEADING_BEGIN = '::: {.option}\n'
const END_SECTION = '\n:::'
const PROGRAM_SECTION = '::: {.program}\n'
export async function convertContentToDocs(
content: string,
frontmatterDefaults: FrontmatterDefaults = {},
currentFileName = '',
): Promise<ConversionResult> {
const ast = fromMarkdown(content)
let depth = 0
let secondaryOptions = false
const frontmatter: Frontmatter = { title: '', ...frontmatterDefaults }
const akaMsLinkMatches: MdNode[] = []
visit(ast, 'heading', (rawNode) => {
const node = rawNode as unknown as MdNode
if (node.depth === 1) {
frontmatter.title = node.children[0].value
}
// Some headings end with markup like {#options-to-configure-the-package-manager.}
if (node.children[0].value.includes('{#')) {
node.children[0].value = node.children[0].value.split('{#')[0].trim()
}
// Headings after Primary options sit one level too deep in source rst, so shift them up.
if (secondaryOptions) {
node.depth = Math.max(1, Math.min(6, node.depth - 1))
}
// Capture depth after secondary options shift changes node.depth.
depth = node.depth
if (node.children[0].value === LAST_PRIMARY_HEADING && node.children[0].type === 'text') {
secondaryOptions = true
}
})
let currentNodeIsDescription = false
visit(ast, (rawNode) => {
const node = rawNode as unknown as MdNode
// A bare return is CONTINUE. Returning false would mean EXIT,
// which stops the whole walk on the root node.
if (node.type !== 'heading' && node.type !== 'paragraph') return
// The first paragraph after Description becomes intro frontmatter.
if (node.children[0]?.value === 'Description' && node.children[0]?.type === 'text') {
currentNodeIsDescription = true
}
if (currentNodeIsDescription && node.type === 'paragraph') {
frontmatter.intro = node.children[0]?.value
currentNodeIsDescription = false
return SKIP
}
})
const matchNodeTypes = ['text', 'code', 'link']
visitParents(
ast,
(rawNode) => {
const node = rawNode as unknown as MdNode
return Boolean(node && matchNodeTypes.includes(node.type))
},
(rawNode, rawAncestors) => {
const node = rawNode as unknown as MdNode
const ancestors = rawAncestors as unknown as MdNode[]
if (node.type === 'code' && node.value.startsWith(`codeql ${frontmatter.title}`)) {
node.lang = 'shell'
node.meta = 'copy'
}
// Secondary labels "Output format options." lack depth; depth+1 nests under last heading.
if (node.type === 'text' && node.value && node.value.includes(HEADING_BEGIN)) {
node.value = node.value.replace(HEADING_BEGIN, '')
// Ancestors run root first, so the last one is the parent.
ancestors[ancestors.length - 1].type = 'heading'
ancestors[ancestors.length - 1].depth = Math.max(1, Math.min(6, depth + 1))
}
// Source code keywords like [Plumbing] do not belong in docs output.
if (node.type === 'text' && node.value) {
for (const keyword of removeKeywords) {
if (node.value.includes(keyword)) {
node.value = node.value.replace(keyword, '').trim()
}
}
}
// Level 2 command headings start with - or <, so render them as inline code.
if (
node.type === 'text' &&
ancestors[ancestors.length - 1].type === 'heading' &&
(node.value.startsWith('-') || node.value.startsWith('<'))
) {
node.type = 'inlineCode'
}
// Pandoc adds a marker at the end of each options section. Remove it.
if (node.type === 'text' && node.value && node.value.includes(END_SECTION)) {
node.value = node.value.replace(END_SECTION, '')
}
// Pandoc emits "codeql test run<test-run>" as inline code plus role marker; convert to link.
if (node.type === 'text' && node.value.includes('{.interpreted-text')) {
const paragraph = ancestors[ancestors.length - 1].children
const docRoleTagChild = paragraph.findIndex(
(child: MdNode) => child.value && child.value.includes('{.interpreted-text'),
)
const link = paragraph[docRoleTagChild - 1]
if (link.type === 'link') {
return
}
if (link.type !== 'inlineCode') {
throw new Error(
'Unexpected node type. The node before a text node with {.interpreted-text role="doc"} should be an inline code or link node.',
)
}
// Titles and link paths sometimes contain newlines.
const linkText = link.value.split('<')[0].replace(/\n/g, ' ').trim()
const linkPath = link.value.split('<')[1].split('>')[0].replace(/'\n/g, '').trim()
node.value = node.value.replace(/\n/g, ' ').replace('{.interpreted-text role="doc"}', '')
// Links to the file being converted would be circular.
const currentFileBaseName = currentFileName.replace('.md', '')
if (currentFileBaseName && linkPath === currentFileBaseName) {
link.type = 'text'
link.value = linkText
} else {
link.type = 'link'
link.url = `${RELATIVE_LINK_PATH}/${linkPath}`
link.children = [{ type: 'text', value: linkText }] as unknown as MdNode[]
delete (link as { value?: string }).value
}
}
// Resolve aka.ms redirects after the tree walk, because visit callbacks cannot await.
if (node.type === 'link' && node.url.includes('aka.ms')) {
akaMsLinkMatches.push(node)
}
// Render https://containers.GHEHOSTNAME example links as inline code so the link checker skips them.
if (node.type === 'link' && node.url.startsWith('https://containers')) {
// Strip the double quotes from the nodes either side.
const nodeBefore = ancestors[ancestors.length - 1].children[0]
const nodeAfter = ancestors[ancestors.length - 1].children[2]
if (nodeBefore.value && nodeBefore.value.endsWith('"')) {
nodeBefore.value = nodeBefore.value.slice(0, -1)
}
if (nodeAfter.value && nodeAfter.value.startsWith('"')) {
nodeAfter.value = nodeAfter.value.slice(1)
}
const inlineCode = node as {
type: string
value: string
url?: string
title?: string
children?: MdNode[]
}
inlineCode.type = 'inlineCode'
inlineCode.value = node.url
inlineCode.title = undefined
inlineCode.url = undefined
inlineCode.children = undefined
}
},
)
// aka.ms redirects supply the docs.github.com relative path.
await Promise.all(
akaMsLinkMatches.map(async (node: MdNode) => {
const url = await getRedirect(node.url)
// Existing Markdown links only need AUTOTITLE text and the resolved URL.
if (node.children[0]) {
node.children[0].value = 'AUTOTITLE'
}
node.url = url
}),
)
remove(ast, (rawNode) => {
const node = rawNode as unknown as MdNode
return Boolean(node.value && node.value.startsWith(PROGRAM_SECTION))
})
// Level 1 headings become the frontmatter title, so drop them from the body.
remove(ast, (rawNode) => {
const node = rawNode as unknown as MdNode
return node.type === 'heading' && node.depth === 1
})
return {
content: toMarkdown(ast, MARKDOWN_OPTIONS as Parameters<typeof toMarkdown>[1]),
data: frontmatter,
}
}
async function getRedirect(url: string): Promise<string> {
let response: Response
try {
response = await fetch(url, { redirect: 'manual' })
if (!response.ok && response.status !== 301 && response.status !== 302) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`)
}
} catch (error) {
console.error(error)
const errorMsg = `Failed to get redirect for ${url} when converting aka.ms links to docs.github.com links.`
throw new Error(errorMsg)
}
const redirectLocation = response.headers.get('location')
if (!redirectLocation) {
throw new Error(`No redirect location found for ${url}`)
}
const redirect = new URL(redirectLocation).pathname
// Some aka.ms links redirect to a URL with a language prefix such as /en.
const parts = redirect.split('/')
if (parts.length > 1 && languageKeys.includes(parts[1])) {
parts.splice(1, 1)
}
return parts.join('/')
}