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.
@@ -1,29 +1,212 @@
1
+ import { Hook, buildJSItem, buildCSSItem, loadJS, noop, wrapFunction, UrlBuilder, persistCSS, persistJS } from "markmap-common";
2
+ import { buildTree } from "markmap-html-parser";
1
3
  import { Remarkable } from "remarkable";
2
- import { buildJSItem, buildCSSItem, Hook, noop, loadJS, UrlBuilder, wrapFunction, persistCSS, persistJS } from "markmap-common";
3
- import remarkableKatex from "remarkable-katex";
4
4
  import yaml from "js-yaml";
5
+ import remarkableKatex from "remarkable-katex";
5
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';
6
7
  const baseJsPaths = [
7
8
  `d3@${"7.8.5"}/dist/d3.min.js`,
8
9
  `markmap-view@${"0.15.8"}/dist/browser/index.js`
9
10
  ];
10
- const name$3 = "katex";
11
+ function createTransformHooks(transformer) {
12
+ return {
13
+ transformer,
14
+ parser: new Hook(),
15
+ beforeParse: new Hook(),
16
+ afterParse: new Hook(),
17
+ htmltag: new Hook(),
18
+ retransform: new Hook()
19
+ };
20
+ }
21
+ function definePlugin(plugin2) {
22
+ return plugin2;
23
+ }
24
+ const svg_marked = `<svg width="16" height="16" fill="currentColor" viewBox="0 -3 24 24"><path d="M19 3H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V5a2 2 0 0 0-2-2m-9 14-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8z"/></svg>`;
25
+ const svg_unmarked = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 -3 24 24"><path fill-rule="evenodd" d="M6 5a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V6a1 1 0 0 0-1-1zM3 6a3 3 0 0 1 3-3h12a3 3 0 0 1 3 3v12a3 3 0 0 1-3 3H6a3 3 0 0 1-3-3v-5z" clip-rule="evenodd"/></svg>`;
26
+ const name$5 = "checkbox";
27
+ const plugin$3 = definePlugin({
28
+ name: name$5,
29
+ transform(transformHooks) {
30
+ transformHooks.parser.tap((md) => {
31
+ md.core.ruler.before(
32
+ "inline",
33
+ "checkbox",
34
+ (state) => {
35
+ for (let i = 2; i < state.tokens.length; i += 1) {
36
+ const token = state.tokens[i];
37
+ if (token.type === "inline" && token.content) {
38
+ const prevType = state.tokens[i - 1].type;
39
+ const prevPrevType = state.tokens[i - 2].type;
40
+ if (prevType === "heading_open" || prevType === "paragraph_open" && prevPrevType === "list_item_open") {
41
+ if (token.content.startsWith("[ ]")) {
42
+ token.content = `${svg_unmarked} ${token.content.slice(3)}`;
43
+ } else if (token.content.startsWith("[x]")) {
44
+ token.content = `${svg_marked} ${token.content.slice(3)}`;
45
+ }
46
+ }
47
+ }
48
+ }
49
+ return false;
50
+ },
51
+ {}
52
+ );
53
+ });
54
+ return {};
55
+ }
56
+ });
57
+ const checkbox = plugin$3;
58
+ const name$4 = "frontmatter";
59
+ const frontmatter = definePlugin({
60
+ name: name$4,
61
+ transform(transformHooks) {
62
+ transformHooks.beforeParse.tap((md, context) => {
63
+ const { content } = context;
64
+ if (!/^---\r?\n/.test(content))
65
+ return;
66
+ const match = /\n---\r?\n/.exec(content);
67
+ if (!match)
68
+ return;
69
+ const raw = content.slice(4, match.index);
70
+ let frontmatter2;
71
+ try {
72
+ frontmatter2 = yaml.load(raw);
73
+ if (frontmatter2 == null ? void 0 : frontmatter2.markmap) {
74
+ frontmatter2.markmap = normalizeMarkmapJsonOptions(
75
+ frontmatter2.markmap
76
+ );
77
+ }
78
+ } catch {
79
+ return;
80
+ }
81
+ context.frontmatter = frontmatter2;
82
+ context.content = content.slice(match.index + match[0].length);
83
+ context.contentLineOffset = content.slice(0, match.index).split("\n").length + 1;
84
+ });
85
+ return {};
86
+ }
87
+ });
88
+ function normalizeMarkmapJsonOptions(options) {
89
+ if (!options)
90
+ return;
91
+ ["color", "extraJs", "extraCss"].forEach((key) => {
92
+ if (options[key] != null)
93
+ options[key] = normalizeStringArray(options[key]);
94
+ });
95
+ ["duration", "maxWidth", "initialExpandLevel"].forEach((key) => {
96
+ if (options[key] != null)
97
+ options[key] = normalizeNumber(options[key]);
98
+ });
99
+ return options;
100
+ }
101
+ function normalizeStringArray(value) {
102
+ let result;
103
+ if (typeof value === "string")
104
+ result = [value];
105
+ else if (Array.isArray(value))
106
+ result = value.filter((item) => item && typeof item === "string");
107
+ return (result == null ? void 0 : result.length) ? result : void 0;
108
+ }
109
+ function normalizeNumber(value) {
110
+ if (isNaN(+value))
111
+ return;
112
+ return +value;
113
+ }
114
+ function patchJSItem(urlBuilder, item) {
115
+ if (item.type === "script" && item.data.src) {
116
+ return {
117
+ ...item,
118
+ data: {
119
+ ...item.data,
120
+ src: urlBuilder.getFullUrl(item.data.src)
121
+ }
122
+ };
123
+ }
124
+ return item;
125
+ }
126
+ function patchCSSItem(urlBuilder, item) {
127
+ if (item.type === "stylesheet" && item.data.href) {
128
+ return {
129
+ ...item,
130
+ data: {
131
+ ...item.data,
132
+ href: urlBuilder.getFullUrl(item.data.href)
133
+ }
134
+ };
135
+ }
136
+ return item;
137
+ }
138
+ const name$3 = "hljs";
11
139
  const preloadScripts$1 = [
140
+ `@highlightjs/cdn-assets@${"11.8.0"}/highlight.min.js`
141
+ ].map((path) => buildJSItem(path));
142
+ const styles$1 = [
143
+ `@highlightjs/cdn-assets@${"11.8.0"}/styles/default.min.css`
144
+ // `highlight.js@${process.env.HLJS_VERSION}/styles/default.css`,
145
+ ].map((path) => buildCSSItem(path));
146
+ const config$1 = {
147
+ versions: {
148
+ hljs: "11.8.0"
149
+ },
150
+ preloadScripts: preloadScripts$1,
151
+ styles: styles$1
152
+ };
153
+ const plugin$2 = definePlugin({
154
+ name: name$3,
155
+ config: config$1,
156
+ transform(transformHooks) {
157
+ var _a, _b, _c;
158
+ let loading;
159
+ const preloadScripts2 = ((_b = (_a = plugin$2.config) == null ? void 0 : _a.preloadScripts) == null ? void 0 : _b.map(
160
+ (item) => patchJSItem(transformHooks.transformer.urlBuilder, item)
161
+ )) || [];
162
+ const autoload = () => {
163
+ loading || (loading = loadJS(preloadScripts2));
164
+ return loading;
165
+ };
166
+ let enableFeature = noop;
167
+ transformHooks.parser.tap((md) => {
168
+ md.set({
169
+ highlight: (str, language) => {
170
+ enableFeature();
171
+ const { hljs: hljs2 } = window;
172
+ if (hljs2) {
173
+ return hljs2.highlightAuto(str, language ? [language] : void 0).value;
174
+ }
175
+ autoload().then(() => {
176
+ transformHooks.retransform.call();
177
+ });
178
+ return str;
179
+ }
180
+ });
181
+ });
182
+ transformHooks.beforeParse.tap((_, context) => {
183
+ enableFeature = () => {
184
+ context.features[name$3] = true;
185
+ };
186
+ });
187
+ return {
188
+ styles: (_c = plugin$2.config) == null ? void 0 : _c.styles
189
+ };
190
+ }
191
+ });
192
+ const hljs = plugin$2;
193
+ const name$2 = "katex";
194
+ const preloadScripts = [
12
195
  `katex@${"0.16.8"}/dist/katex.min.js`
13
196
  ].map((path) => buildJSItem(path));
14
197
  const webfontloader = buildJSItem(
15
198
  `webfontloader@${"1.6.28"}/webfontloader.js`
16
199
  );
17
200
  webfontloader.data.defer = true;
18
- const styles$1 = [`katex@${"0.16.8"}/dist/katex.min.css`].map(
201
+ const styles = [`katex@${"0.16.8"}/dist/katex.min.css`].map(
19
202
  (path) => buildCSSItem(path)
20
203
  );
21
- const config$1 = {
204
+ const config = {
22
205
  versions: {
23
206
  katex: "0.16.8",
24
207
  webfontloader: "1.6.28"
25
208
  },
26
- preloadScripts: preloadScripts$1,
209
+ preloadScripts,
27
210
  scripts: [
28
211
  {
29
212
  type: "iife",
@@ -58,7 +241,7 @@ const config$1 = {
58
241
  },
59
242
  webfontloader
60
243
  ],
61
- styles: styles$1
244
+ styles
62
245
  };
63
246
  function addDefaultVersions(paths, name2, version) {
64
247
  return paths.map((path) => {
@@ -76,46 +259,9 @@ function addDefaultVersions(paths, name2, version) {
76
259
  return path;
77
260
  });
78
261
  }
79
- function patchJSItem(urlBuilder, item) {
80
- if (item.type === "script" && item.data.src) {
81
- return {
82
- ...item,
83
- data: {
84
- ...item.data,
85
- src: urlBuilder.getFullUrl(item.data.src)
86
- }
87
- };
88
- }
89
- return item;
90
- }
91
- function patchCSSItem(urlBuilder, item) {
92
- if (item.type === "stylesheet" && item.data.href) {
93
- return {
94
- ...item,
95
- data: {
96
- ...item.data,
97
- href: urlBuilder.getFullUrl(item.data.href)
98
- }
99
- };
100
- }
101
- return item;
102
- }
103
- function createTransformHooks(transformer) {
104
- return {
105
- transformer,
106
- parser: new Hook(),
107
- beforeParse: new Hook(),
108
- afterParse: new Hook(),
109
- htmltag: new Hook(),
110
- retransform: new Hook()
111
- };
112
- }
113
- function definePlugin(plugin2) {
114
- return plugin2;
115
- }
116
262
  const plugin$1 = definePlugin({
117
- name: name$3,
118
- config: config$1,
263
+ name: name$2,
264
+ config,
119
265
  transform(transformHooks) {
120
266
  var _a, _b, _c, _d;
121
267
  let loading;
@@ -127,9 +273,9 @@ const plugin$1 = definePlugin({
127
273
  return loading;
128
274
  };
129
275
  const renderKatex = (source, displayMode) => {
130
- const { katex } = window;
131
- if (katex) {
132
- return katex.renderToString(source, {
276
+ const { katex: katex2 } = window;
277
+ if (katex2) {
278
+ return katex2.renderToString(source, {
133
279
  displayMode,
134
280
  throwOnError: false
135
281
  });
@@ -150,7 +296,7 @@ const plugin$1 = definePlugin({
150
296
  });
151
297
  transformHooks.beforeParse.tap((_, context) => {
152
298
  enableFeature = () => {
153
- context.features[name$3] = true;
299
+ context.features[name$2] = true;
154
300
  };
155
301
  });
156
302
  transformHooks.afterParse.tap((_, context) => {
@@ -163,7 +309,7 @@ const plugin$1 = definePlugin({
163
309
  if (value) {
164
310
  markmap[key] = addDefaultVersions(
165
311
  value,
166
- name$3,
312
+ name$2,
167
313
  ((_b2 = (_a3 = plugin$1.config) == null ? void 0 : _a3.versions) == null ? void 0 : _b2.katex) || ""
168
314
  );
169
315
  }
@@ -176,62 +322,7 @@ const plugin$1 = definePlugin({
176
322
  };
177
323
  }
178
324
  });
179
- const name$2 = "frontmatter";
180
- const frontmatter = definePlugin({
181
- name: name$2,
182
- transform(transformHooks) {
183
- transformHooks.beforeParse.tap((md, context) => {
184
- const { content } = context;
185
- if (!/^---\r?\n/.test(content))
186
- return;
187
- const match = /\n---\r?\n/.exec(content);
188
- if (!match)
189
- return;
190
- const raw = content.slice(4, match.index);
191
- let frontmatter2;
192
- try {
193
- frontmatter2 = yaml.load(raw);
194
- if (frontmatter2 == null ? void 0 : frontmatter2.markmap) {
195
- frontmatter2.markmap = normalizeMarkmapJsonOptions(
196
- frontmatter2.markmap
197
- );
198
- }
199
- } catch {
200
- return;
201
- }
202
- context.frontmatter = frontmatter2;
203
- context.content = content.slice(match.index + match[0].length);
204
- context.contentLineOffset = content.slice(0, match.index).split("\n").length + 1;
205
- });
206
- return {};
207
- }
208
- });
209
- function normalizeMarkmapJsonOptions(options) {
210
- if (!options)
211
- return;
212
- ["color", "extraJs", "extraCss"].forEach((key) => {
213
- if (options[key] != null)
214
- options[key] = normalizeStringArray(options[key]);
215
- });
216
- ["duration", "maxWidth", "initialExpandLevel"].forEach((key) => {
217
- if (options[key] != null)
218
- options[key] = normalizeNumber(options[key]);
219
- });
220
- return options;
221
- }
222
- function normalizeStringArray(value) {
223
- let result;
224
- if (typeof value === "string")
225
- result = [value];
226
- else if (Array.isArray(value))
227
- result = value.filter((item) => item && typeof item === "string");
228
- return (result == null ? void 0 : result.length) ? result : void 0;
229
- }
230
- function normalizeNumber(value) {
231
- if (isNaN(+value))
232
- return;
233
- return +value;
234
- }
325
+ const katex = plugin$1;
235
326
  const name$1 = "npmUrl";
236
327
  const npmUrl = definePlugin({
237
328
  name: name$1,
@@ -258,113 +349,68 @@ const npmUrl = definePlugin({
258
349
  return {};
259
350
  }
260
351
  });
261
- const name = "hljs";
262
- const preloadScripts = [
263
- `@highlightjs/cdn-assets@${"11.8.0"}/highlight.min.js`
264
- ].map((path) => buildJSItem(path));
265
- const styles = [
266
- `@highlightjs/cdn-assets@${"11.8.0"}/styles/default.min.css`
267
- // `highlight.js@${process.env.HLJS_VERSION}/styles/default.css`,
268
- ].map((path) => buildCSSItem(path));
269
- const config = {
270
- versions: {
271
- hljs: "11.8.0"
272
- },
273
- preloadScripts,
274
- styles
275
- };
352
+ const name = "sourceLines";
276
353
  const plugin = definePlugin({
277
354
  name,
278
- config,
279
355
  transform(transformHooks) {
280
- var _a, _b, _c;
281
- let loading;
282
- const preloadScripts2 = ((_b = (_a = plugin.config) == null ? void 0 : _a.preloadScripts) == null ? void 0 : _b.map(
283
- (item) => patchJSItem(transformHooks.transformer.urlBuilder, item)
284
- )) || [];
285
- const autoload = () => {
286
- loading || (loading = loadJS(preloadScripts2));
287
- return loading;
288
- };
289
- let enableFeature = noop;
290
356
  transformHooks.parser.tap((md) => {
291
- md.set({
292
- highlight: (str, language) => {
293
- enableFeature();
294
- const { hljs } = window;
295
- if (hljs) {
296
- return hljs.highlightAuto(str, language ? [language] : void 0).value;
297
- }
298
- autoload().then(() => {
299
- transformHooks.retransform.call();
357
+ Object.entries(md.renderer.rules).forEach(([key, value]) => {
358
+ if (typeof value === "function") {
359
+ md.renderer.rules[key] = patchRule(value);
360
+ } else {
361
+ Object.entries(value).forEach(([k, v]) => {
362
+ value[k] = patchRule(v);
300
363
  });
301
- return str;
302
364
  }
303
365
  });
304
366
  });
305
- transformHooks.beforeParse.tap((_, context) => {
306
- enableFeature = () => {
307
- context.features[name] = true;
308
- };
309
- });
310
- return {
311
- styles: (_c = plugin.config) == null ? void 0 : _c.styles
312
- };
367
+ return {};
313
368
  }
314
369
  });
315
- const plugins = [frontmatter, plugin$1, plugin, npmUrl];
316
- function cleanNode(node) {
317
- var _a, _b;
318
- if (node.type === "heading") {
319
- node.children = node.children.filter((item) => item.type !== "paragraph");
320
- } else if (node.type === "list_item") {
321
- node.children = node.children.filter((item) => {
322
- if (["paragraph", "fence"].includes(item.type)) {
323
- if (!node.content) {
324
- node.content = item.content;
325
- node.payload = {
326
- ...node.payload,
327
- ...item.payload
328
- };
329
- }
330
- return false;
331
- }
332
- return true;
333
- });
334
- if (((_a = node.payload) == null ? void 0 : _a.index) != null) {
335
- node.content = `${node.payload.index}. ${node.content}`;
370
+ function patchRule(rule, _key) {
371
+ return wrapFunction(rule, (render, tokens, idx, ...rest) => {
372
+ let html = render(tokens, idx, ...rest);
373
+ const { lines } = tokens[idx];
374
+ if (lines) {
375
+ html = html.replace(
376
+ /^<[\w-]+/,
377
+ (m) => `${m} data-lines="${lines.join(",")}"`
378
+ );
336
379
  }
337
- } else if (node.type === "ordered_list") {
338
- let index = ((_b = node.payload) == null ? void 0 : _b.startIndex) ?? 1;
339
- node.children.forEach((item) => {
340
- if (item.type === "list_item") {
341
- item.payload = {
342
- ...item.payload,
343
- index
344
- };
345
- index += 1;
346
- }
347
- });
380
+ return html;
381
+ });
382
+ }
383
+ const sourceLines = plugin;
384
+ const plugins = [
385
+ frontmatter,
386
+ katex,
387
+ hljs,
388
+ npmUrl,
389
+ checkbox,
390
+ sourceLines
391
+ ];
392
+ const builtInPlugins = plugins;
393
+ function cleanNode(node) {
394
+ while (!node.content && node.children.length === 1) {
395
+ node = node.children[0];
348
396
  }
349
- if (node.children.length > 0) {
350
- node.children.forEach((child) => cleanNode(child));
351
- if (node.children.length === 1 && !node.children[0].content) {
352
- node.children = node.children[0].children;
353
- }
397
+ while (node.children.length === 1 && !node.children[0].content) {
398
+ node = {
399
+ ...node,
400
+ children: node.children[0].children
401
+ };
354
402
  }
355
- }
356
- function resetDepth(node, depth = 0) {
357
- node.depth = depth;
358
- node.children.forEach((child) => {
359
- resetDepth(child, depth + 1);
360
- });
403
+ return {
404
+ ...node,
405
+ children: node.children.map(cleanNode)
406
+ };
361
407
  }
362
408
  class Transformer {
363
- constructor(plugins$1 = plugins) {
409
+ constructor(plugins2 = builtInPlugins) {
364
410
  this.assetsMap = {};
365
411
  this.urlBuilder = new UrlBuilder();
366
412
  this.hooks = createTransformHooks(this);
367
- this.plugins = plugins$1.map(
413
+ this.plugins = plugins2.map(
368
414
  (plugin2) => typeof plugin2 === "function" ? plugin2() : plugin2
369
415
  );
370
416
  const assetsMap = {};
@@ -388,100 +434,22 @@ class Transformer {
388
434
  this.md = md;
389
435
  this.hooks.parser.call(md);
390
436
  }
391
- buildTree(tokens) {
392
- const { md } = this;
393
- const root = {
394
- type: "root",
395
- depth: 0,
396
- content: "",
397
- children: [],
398
- payload: {}
399
- };
400
- const stack = [root];
401
- let depth = 0;
402
- for (const token of tokens) {
403
- const payload = {};
404
- if (token.lines) {
405
- payload.lines = token.lines;
406
- }
407
- let current = stack[stack.length - 1];
408
- if (token.type.endsWith("_open")) {
409
- const type = token.type.slice(0, -5);
410
- if (type === "heading") {
411
- depth = token.hLevel;
412
- while ((current == null ? void 0 : current.depth) >= depth) {
413
- stack.pop();
414
- current = stack[stack.length - 1];
415
- }
416
- } else {
417
- depth = Math.max(depth, (current == null ? void 0 : current.depth) || 0) + 1;
418
- if (type === "ordered_list") {
419
- payload.startIndex = token.order;
420
- }
421
- }
422
- const item = {
423
- type,
424
- depth,
425
- payload,
426
- content: "",
427
- children: []
428
- };
429
- current.children.push(item);
430
- stack.push(item);
431
- } else if (!current) {
432
- continue;
433
- } else if (token.type === `${current.type}_close`) {
434
- if (current.type === "heading") {
435
- depth = current.depth;
436
- } else {
437
- stack.pop();
438
- depth = 0;
439
- }
440
- } else if (token.type === "inline") {
441
- const revoke = this.hooks.htmltag.tap((ctx) => {
442
- var _a;
443
- const comment = (_a = ctx.result) == null ? void 0 : _a.match(/^<!--([\s\S]*?)-->$/);
444
- const data = comment == null ? void 0 : comment[1].trim().split(" ");
445
- if ((data == null ? void 0 : data[0]) === "fold") {
446
- current.payload = {
447
- ...current.payload,
448
- fold: ["all", "recursively"].includes(data[1]) ? 2 : 1
449
- };
450
- ctx.result = "";
451
- }
452
- });
453
- const text = md.renderer.render([token], md.options, {});
454
- revoke();
455
- current.content = `${current.content || ""}${text}`;
456
- } else if (token.type === "fence") {
457
- const result = md.renderer.render([token], md.options, {});
458
- current.children.push({
459
- type: token.type,
460
- depth: depth + 1,
461
- content: result,
462
- children: [],
463
- payload
464
- });
465
- } else
466
- ;
467
- }
468
- return root;
469
- }
470
- transform(content) {
471
- var _a;
437
+ transform(content, opts) {
438
+ var _a, _b;
472
439
  const context = {
473
440
  content,
474
441
  features: {},
475
442
  contentLineOffset: 0
476
443
  };
477
444
  this.hooks.beforeParse.call(this.md, context);
478
- const tokens = this.md.parse(context.content, {});
445
+ const html = this.md.render(context.content, {});
479
446
  this.hooks.afterParse.call(this.md, context);
480
- let root = this.buildTree(tokens);
481
- cleanNode(root);
482
- if (((_a = root.children) == null ? void 0 : _a.length) === 1)
483
- root = root.children[0];
484
- resetDepth(root);
447
+ const root = cleanNode(
448
+ buildTree(html, {
449
+ ...(_b = (_a = context.frontmatter) == null ? void 0 : _a.markmap) == null ? void 0 : _b.htmlParser,
450
+ ...opts
451
+ })
452
+ );
485
453
  return { ...context, root };
486
454
  }
487
455
  /**
@@ -560,7 +528,7 @@ const transformerVersions = {
560
528
  export {
561
529
  Transformer,
562
530
  baseJsPaths,
563
- plugins as builtInPlugins,
531
+ builtInPlugins,
564
532
  template,
565
533
  transformerVersions
566
534
  };