-
-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathindex.js
More file actions
114 lines (98 loc) · 4.14 KB
/
index.js
File metadata and controls
114 lines (98 loc) · 4.14 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
const FenceMarkmap = require("./fence.js")
const TOCMarkmap = require("./toc.js")
class MarkmapPlugin extends BasePlugin {
beforeProcess = () => {
this.Lib = {}
this.tocMarkmap = this.config.ENABLE_TOC_MARKMAP ? new TOCMarkmap(this) : null
this.fenceMarkmap = this.config.ENABLE_FENCE_MARKMAP ? new FenceMarkmap(this) : null
}
styleTemplate = () => true
html = () => this.tocMarkmap?.html()
hotkey = () => [this.tocMarkmap, this.fenceMarkmap].filter(Boolean).flatMap(p => p.hotkey())
init = () => {
this.staticActions = this.i18n.fillActions([
{ act_value: "draw_fence_outline", act_hotkey: this.config.FENCE_HOTKEY, act_hidden: !this.fenceMarkmap },
{ act_value: "draw_fence_template", act_hidden: !this.fenceMarkmap },
{ act_value: "toggle_toc", act_hotkey: this.config.TOC_HOTKEY, act_hidden: !this.tocMarkmap }
])
}
process = () => {
if (this.tocMarkmap) {
this.tocMarkmap.init()
this.tocMarkmap.process()
}
if (this.fenceMarkmap) {
this.fenceMarkmap.process()
}
}
call = async action => {
if (action === "toggle_toc") {
if (this.tocMarkmap) {
await this.tocMarkmap.callback(action)
}
} else if (action === "draw_fence_template" || action === "draw_fence_outline") {
if (this.fenceMarkmap) {
await this.fenceMarkmap.callback(action)
}
}
}
onButtonClick = () => this.tocMarkmap?.callback()
getToc = (
fixSkip = this.config.FIX_SKIPPED_LEVEL_HEADERS,
removeStyles = this.config.REMOVE_HEADER_STYLES,
) => {
const tree = this.utils.getTocTree(removeStyles)
const getHeaders = (node, ret, indent) => {
const head = "#".repeat(fixSkip ? indent : node.depth)
ret.push(`${head} ${node.text}`)
for (const child of node.children) {
getHeaders(child, ret, indent + 1)
}
return ret
}
return getHeaders(tree, [], 0).slice(1).join("\n")
}
assignOptions = (update, origin) => {
const options = this.Lib.deriveOptions({ ...origin, ...update })
// `toggleRecursively` is deleted after calling deriveOptions
options.toggleRecursively = update.toggleRecursively
return options
}
lazyLoad = async () => {
if (this.Lib.transformerVersions) return
const { Transformer, transformerVersions, markmap } = require("./resource/markmap.min.js")
const transformer = new Transformer()
Object.assign(this.Lib, markmap, { transformer, Transformer, transformerVersions })
const { styles, scripts } = transformer.getAssets()
this.localizeResources(styles, scripts)
await markmap.loadCSS(styles)
await markmap.loadJS(scripts, { getMarkmap: () => markmap })
}
localizeResources = (styles = [], scripts = []) => {
const katexBase = "./plugin/global/core/lib/katex"
const pluginBase = "./plugin/markmap/resource/"
const localResources = {
"katex.min.js": this.utils.joinPluginPath(katexBase, "katex.js"),
"katex.min.css": this.utils.joinPluginPath(katexBase, "katex.min.css"),
"default.min.css": this.utils.joinPluginPath(pluginBase, "default.min.css"),
"webfontloader.js": this.utils.joinPluginPath(pluginBase, "webfontloader.js"),
}
const localize = (items, expectedType, urlProp) => {
for (const item of items) {
if (item?.type === expectedType && typeof item?.data?.[urlProp] === "string") {
const url = item.data[urlProp]
const filename = url.slice(url.lastIndexOf("/") + 1)
const localResource = localResources[filename]
if (localResource) {
item.data[urlProp] = localResource
}
}
}
}
localize(styles, "stylesheet", "href")
localize(scripts, "script", "src")
}
}
module.exports = {
plugin: MarkmapPlugin
}