markmap-lib 0.15.8 → 0.15.9-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/index.iife.js +18517 -4747
- package/dist/browser/index.mjs +252 -284
- package/dist/index.js +40 -361
- package/dist/index.mjs +40 -361
- package/dist/index.no-plugins.js +185 -0
- package/dist/index.no-plugins.mjs +185 -0
- package/dist/plugins/checkbox/index.d.ts +2 -0
- package/dist/plugins/frontmatter/index.d.ts +2 -0
- package/dist/plugins/hljs/index.browser.d.ts +2 -0
- package/dist/plugins/hljs/index.d.ts +2 -0
- package/dist/plugins/katex/index.browser.d.ts +2 -0
- package/dist/plugins/katex/index.d.ts +2 -0
- package/dist/plugins/npm-url/index.d.ts +2 -0
- package/dist/plugins/prism/index.browser.d.ts +2 -0
- package/dist/plugins/prism/index.d.ts +2 -0
- package/dist/plugins/source-lines/index.d.ts +2 -0
- package/dist/plugins.js +299 -0
- package/dist/plugins.mjs +299 -0
- package/dist/transform.d.ts +5 -6
- package/dist/types.d.ts +6 -1
- package/package.json +18 -5
- package/dist/plugins/frontmatter.d.ts +0 -2
- package/dist/plugins/hljs.browser.d.ts +0 -2
- package/dist/plugins/hljs.d.ts +0 -2
- package/dist/plugins/katex.browser.d.ts +0 -2
- package/dist/plugins/katex.d.ts +0 -2
- package/dist/plugins/npm-url.d.ts +0 -2
- package/dist/plugins/prism.browser.d.ts +0 -2
- package/dist/plugins/prism.d.ts +0 -2
- /package/dist/plugins/{hljs.config.d.ts → hljs/config.d.ts} +0 -0
- /package/dist/plugins/{katex.config.d.ts → katex/config.d.ts} +0 -0
- /package/dist/plugins/{prism.config.d.ts → prism/config.d.ts} +0 -0
package/dist/index.mjs
CHANGED
|
@@ -1,228 +1,15 @@
|
|
|
1
|
+
import { UrlBuilder, wrapFunction, buildJSItem, persistCSS, persistJS } from "markmap-common";
|
|
2
|
+
import { buildTree } from "markmap-html-parser";
|
|
1
3
|
import { Remarkable } from "remarkable";
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
4
|
+
import { plugins, createTransformHooks } from "./plugins.mjs";
|
|
5
|
+
import "js-yaml";
|
|
6
|
+
import "highlight.js";
|
|
7
|
+
import "remarkable-katex";
|
|
6
8
|
const template = '<!DOCTYPE html>\n<html>\n<head>\n<meta charset="UTF-8">\n<meta name="viewport" content="width=device-width, initial-scale=1.0">\n<meta http-equiv="X-UA-Compatible" content="ie=edge">\n<title>Markmap</title>\n<style>\n* {\n margin: 0;\n padding: 0;\n}\n#mindmap {\n display: block;\n width: 100vw;\n height: 100vh;\n}\n</style>\n<!--CSS-->\n</head>\n<body>\n<svg id="mindmap"></svg>\n<!--JS-->\n</body>\n</html>\n';
|
|
7
9
|
const baseJsPaths = [
|
|
8
10
|
`d3@${"7.8.5"}/dist/d3.min.js`,
|
|
9
11
|
`markmap-view@${"0.15.8"}/dist/browser/index.js`
|
|
10
12
|
];
|
|
11
|
-
const name$3 = "katex";
|
|
12
|
-
const preloadScripts$1 = [
|
|
13
|
-
`katex@${"0.16.8"}/dist/katex.min.js`
|
|
14
|
-
].map((path) => buildJSItem(path));
|
|
15
|
-
const webfontloader = buildJSItem(
|
|
16
|
-
`webfontloader@${"1.6.28"}/webfontloader.js`
|
|
17
|
-
);
|
|
18
|
-
webfontloader.data.defer = true;
|
|
19
|
-
const styles$1 = [`katex@${"0.16.8"}/dist/katex.min.css`].map(
|
|
20
|
-
(path) => buildCSSItem(path)
|
|
21
|
-
);
|
|
22
|
-
const config$1 = {
|
|
23
|
-
versions: {
|
|
24
|
-
katex: "0.16.8",
|
|
25
|
-
webfontloader: "1.6.28"
|
|
26
|
-
},
|
|
27
|
-
preloadScripts: preloadScripts$1,
|
|
28
|
-
scripts: [
|
|
29
|
-
{
|
|
30
|
-
type: "iife",
|
|
31
|
-
data: {
|
|
32
|
-
fn: (getMarkmap) => {
|
|
33
|
-
window.WebFontConfig = {
|
|
34
|
-
custom: {
|
|
35
|
-
families: [
|
|
36
|
-
"KaTeX_AMS",
|
|
37
|
-
"KaTeX_Caligraphic:n4,n7",
|
|
38
|
-
"KaTeX_Fraktur:n4,n7",
|
|
39
|
-
"KaTeX_Main:n4,n7,i4,i7",
|
|
40
|
-
"KaTeX_Math:i4,i7",
|
|
41
|
-
"KaTeX_Script",
|
|
42
|
-
"KaTeX_SansSerif:n4,n7,i4",
|
|
43
|
-
"KaTeX_Size1",
|
|
44
|
-
"KaTeX_Size2",
|
|
45
|
-
"KaTeX_Size3",
|
|
46
|
-
"KaTeX_Size4",
|
|
47
|
-
"KaTeX_Typewriter"
|
|
48
|
-
]
|
|
49
|
-
},
|
|
50
|
-
active: () => {
|
|
51
|
-
getMarkmap().refreshHook.call();
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
},
|
|
55
|
-
getParams({ getMarkmap }) {
|
|
56
|
-
return [getMarkmap];
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
|
-
webfontloader
|
|
61
|
-
],
|
|
62
|
-
styles: styles$1
|
|
63
|
-
};
|
|
64
|
-
function createTransformHooks(transformer) {
|
|
65
|
-
return {
|
|
66
|
-
transformer,
|
|
67
|
-
parser: new Hook(),
|
|
68
|
-
beforeParse: new Hook(),
|
|
69
|
-
afterParse: new Hook(),
|
|
70
|
-
htmltag: new Hook(),
|
|
71
|
-
retransform: new Hook()
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
function definePlugin(plugin2) {
|
|
75
|
-
return plugin2;
|
|
76
|
-
}
|
|
77
|
-
const plugin$1 = definePlugin({
|
|
78
|
-
name: name$3,
|
|
79
|
-
config: config$1,
|
|
80
|
-
transform(transformHooks) {
|
|
81
|
-
var _a, _b;
|
|
82
|
-
let enableFeature = noop;
|
|
83
|
-
transformHooks.parser.tap((md) => {
|
|
84
|
-
md.use(remarkableKatex);
|
|
85
|
-
md.renderer.rules.katex = wrapFunction(
|
|
86
|
-
md.renderer.rules.katex,
|
|
87
|
-
(render, ...args) => {
|
|
88
|
-
enableFeature();
|
|
89
|
-
return render(...args);
|
|
90
|
-
}
|
|
91
|
-
);
|
|
92
|
-
});
|
|
93
|
-
transformHooks.beforeParse.tap((_, context) => {
|
|
94
|
-
enableFeature = () => {
|
|
95
|
-
context.features[name$3] = true;
|
|
96
|
-
};
|
|
97
|
-
});
|
|
98
|
-
return {
|
|
99
|
-
styles: (_a = plugin$1.config) == null ? void 0 : _a.styles,
|
|
100
|
-
scripts: (_b = plugin$1.config) == null ? void 0 : _b.scripts
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
const name$2 = "frontmatter";
|
|
105
|
-
const frontmatter = definePlugin({
|
|
106
|
-
name: name$2,
|
|
107
|
-
transform(transformHooks) {
|
|
108
|
-
transformHooks.beforeParse.tap((md, context) => {
|
|
109
|
-
const { content } = context;
|
|
110
|
-
if (!/^---\r?\n/.test(content))
|
|
111
|
-
return;
|
|
112
|
-
const match = /\n---\r?\n/.exec(content);
|
|
113
|
-
if (!match)
|
|
114
|
-
return;
|
|
115
|
-
const raw = content.slice(4, match.index);
|
|
116
|
-
let frontmatter2;
|
|
117
|
-
try {
|
|
118
|
-
frontmatter2 = yaml.load(raw);
|
|
119
|
-
if (frontmatter2 == null ? void 0 : frontmatter2.markmap) {
|
|
120
|
-
frontmatter2.markmap = normalizeMarkmapJsonOptions(
|
|
121
|
-
frontmatter2.markmap
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
} catch {
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
context.frontmatter = frontmatter2;
|
|
128
|
-
context.content = content.slice(match.index + match[0].length);
|
|
129
|
-
context.contentLineOffset = content.slice(0, match.index).split("\n").length + 1;
|
|
130
|
-
});
|
|
131
|
-
return {};
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
function normalizeMarkmapJsonOptions(options) {
|
|
135
|
-
if (!options)
|
|
136
|
-
return;
|
|
137
|
-
["color", "extraJs", "extraCss"].forEach((key) => {
|
|
138
|
-
if (options[key] != null)
|
|
139
|
-
options[key] = normalizeStringArray(options[key]);
|
|
140
|
-
});
|
|
141
|
-
["duration", "maxWidth", "initialExpandLevel"].forEach((key) => {
|
|
142
|
-
if (options[key] != null)
|
|
143
|
-
options[key] = normalizeNumber(options[key]);
|
|
144
|
-
});
|
|
145
|
-
return options;
|
|
146
|
-
}
|
|
147
|
-
function normalizeStringArray(value) {
|
|
148
|
-
let result;
|
|
149
|
-
if (typeof value === "string")
|
|
150
|
-
result = [value];
|
|
151
|
-
else if (Array.isArray(value))
|
|
152
|
-
result = value.filter((item) => item && typeof item === "string");
|
|
153
|
-
return (result == null ? void 0 : result.length) ? result : void 0;
|
|
154
|
-
}
|
|
155
|
-
function normalizeNumber(value) {
|
|
156
|
-
if (isNaN(+value))
|
|
157
|
-
return;
|
|
158
|
-
return +value;
|
|
159
|
-
}
|
|
160
|
-
const name$1 = "npmUrl";
|
|
161
|
-
const npmUrl = definePlugin({
|
|
162
|
-
name: name$1,
|
|
163
|
-
transform(transformHooks) {
|
|
164
|
-
transformHooks.afterParse.tap((_, context) => {
|
|
165
|
-
const { frontmatter: frontmatter2 } = context;
|
|
166
|
-
const markmap = frontmatter2 == null ? void 0 : frontmatter2.markmap;
|
|
167
|
-
if (markmap) {
|
|
168
|
-
["extraJs", "extraCss"].forEach((key) => {
|
|
169
|
-
const value = markmap[key];
|
|
170
|
-
if (value) {
|
|
171
|
-
markmap[key] = value.map((path) => {
|
|
172
|
-
if (path.startsWith("npm:")) {
|
|
173
|
-
return transformHooks.transformer.urlBuilder.getFullUrl(
|
|
174
|
-
path.slice(4)
|
|
175
|
-
);
|
|
176
|
-
}
|
|
177
|
-
return path;
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
return {};
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
const name = "hljs";
|
|
187
|
-
const preloadScripts = [
|
|
188
|
-
`@highlightjs/cdn-assets@${"11.8.0"}/highlight.min.js`
|
|
189
|
-
].map((path) => buildJSItem(path));
|
|
190
|
-
const styles = [
|
|
191
|
-
`@highlightjs/cdn-assets@${"11.8.0"}/styles/default.min.css`
|
|
192
|
-
// `highlight.js@${process.env.HLJS_VERSION}/styles/default.css`,
|
|
193
|
-
].map((path) => buildCSSItem(path));
|
|
194
|
-
const config = {
|
|
195
|
-
versions: {
|
|
196
|
-
hljs: "11.8.0"
|
|
197
|
-
},
|
|
198
|
-
preloadScripts,
|
|
199
|
-
styles
|
|
200
|
-
};
|
|
201
|
-
const plugin = definePlugin({
|
|
202
|
-
name,
|
|
203
|
-
config,
|
|
204
|
-
transform(transformHooks) {
|
|
205
|
-
var _a;
|
|
206
|
-
let enableFeature = noop;
|
|
207
|
-
transformHooks.parser.tap((md) => {
|
|
208
|
-
md.set({
|
|
209
|
-
highlight: (str, language) => {
|
|
210
|
-
enableFeature();
|
|
211
|
-
return hljs.highlightAuto(str, language ? [language] : void 0).value;
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
});
|
|
215
|
-
transformHooks.beforeParse.tap((_, context) => {
|
|
216
|
-
enableFeature = () => {
|
|
217
|
-
context.features[name] = true;
|
|
218
|
-
};
|
|
219
|
-
});
|
|
220
|
-
return {
|
|
221
|
-
styles: (_a = plugin.config) == null ? void 0 : _a.styles
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
const plugins = [frontmatter, plugin$1, plugin, npmUrl];
|
|
226
13
|
function patchJSItem(urlBuilder, item) {
|
|
227
14
|
if (item.type === "script" && item.data.src) {
|
|
228
15
|
return {
|
|
@@ -247,63 +34,33 @@ function patchCSSItem(urlBuilder, item) {
|
|
|
247
34
|
}
|
|
248
35
|
return item;
|
|
249
36
|
}
|
|
37
|
+
const builtInPlugins = plugins;
|
|
250
38
|
function cleanNode(node) {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
node.children = node.children.filter((item) => item.type !== "paragraph");
|
|
254
|
-
} else if (node.type === "list_item") {
|
|
255
|
-
node.children = node.children.filter((item) => {
|
|
256
|
-
if (["paragraph", "fence"].includes(item.type)) {
|
|
257
|
-
if (!node.content) {
|
|
258
|
-
node.content = item.content;
|
|
259
|
-
node.payload = {
|
|
260
|
-
...node.payload,
|
|
261
|
-
...item.payload
|
|
262
|
-
};
|
|
263
|
-
}
|
|
264
|
-
return false;
|
|
265
|
-
}
|
|
266
|
-
return true;
|
|
267
|
-
});
|
|
268
|
-
if (((_a = node.payload) == null ? void 0 : _a.index) != null) {
|
|
269
|
-
node.content = `${node.payload.index}. ${node.content}`;
|
|
270
|
-
}
|
|
271
|
-
} else if (node.type === "ordered_list") {
|
|
272
|
-
let index = ((_b = node.payload) == null ? void 0 : _b.startIndex) ?? 1;
|
|
273
|
-
node.children.forEach((item) => {
|
|
274
|
-
if (item.type === "list_item") {
|
|
275
|
-
item.payload = {
|
|
276
|
-
...item.payload,
|
|
277
|
-
index
|
|
278
|
-
};
|
|
279
|
-
index += 1;
|
|
280
|
-
}
|
|
281
|
-
});
|
|
39
|
+
while (!node.content && node.children.length === 1) {
|
|
40
|
+
node = node.children[0];
|
|
282
41
|
}
|
|
283
|
-
|
|
284
|
-
node
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
}
|
|
42
|
+
while (node.children.length === 1 && !node.children[0].content) {
|
|
43
|
+
node = {
|
|
44
|
+
...node,
|
|
45
|
+
children: node.children[0].children
|
|
46
|
+
};
|
|
288
47
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
resetDepth(child, depth + 1);
|
|
294
|
-
});
|
|
48
|
+
return {
|
|
49
|
+
...node,
|
|
50
|
+
children: node.children.map(cleanNode)
|
|
51
|
+
};
|
|
295
52
|
}
|
|
296
53
|
class Transformer {
|
|
297
|
-
constructor(
|
|
54
|
+
constructor(plugins2 = builtInPlugins) {
|
|
298
55
|
this.assetsMap = {};
|
|
299
56
|
this.urlBuilder = new UrlBuilder();
|
|
300
57
|
this.hooks = createTransformHooks(this);
|
|
301
|
-
this.plugins =
|
|
302
|
-
(
|
|
58
|
+
this.plugins = plugins2.map(
|
|
59
|
+
(plugin) => typeof plugin === "function" ? plugin() : plugin
|
|
303
60
|
);
|
|
304
61
|
const assetsMap = {};
|
|
305
|
-
for (const { name
|
|
306
|
-
assetsMap[
|
|
62
|
+
for (const { name, transform } of this.plugins) {
|
|
63
|
+
assetsMap[name] = transform(this.hooks);
|
|
307
64
|
}
|
|
308
65
|
this.assetsMap = assetsMap;
|
|
309
66
|
const md = new Remarkable("full", {
|
|
@@ -322,119 +79,41 @@ class Transformer {
|
|
|
322
79
|
this.md = md;
|
|
323
80
|
this.hooks.parser.call(md);
|
|
324
81
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
const root = {
|
|
328
|
-
type: "root",
|
|
329
|
-
depth: 0,
|
|
330
|
-
content: "",
|
|
331
|
-
children: [],
|
|
332
|
-
payload: {}
|
|
333
|
-
};
|
|
334
|
-
const stack = [root];
|
|
335
|
-
let depth = 0;
|
|
336
|
-
for (const token of tokens) {
|
|
337
|
-
const payload = {};
|
|
338
|
-
if (token.lines) {
|
|
339
|
-
payload.lines = token.lines;
|
|
340
|
-
}
|
|
341
|
-
let current = stack[stack.length - 1];
|
|
342
|
-
if (token.type.endsWith("_open")) {
|
|
343
|
-
const type = token.type.slice(0, -5);
|
|
344
|
-
if (type === "heading") {
|
|
345
|
-
depth = token.hLevel;
|
|
346
|
-
while ((current == null ? void 0 : current.depth) >= depth) {
|
|
347
|
-
stack.pop();
|
|
348
|
-
current = stack[stack.length - 1];
|
|
349
|
-
}
|
|
350
|
-
} else {
|
|
351
|
-
depth = Math.max(depth, (current == null ? void 0 : current.depth) || 0) + 1;
|
|
352
|
-
if (type === "ordered_list") {
|
|
353
|
-
payload.startIndex = token.order;
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
const item = {
|
|
357
|
-
type,
|
|
358
|
-
depth,
|
|
359
|
-
payload,
|
|
360
|
-
content: "",
|
|
361
|
-
children: []
|
|
362
|
-
};
|
|
363
|
-
current.children.push(item);
|
|
364
|
-
stack.push(item);
|
|
365
|
-
} else if (!current) {
|
|
366
|
-
continue;
|
|
367
|
-
} else if (token.type === `${current.type}_close`) {
|
|
368
|
-
if (current.type === "heading") {
|
|
369
|
-
depth = current.depth;
|
|
370
|
-
} else {
|
|
371
|
-
stack.pop();
|
|
372
|
-
depth = 0;
|
|
373
|
-
}
|
|
374
|
-
} else if (token.type === "inline") {
|
|
375
|
-
const revoke = this.hooks.htmltag.tap((ctx) => {
|
|
376
|
-
var _a;
|
|
377
|
-
const comment = (_a = ctx.result) == null ? void 0 : _a.match(/^<!--([\s\S]*?)-->$/);
|
|
378
|
-
const data = comment == null ? void 0 : comment[1].trim().split(" ");
|
|
379
|
-
if ((data == null ? void 0 : data[0]) === "fold") {
|
|
380
|
-
current.payload = {
|
|
381
|
-
...current.payload,
|
|
382
|
-
fold: ["all", "recursively"].includes(data[1]) ? 2 : 1
|
|
383
|
-
};
|
|
384
|
-
ctx.result = "";
|
|
385
|
-
}
|
|
386
|
-
});
|
|
387
|
-
const text = md.renderer.render([token], md.options, {});
|
|
388
|
-
revoke();
|
|
389
|
-
current.content = `${current.content || ""}${text}`;
|
|
390
|
-
} else if (token.type === "fence") {
|
|
391
|
-
const result = md.renderer.render([token], md.options, {});
|
|
392
|
-
current.children.push({
|
|
393
|
-
type: token.type,
|
|
394
|
-
depth: depth + 1,
|
|
395
|
-
content: result,
|
|
396
|
-
children: [],
|
|
397
|
-
payload
|
|
398
|
-
});
|
|
399
|
-
} else
|
|
400
|
-
;
|
|
401
|
-
}
|
|
402
|
-
return root;
|
|
403
|
-
}
|
|
404
|
-
transform(content) {
|
|
405
|
-
var _a;
|
|
82
|
+
transform(content, opts) {
|
|
83
|
+
var _a, _b;
|
|
406
84
|
const context = {
|
|
407
85
|
content,
|
|
408
86
|
features: {},
|
|
409
87
|
contentLineOffset: 0
|
|
410
88
|
};
|
|
411
89
|
this.hooks.beforeParse.call(this.md, context);
|
|
412
|
-
const
|
|
90
|
+
const html = this.md.render(context.content, {});
|
|
413
91
|
this.hooks.afterParse.call(this.md, context);
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
92
|
+
const root = cleanNode(
|
|
93
|
+
buildTree(html, {
|
|
94
|
+
...(_b = (_a = context.frontmatter) == null ? void 0 : _a.markmap) == null ? void 0 : _b.htmlParser,
|
|
95
|
+
...opts
|
|
96
|
+
})
|
|
97
|
+
);
|
|
419
98
|
return { ...context, root };
|
|
420
99
|
}
|
|
421
100
|
/**
|
|
422
101
|
* Get all assets from enabled plugins or filter them by plugin names as keys.
|
|
423
102
|
*/
|
|
424
103
|
getAssets(keys) {
|
|
425
|
-
const
|
|
104
|
+
const styles = [];
|
|
426
105
|
const scripts = [];
|
|
427
|
-
keys ?? (keys = this.plugins.map((
|
|
106
|
+
keys ?? (keys = this.plugins.map((plugin) => plugin.name));
|
|
428
107
|
for (const assets of keys.map((key) => this.assetsMap[key])) {
|
|
429
108
|
if (assets) {
|
|
430
109
|
if (assets.styles)
|
|
431
|
-
|
|
110
|
+
styles.push(...assets.styles);
|
|
432
111
|
if (assets.scripts)
|
|
433
112
|
scripts.push(...assets.scripts);
|
|
434
113
|
}
|
|
435
114
|
}
|
|
436
115
|
return {
|
|
437
|
-
styles:
|
|
116
|
+
styles: styles.map((item) => patchCSSItem(this.urlBuilder, item)),
|
|
438
117
|
scripts: scripts.map((item) => patchJSItem(this.urlBuilder, item))
|
|
439
118
|
};
|
|
440
119
|
}
|
|
@@ -442,7 +121,7 @@ class Transformer {
|
|
|
442
121
|
* Get used assets by features object returned by `transform`.
|
|
443
122
|
*/
|
|
444
123
|
getUsedAssets(features) {
|
|
445
|
-
const keys = this.plugins.map((
|
|
124
|
+
const keys = this.plugins.map((plugin) => plugin.name).filter((name) => features[name]);
|
|
446
125
|
return this.getAssets(keys);
|
|
447
126
|
}
|
|
448
127
|
fillTemplate(root, assets, extra) {
|
|
@@ -450,8 +129,8 @@ class Transformer {
|
|
|
450
129
|
...extra
|
|
451
130
|
};
|
|
452
131
|
extra.baseJs ?? (extra.baseJs = baseJsPaths.map((path) => this.urlBuilder.getFullUrl(path)).map((path) => buildJSItem(path)));
|
|
453
|
-
const { scripts, styles
|
|
454
|
-
const cssList = [...
|
|
132
|
+
const { scripts, styles } = assets;
|
|
133
|
+
const cssList = [...styles ? persistCSS(styles) : []];
|
|
455
134
|
const context = {
|
|
456
135
|
getMarkmap: () => window.markmap,
|
|
457
136
|
getOptions: extra.getOptions,
|
|
@@ -494,7 +173,7 @@ const transformerVersions = {
|
|
|
494
173
|
export {
|
|
495
174
|
Transformer,
|
|
496
175
|
baseJsPaths,
|
|
497
|
-
|
|
176
|
+
builtInPlugins,
|
|
498
177
|
template,
|
|
499
178
|
transformerVersions
|
|
500
179
|
};
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const markmapCommon = require("markmap-common");
|
|
4
|
+
const markmapHtmlParser = require("markmap-html-parser");
|
|
5
|
+
const remarkable = require("remarkable");
|
|
6
|
+
const template = '<!DOCTYPE html>\n<html>\n<head>\n<meta charset="UTF-8">\n<meta name="viewport" content="width=device-width, initial-scale=1.0">\n<meta http-equiv="X-UA-Compatible" content="ie=edge">\n<title>Markmap</title>\n<style>\n* {\n margin: 0;\n padding: 0;\n}\n#mindmap {\n display: block;\n width: 100vw;\n height: 100vh;\n}\n</style>\n<!--CSS-->\n</head>\n<body>\n<svg id="mindmap"></svg>\n<!--JS-->\n</body>\n</html>\n';
|
|
7
|
+
const baseJsPaths = [
|
|
8
|
+
`d3@${"7.8.5"}/dist/d3.min.js`,
|
|
9
|
+
`markmap-view@${"0.15.8"}/dist/browser/index.js`
|
|
10
|
+
];
|
|
11
|
+
function createTransformHooks(transformer) {
|
|
12
|
+
return {
|
|
13
|
+
transformer,
|
|
14
|
+
parser: new markmapCommon.Hook(),
|
|
15
|
+
beforeParse: new markmapCommon.Hook(),
|
|
16
|
+
afterParse: new markmapCommon.Hook(),
|
|
17
|
+
htmltag: new markmapCommon.Hook(),
|
|
18
|
+
retransform: new markmapCommon.Hook()
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function patchJSItem(urlBuilder, item) {
|
|
22
|
+
if (item.type === "script" && item.data.src) {
|
|
23
|
+
return {
|
|
24
|
+
...item,
|
|
25
|
+
data: {
|
|
26
|
+
...item.data,
|
|
27
|
+
src: urlBuilder.getFullUrl(item.data.src)
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
return item;
|
|
32
|
+
}
|
|
33
|
+
function patchCSSItem(urlBuilder, item) {
|
|
34
|
+
if (item.type === "stylesheet" && item.data.href) {
|
|
35
|
+
return {
|
|
36
|
+
...item,
|
|
37
|
+
data: {
|
|
38
|
+
...item.data,
|
|
39
|
+
href: urlBuilder.getFullUrl(item.data.href)
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return item;
|
|
44
|
+
}
|
|
45
|
+
const builtInPlugins = [];
|
|
46
|
+
function cleanNode(node) {
|
|
47
|
+
while (!node.content && node.children.length === 1) {
|
|
48
|
+
node = node.children[0];
|
|
49
|
+
}
|
|
50
|
+
while (node.children.length === 1 && !node.children[0].content) {
|
|
51
|
+
node = {
|
|
52
|
+
...node,
|
|
53
|
+
children: node.children[0].children
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
...node,
|
|
58
|
+
children: node.children.map(cleanNode)
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
class Transformer {
|
|
62
|
+
constructor(plugins = builtInPlugins) {
|
|
63
|
+
this.assetsMap = {};
|
|
64
|
+
this.urlBuilder = new markmapCommon.UrlBuilder();
|
|
65
|
+
this.hooks = createTransformHooks(this);
|
|
66
|
+
this.plugins = plugins.map(
|
|
67
|
+
(plugin) => typeof plugin === "function" ? plugin() : plugin
|
|
68
|
+
);
|
|
69
|
+
const assetsMap = {};
|
|
70
|
+
for (const { name, transform } of this.plugins) {
|
|
71
|
+
assetsMap[name] = transform(this.hooks);
|
|
72
|
+
}
|
|
73
|
+
this.assetsMap = assetsMap;
|
|
74
|
+
const md = new remarkable.Remarkable("full", {
|
|
75
|
+
html: true,
|
|
76
|
+
breaks: true,
|
|
77
|
+
maxNesting: Infinity
|
|
78
|
+
});
|
|
79
|
+
md.renderer.rules.htmltag = markmapCommon.wrapFunction(
|
|
80
|
+
md.renderer.rules.htmltag,
|
|
81
|
+
(render, ...args) => {
|
|
82
|
+
const result = render(...args);
|
|
83
|
+
this.hooks.htmltag.call({ args, result });
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
);
|
|
87
|
+
this.md = md;
|
|
88
|
+
this.hooks.parser.call(md);
|
|
89
|
+
}
|
|
90
|
+
transform(content, opts) {
|
|
91
|
+
var _a, _b;
|
|
92
|
+
const context = {
|
|
93
|
+
content,
|
|
94
|
+
features: {},
|
|
95
|
+
contentLineOffset: 0
|
|
96
|
+
};
|
|
97
|
+
this.hooks.beforeParse.call(this.md, context);
|
|
98
|
+
const html = this.md.render(context.content, {});
|
|
99
|
+
this.hooks.afterParse.call(this.md, context);
|
|
100
|
+
const root = cleanNode(
|
|
101
|
+
markmapHtmlParser.buildTree(html, {
|
|
102
|
+
...(_b = (_a = context.frontmatter) == null ? void 0 : _a.markmap) == null ? void 0 : _b.htmlParser,
|
|
103
|
+
...opts
|
|
104
|
+
})
|
|
105
|
+
);
|
|
106
|
+
return { ...context, root };
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Get all assets from enabled plugins or filter them by plugin names as keys.
|
|
110
|
+
*/
|
|
111
|
+
getAssets(keys) {
|
|
112
|
+
const styles = [];
|
|
113
|
+
const scripts = [];
|
|
114
|
+
keys ?? (keys = this.plugins.map((plugin) => plugin.name));
|
|
115
|
+
for (const assets of keys.map((key) => this.assetsMap[key])) {
|
|
116
|
+
if (assets) {
|
|
117
|
+
if (assets.styles)
|
|
118
|
+
styles.push(...assets.styles);
|
|
119
|
+
if (assets.scripts)
|
|
120
|
+
scripts.push(...assets.scripts);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
styles: styles.map((item) => patchCSSItem(this.urlBuilder, item)),
|
|
125
|
+
scripts: scripts.map((item) => patchJSItem(this.urlBuilder, item))
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Get used assets by features object returned by `transform`.
|
|
130
|
+
*/
|
|
131
|
+
getUsedAssets(features) {
|
|
132
|
+
const keys = this.plugins.map((plugin) => plugin.name).filter((name) => features[name]);
|
|
133
|
+
return this.getAssets(keys);
|
|
134
|
+
}
|
|
135
|
+
fillTemplate(root, assets, extra) {
|
|
136
|
+
extra = {
|
|
137
|
+
...extra
|
|
138
|
+
};
|
|
139
|
+
extra.baseJs ?? (extra.baseJs = baseJsPaths.map((path) => this.urlBuilder.getFullUrl(path)).map((path) => markmapCommon.buildJSItem(path)));
|
|
140
|
+
const { scripts, styles } = assets;
|
|
141
|
+
const cssList = [...styles ? markmapCommon.persistCSS(styles) : []];
|
|
142
|
+
const context = {
|
|
143
|
+
getMarkmap: () => window.markmap,
|
|
144
|
+
getOptions: extra.getOptions,
|
|
145
|
+
jsonOptions: extra.jsonOptions,
|
|
146
|
+
root
|
|
147
|
+
};
|
|
148
|
+
const jsList = [
|
|
149
|
+
...markmapCommon.persistJS(
|
|
150
|
+
[
|
|
151
|
+
...extra.baseJs,
|
|
152
|
+
...scripts || [],
|
|
153
|
+
{
|
|
154
|
+
type: "iife",
|
|
155
|
+
data: {
|
|
156
|
+
fn: (getMarkmap, getOptions, root2, jsonOptions) => {
|
|
157
|
+
const markmap = getMarkmap();
|
|
158
|
+
window.mm = markmap.Markmap.create(
|
|
159
|
+
"svg#mindmap",
|
|
160
|
+
(getOptions || markmap.deriveOptions)(jsonOptions),
|
|
161
|
+
root2
|
|
162
|
+
);
|
|
163
|
+
},
|
|
164
|
+
getParams: ({ getMarkmap, getOptions, root: root2, jsonOptions }) => {
|
|
165
|
+
return [getMarkmap, getOptions, root2, jsonOptions];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
],
|
|
170
|
+
context
|
|
171
|
+
)
|
|
172
|
+
];
|
|
173
|
+
const html = template.replace("<!--CSS-->", () => cssList.join("")).replace("<!--JS-->", () => jsList.join(""));
|
|
174
|
+
return html;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const transformerVersions = {
|
|
178
|
+
"markmap-lib": "process.env.VERSION",
|
|
179
|
+
d3: "7.8.5"
|
|
180
|
+
};
|
|
181
|
+
exports.Transformer = Transformer;
|
|
182
|
+
exports.baseJsPaths = baseJsPaths;
|
|
183
|
+
exports.builtInPlugins = builtInPlugins;
|
|
184
|
+
exports.template = template;
|
|
185
|
+
exports.transformerVersions = transformerVersions;
|