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