forked from canpolatlardanfurkan/api-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkify.ts
More file actions
62 lines (53 loc) · 1.98 KB
/
linkify.ts
File metadata and controls
62 lines (53 loc) · 1.98 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
// Processes the built site to wire up any @link tokens included in the TSDoc
// source code. It does this in two passes, the first to build up a page
// of paths -> api item id, the second to append an href to all @link tags
// the modified document is then written over the original file.
//
// For the other implementation points see the `LinkTag` geneation in
// TSDocReactEmitter.ts and the id resolution in the APIOverviewElement
// component.
//
// Usage:
//
// % yarn ts-node ./linkify.ts <htmlfile> [, <htmlfile>...]
// % yarn ts-node ./linkify.ts build/**/*.html
import * as fs from "fs"
import * as cheerio from "cheerio"
import chalk from "chalk"
const { yellow, gray, white } = chalk
const inputs = process.argv.slice(2)
const parsed = inputs.map(filepath => cheerio.load(fs.readFileSync(filepath).toString("utf-8")))
const pathById: { [id: string]: string } = {}
console.log("Updating TSDoc @link references")
parsed.forEach($ => {
$("[data-permalink-ref][data-permalink-path]").each((_, el) => {
const { permalinkRef, permalinkPath } = $(el).data()
pathById[permalinkRef] = permalinkPath
})
})
parsed.forEach(($, idx) => {
const filepath = inputs[idx]
let hasMatch = false
$("[data-link-ref]").each((_, el) => {
hasMatch = true
const $el = $(el)
const ref = $el.data("linkRef")
if (pathById[ref]) {
$el.attr("href", `${pathById[ref]}`)
} else {
$el.addClass("link-ref-missing")
const id = $el.parents("[data-tsdoc-ref]").data("tsdocRef")
console.warn(
gray(
`${yellow("Warning:")} Could not resolve reference for: ${white(ref)} in ${white(
id || "Unknown"
)} on page ${white(filepath)}`
)
)
}
})
if (hasMatch) {
console.log(gray(`Writing new @link references → ${white(filepath)}`))
fs.writeFileSync(filepath, $.html())
}
})