-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme.js
43 lines (36 loc) · 1.27 KB
/
theme.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
import { slug } from 'github-slugger'
import path from 'node:path'
import {
MarkdownTheme,
MarkdownThemeRenderContext
} from 'typedoc-plugin-markdown'
const externalLinkRegex = /^\w+:/
export function load(app) {
app.renderer.defineTheme('custom-markdown-theme', CustomMarkdownTheme)
}
export class CustomMarkdownTheme extends MarkdownTheme {
getRenderContext(pageEvent) {
return new CustomMarkdownThemeContext(pageEvent, this.application.options)
}
}
class CustomMarkdownThemeContext extends MarkdownThemeRenderContext {
relativeURL = url => {
if (!url) {
return null
} else if (externalLinkRegex.test(url)) {
return url
}
let basePath = this.options.getValue('basePath')
let basePathParsed = path.parse(basePath)
let baseUrl = basePath.replace(basePathParsed.root, '/')
let filePathParsed = path.parse(url)
let directory = filePathParsed.dir.split(path.sep).join('/')
let [, anchor] = filePathParsed.base.split('#')
let constructedUrl = typeof baseUrl === 'string' ? baseUrl : ''
constructedUrl += '/'
constructedUrl += directory.length > 0 ? `${directory}/` : ''
constructedUrl += filePathParsed.name
constructedUrl += anchor && anchor.length > 0 ? `#${slug(anchor)}` : ''
return constructedUrl
}
}