markmap-lib 0.14.4-alpha.1 → 0.15.0-alpha.0

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,4 +1,4 @@
1
- /*! markmap-lib v0.14.4-alpha.1+8a57fea | MIT License */
1
+ /*! markmap-lib v0.15.0-alpha.0 | MIT License */
2
2
  'use strict';
3
3
 
4
4
  var _extends = require('@babel/runtime/helpers/extends');
@@ -9,25 +9,12 @@ var Prism = require('prismjs');
9
9
  var loadLanguages = require('prismjs/components/');
10
10
  var yaml = require('js-yaml');
11
11
 
12
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
13
-
14
- var _extends__default = /*#__PURE__*/_interopDefaultLegacy(_extends);
15
- var remarkableKatex__default = /*#__PURE__*/_interopDefaultLegacy(remarkableKatex);
16
- var Prism__default = /*#__PURE__*/_interopDefaultLegacy(Prism);
17
- var loadLanguages__default = /*#__PURE__*/_interopDefaultLegacy(loadLanguages);
18
- var yaml__default = /*#__PURE__*/_interopDefaultLegacy(yaml);
19
-
20
12
  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";
21
- const BASE_JS = [`https://cdn.jsdelivr.net/npm/d3@${"6.7.0"}`, `https://cdn.jsdelivr.net/npm/markmap-view@${"0.14.4-alpha.1+8a57fea"}`].map(src => ({
22
- type: 'script',
23
- data: {
24
- src
25
- }
26
- }));
13
+ const baseJsPaths = [`d3@${"6.2.0"}`, `markmap-view@${"0.15.0-alpha.0"}`];
27
14
  function fillTemplate(root, assets, extra) {
28
- extra = _extends__default({
29
- baseJs: BASE_JS
30
- }, extra);
15
+ var _extra, _extra$baseJs;
16
+ extra = _extends({}, extra);
17
+ (_extra$baseJs = (_extra = extra).baseJs) != null ? _extra$baseJs : _extra.baseJs = baseJsPaths.map(markmapCommon.buildJSItem);
31
18
  const {
32
19
  scripts,
33
20
  styles
@@ -60,52 +47,40 @@ function fillTemplate(root, assets, extra) {
60
47
  return html;
61
48
  }
62
49
 
63
- var config$1 = {
64
- versions: {
65
- katex: "0.16.0",
66
- webfontloader: "1.6.28"
67
- },
68
- preloadScripts: [{
69
- type: 'script',
70
- data: {
71
- src: `https://cdn.jsdelivr.net/npm/katex@${"0.16.0"}/dist/katex.min.js`
72
- }
73
- }],
74
- scripts: [{
75
- type: 'iife',
76
- data: {
77
- fn: getMarkmap => {
78
- window.WebFontConfig = {
79
- custom: {
80
- families: ['KaTeX_AMS', 'KaTeX_Caligraphic:n4,n7', 'KaTeX_Fraktur:n4,n7', 'KaTeX_Main:n4,n7,i4,i7', 'KaTeX_Math:i4,i7', 'KaTeX_Script', 'KaTeX_SansSerif:n4,n7,i4', 'KaTeX_Size1', 'KaTeX_Size2', 'KaTeX_Size3', 'KaTeX_Size4', 'KaTeX_Typewriter']
81
- },
82
- active: () => {
83
- getMarkmap().refreshHook.call();
84
- }
85
- };
86
- },
87
-
88
- getParams({
89
- getMarkmap
90
- }) {
91
- return [getMarkmap];
50
+ function getConfig$1() {
51
+ const preloadScripts = [`katex@${"0.16.0"}/dist/katex.min.js`].map(markmapCommon.buildJSItem);
52
+ const webfontloader = markmapCommon.buildJSItem(`webfontloader@${"1.6.28"}/webfontloader.js`);
53
+ webfontloader.data.defer = true;
54
+ const styles = [`katex@${"0.16.0"}/dist/katex.min.css`].map(markmapCommon.buildCSSItem);
55
+ return {
56
+ versions: {
57
+ katex: "0.16.0",
58
+ webfontloader: "1.6.28"
59
+ },
60
+ preloadScripts,
61
+ scripts: [{
62
+ type: 'iife',
63
+ data: {
64
+ fn: getMarkmap => {
65
+ window.WebFontConfig = {
66
+ custom: {
67
+ families: ['KaTeX_AMS', 'KaTeX_Caligraphic:n4,n7', 'KaTeX_Fraktur:n4,n7', 'KaTeX_Main:n4,n7,i4,i7', 'KaTeX_Math:i4,i7', 'KaTeX_Script', 'KaTeX_SansSerif:n4,n7,i4', 'KaTeX_Size1', 'KaTeX_Size2', 'KaTeX_Size3', 'KaTeX_Size4', 'KaTeX_Typewriter']
68
+ },
69
+ active: () => {
70
+ getMarkmap().refreshHook.call();
71
+ }
72
+ };
73
+ },
74
+ getParams({
75
+ getMarkmap
76
+ }) {
77
+ return [getMarkmap];
78
+ }
92
79
  }
93
-
94
- }
95
- }, {
96
- type: 'script',
97
- data: {
98
- src: `https://cdn.jsdelivr.net/npm/webfontloader@${"1.6.28"}/webfontloader.js`,
99
- defer: true
100
- }
101
- }],
102
- styles: [{
103
- type: 'stylesheet',
104
- data: {
105
- href: `https://cdn.jsdelivr.net/npm/katex@${"0.16.0"}/dist/katex.min.css`
106
- }
107
- }]
108
- };
80
+ }, webfontloader],
81
+ styles
82
+ };
83
+ }
109
84
 
110
85
  function createTransformHooks() {
111
86
  return {
@@ -116,149 +91,119 @@ function createTransformHooks() {
116
91
  retransform: new markmapCommon.Hook()
117
92
  };
118
93
  }
94
+
119
95
  /**
120
96
  * This function is only used to help type checking.
121
97
  */
122
-
123
98
  function definePlugin(plugin) {
124
99
  return plugin;
125
100
  }
126
101
 
127
- const name$2 = 'katex';
128
- var katex = definePlugin({
129
- name: name$2,
130
- config: config$1,
131
-
132
- transform(transformHooks) {
133
- let enableFeature = () => {};
134
-
135
- transformHooks.parser.tap(md => {
136
- md.use(remarkableKatex__default);
137
- md.renderer.rules.katex = markmapCommon.wrapFunction(md.renderer.rules.katex, {
138
- after: () => {
102
+ const name$3 = 'katex';
103
+ var katex = definePlugin(() => {
104
+ const plugin = {
105
+ name: name$3,
106
+ config: getConfig$1(),
107
+ transform(transformHooks) {
108
+ let enableFeature = markmapCommon.noop;
109
+ transformHooks.parser.tap(md => {
110
+ md.use(remarkableKatex);
111
+ md.renderer.rules.katex = markmapCommon.wrapFunction(md.renderer.rules.katex, (render, ...args) => {
139
112
  enableFeature();
140
- }
113
+ return render(...args);
114
+ });
141
115
  });
142
- });
143
- transformHooks.beforeParse.tap((_, context) => {
144
- enableFeature = () => {
145
- context.features[name$2] = true;
116
+ transformHooks.beforeParse.tap((_, context) => {
117
+ enableFeature = () => {
118
+ context.features[name$3] = true;
119
+ };
120
+ });
121
+ return {
122
+ styles: plugin.config.styles,
123
+ scripts: plugin.config.scripts
146
124
  };
147
- });
148
- return {
149
- styles: config$1.styles,
150
- scripts: config$1.scripts
151
- };
152
- }
153
-
154
- });
155
-
156
- var config = {
157
- versions: {
158
- prismjs: "1.28.0"
159
- },
160
- preloadScripts: [{
161
- type: 'script',
162
- data: {
163
- src: `https://cdn.jsdelivr.net/npm/prismjs@${"1.28.0"}/components/prism-core.min.js`
164
- }
165
- }, {
166
- type: 'script',
167
- data: {
168
- src: `https://cdn.jsdelivr.net/npm/prismjs@${"1.28.0"}/plugins/autoloader/prism-autoloader.min.js`
169
- }
170
- }],
171
- styles: [{
172
- type: 'stylesheet',
173
- data: {
174
- href: `https://cdn.jsdelivr.net/npm/prismjs@${"1.28.0"}/themes/prism.css`
175
125
  }
176
- }]
177
- };
178
-
179
- const name$1 = 'prism';
180
- var prism = definePlugin({
181
- name: name$1,
182
- config,
183
-
184
- transform(transformHooks) {
185
- let enableFeature = () => {};
186
-
187
- transformHooks.parser.tap(md => {
188
- md.set({
189
- highlight: (str, lang) => {
190
- enableFeature();
191
- let grammar = Prism__default.languages[lang];
126
+ };
127
+ return plugin;
128
+ });
192
129
 
193
- if (!grammar && lang) {
194
- loadLanguages__default([lang]);
195
- grammar = Prism__default.languages[lang];
196
- }
130
+ function getConfig() {
131
+ const preloadScripts = [`prismjs@${"1.28.0"}/components/prism-core.min.js`, `prismjs@${"1.28.0"}/plugins/autoloader/prism-autoloader.min.js`].map(markmapCommon.buildJSItem);
132
+ const styles = [`prismjs@${"1.28.0"}/themes/prism.css`].map(markmapCommon.buildCSSItem);
133
+ return {
134
+ versions: {
135
+ prismjs: "1.28.0"
136
+ },
137
+ preloadScripts,
138
+ styles
139
+ };
140
+ }
197
141
 
198
- if (grammar) {
199
- return Prism__default.highlight(str, grammar, lang);
142
+ const name$2 = 'prism';
143
+ var prism = definePlugin(() => {
144
+ const plugin = {
145
+ name: name$2,
146
+ config: getConfig(),
147
+ transform(transformHooks) {
148
+ let enableFeature = markmapCommon.noop;
149
+ transformHooks.parser.tap(md => {
150
+ md.set({
151
+ highlight: (str, lang) => {
152
+ enableFeature();
153
+ let grammar = Prism.languages[lang];
154
+ if (!grammar && lang) {
155
+ loadLanguages([lang]);
156
+ grammar = Prism.languages[lang];
157
+ }
158
+ if (grammar) {
159
+ return Prism.highlight(str, grammar, lang);
160
+ }
161
+ return '';
200
162
  }
201
-
202
- return '';
203
- }
163
+ });
204
164
  });
205
- });
206
- transformHooks.beforeParse.tap((_, context) => {
207
- enableFeature = () => {
208
- context.features[name$1] = true;
165
+ transformHooks.beforeParse.tap((_, context) => {
166
+ enableFeature = () => {
167
+ context.features[name$2] = true;
168
+ };
169
+ });
170
+ return {
171
+ styles: plugin.config.styles
209
172
  };
210
- });
211
- return {
212
- styles: config.styles
213
- };
214
- }
215
-
173
+ }
174
+ };
175
+ return plugin;
216
176
  });
217
177
 
218
- const name = 'frontmatter';
178
+ const name$1 = 'frontmatter';
219
179
  var frontmatter = definePlugin({
220
- name,
221
-
180
+ name: name$1,
222
181
  transform(transformHooks) {
223
182
  transformHooks.beforeParse.tap((md, context) => {
224
- const origParse = md.parse;
225
- md.parse = markmapCommon.wrapFunction(origParse, {
226
- before(ctx) {
227
- const [content] = ctx.args;
228
- if (!content.startsWith('---\n')) return;
229
- const endOffset = content.indexOf('\n---\n');
230
- if (endOffset < 0) return;
231
- const raw = content.slice(4, endOffset);
232
- let frontmatter;
233
-
234
- try {
235
- var _frontmatter;
236
-
237
- frontmatter = yaml__default.load(raw);
238
-
239
- if ((_frontmatter = frontmatter) != null && _frontmatter.markmap) {
240
- frontmatter.markmap = normalizeMarkmapJsonOptions(frontmatter.markmap);
241
- }
242
- } catch (_unused) {
243
- return;
244
- }
245
-
246
- context.frontmatter = frontmatter;
247
- const offset = endOffset + 5;
248
- ctx.args[0] = content.slice(offset);
249
- },
250
-
251
- after() {
252
- md.parse = origParse;
183
+ const {
184
+ content
185
+ } = context;
186
+ if (!content.startsWith('---\n')) return;
187
+ const endOffset = content.indexOf('\n---\n');
188
+ if (endOffset < 0) return;
189
+ const raw = content.slice(4, endOffset);
190
+ let frontmatter;
191
+ try {
192
+ var _frontmatter;
193
+ frontmatter = yaml.load(raw);
194
+ if ((_frontmatter = frontmatter) != null && _frontmatter.markmap) {
195
+ frontmatter.markmap = normalizeMarkmapJsonOptions(frontmatter.markmap);
253
196
  }
254
-
255
- });
197
+ } catch (_unused) {
198
+ return;
199
+ }
200
+ context.frontmatter = frontmatter;
201
+ context.content = content.slice(endOffset + 5);
202
+ context.contentLineOffset = content.slice(0, endOffset).split('\n').length + 1;
256
203
  });
257
204
  return {};
258
205
  }
259
-
260
206
  });
261
-
262
207
  function normalizeMarkmapJsonOptions(options) {
263
208
  if (!options) return;
264
209
  ['color', 'extraJs', 'extraCss'].forEach(key => {
@@ -269,21 +214,43 @@ function normalizeMarkmapJsonOptions(options) {
269
214
  });
270
215
  return options;
271
216
  }
272
-
273
217
  function normalizeStringArray(value) {
274
218
  var _result;
275
-
276
219
  let result;
277
220
  if (typeof value === 'string') result = [value];else if (Array.isArray(value)) result = value.filter(item => item && typeof item === 'string');
278
221
  return (_result = result) != null && _result.length ? result : undefined;
279
222
  }
280
-
281
223
  function normalizeNumber(value) {
282
224
  if (isNaN(+value)) return;
283
225
  return +value;
284
226
  }
285
227
 
286
- const plugins = [frontmatter, katex, prism];
228
+ const name = 'npmUrl';
229
+ var npmUrl = definePlugin({
230
+ name,
231
+ transform(transformHooks) {
232
+ transformHooks.afterParse.tap((_, context) => {
233
+ const {
234
+ frontmatter
235
+ } = context;
236
+ if (frontmatter != null && frontmatter.markmap) {
237
+ ['extraJs', 'extraCss'].forEach(key => {
238
+ if (frontmatter.markmap[key]) {
239
+ frontmatter.markmap[key] = frontmatter.markmap[key].map(path => {
240
+ if (path.startsWith('npm:')) {
241
+ return markmapCommon.getFullUrl(path.slice(4));
242
+ }
243
+ return path;
244
+ });
245
+ }
246
+ });
247
+ }
248
+ });
249
+ return {};
250
+ }
251
+ });
252
+
253
+ const plugins = [frontmatter, katex, prism, npmUrl];
287
254
 
288
255
  function cleanNode(node) {
289
256
  if (node.type === 'heading') {
@@ -291,87 +258,77 @@ function cleanNode(node) {
291
258
  node.children = node.children.filter(item => item.type !== 'paragraph');
292
259
  } else if (node.type === 'list_item') {
293
260
  var _node$payload;
294
-
295
261
  // keep first paragraph as content of list_item, drop others
296
262
  node.children = node.children.filter(item => {
297
263
  if (['paragraph', 'fence'].includes(item.type)) {
298
264
  if (!node.content) {
299
265
  node.content = item.content;
300
- node.payload = _extends__default({}, node.payload, item.payload);
266
+ node.payload = _extends({}, node.payload, item.payload);
301
267
  }
302
-
303
268
  return false;
304
269
  }
305
-
306
270
  return true;
307
271
  });
308
-
309
272
  if (((_node$payload = node.payload) == null ? void 0 : _node$payload.index) != null) {
310
273
  node.content = `${node.payload.index}. ${node.content}`;
311
274
  }
312
275
  } else if (node.type === 'ordered_list') {
313
276
  var _node$payload$startIn, _node$payload2;
314
-
315
277
  let index = (_node$payload$startIn = (_node$payload2 = node.payload) == null ? void 0 : _node$payload2.startIndex) != null ? _node$payload$startIn : 1;
316
278
  node.children.forEach(item => {
317
279
  if (item.type === 'list_item') {
318
- item.payload = _extends__default({}, item.payload, {
280
+ item.payload = _extends({}, item.payload, {
319
281
  index
320
282
  });
321
283
  index += 1;
322
284
  }
323
285
  });
324
286
  }
325
-
326
287
  if (node.children.length === 0) {
327
288
  delete node.children;
328
289
  } else {
329
290
  node.children.forEach(child => cleanNode(child));
330
-
331
291
  if (node.children.length === 1 && !node.children[0].content) {
332
292
  node.children = node.children[0].children;
333
293
  }
334
294
  }
335
295
  }
336
-
337
296
  function resetDepth(node, depth = 0) {
338
297
  var _node$children;
339
-
340
298
  node.depth = depth;
341
299
  (_node$children = node.children) == null ? void 0 : _node$children.forEach(child => {
342
300
  resetDepth(child, depth + 1);
343
301
  });
344
302
  }
345
-
346
303
  class Transformer {
347
304
  constructor(plugins$1 = plugins) {
348
305
  this.assetsMap = {};
349
- this.plugins = plugins$1;
350
306
  this.hooks = createTransformHooks();
307
+ this.plugins = plugins$1.map(plugin => typeof plugin === 'function' ? plugin() : plugin);
351
308
  const assetsMap = {};
352
-
353
309
  for (const {
354
310
  name,
355
311
  transform
356
- } of plugins$1) {
312
+ } of this.plugins) {
357
313
  assetsMap[name] = transform(this.hooks);
358
314
  }
359
-
360
315
  this.assetsMap = assetsMap;
361
316
  const md = new remarkable.Remarkable('full', {
362
317
  html: true,
363
318
  breaks: true,
364
319
  maxNesting: Infinity
365
320
  });
366
- md.renderer.rules.htmltag = markmapCommon.wrapFunction(md.renderer.rules.htmltag, {
367
- after: ctx => {
368
- this.hooks.htmltag.call(ctx);
369
- }
321
+ md.renderer.rules.htmltag = markmapCommon.wrapFunction(md.renderer.rules.htmltag, (render, ...args) => {
322
+ const result = render(...args);
323
+ this.hooks.htmltag.call({
324
+ args,
325
+ result
326
+ });
327
+ return result;
370
328
  });
371
329
  this.md = md;
372
330
  this.hooks.parser.call(md);
373
331
  }
374
-
375
332
  buildTree(tokens) {
376
333
  const {
377
334
  md
@@ -385,37 +342,28 @@ class Transformer {
385
342
  };
386
343
  const stack = [root];
387
344
  let depth = 0;
388
-
389
345
  for (const token of tokens) {
390
346
  let current = stack[stack.length - 1];
391
-
392
347
  if (token.type.endsWith('_open')) {
393
348
  const type = token.type.slice(0, -5);
394
349
  const payload = {};
395
-
396
350
  if (token.lines) {
397
351
  payload.lines = token.lines;
398
352
  }
399
-
400
353
  if (type === 'heading') {
401
354
  depth = token.hLevel;
402
-
403
355
  while (((_current = current) == null ? void 0 : _current.depth) >= depth) {
404
356
  var _current;
405
-
406
357
  stack.pop();
407
358
  current = stack[stack.length - 1];
408
359
  }
409
360
  } else {
410
361
  var _current2;
411
-
412
362
  depth = Math.max(depth, ((_current2 = current) == null ? void 0 : _current2.depth) || 0) + 1;
413
-
414
363
  if (type === 'ordered_list') {
415
364
  payload.startIndex = token.order;
416
365
  }
417
366
  }
418
-
419
367
  const item = {
420
368
  type,
421
369
  depth,
@@ -438,7 +386,6 @@ class Transformer {
438
386
  const revoke = this.hooks.htmltag.tap(ctx => {
439
387
  const comment = ctx.result.match(/^<!--([\s\S]*?)-->$/);
440
388
  const data = comment == null ? void 0 : comment[1].trim().split(' ');
441
-
442
389
  if (data[0] === 'fold') {
443
390
  current.payload.fold = ['all', 'recursively'].includes(data[1]) ? 2 : 1;
444
391
  ctx.result = '';
@@ -448,8 +395,8 @@ class Transformer {
448
395
  revoke();
449
396
  current.content = `${current.content || ''}${text}`;
450
397
  } else if (token.type === 'fence') {
451
- let result = md.renderer.render([token], md.options, {}); // Remarkable only adds className to `<code>` but not `<pre>`, copy it to make PrismJS style work.
452
-
398
+ let result = md.renderer.render([token], md.options, {});
399
+ // Remarkable only adds className to `<code>` but not `<pre>`, copy it to make PrismJS style work.
453
400
  const matches = result.match(/<code( class="[^"]*")>/);
454
401
  if (matches) result = result.replace('<pre>', `<pre${matches[1]}>`);
455
402
  current.children.push({
@@ -460,69 +407,63 @@ class Transformer {
460
407
  });
461
408
  } else ;
462
409
  }
463
-
464
410
  return root;
465
411
  }
466
-
467
412
  transform(content) {
468
413
  var _root$children;
469
-
470
414
  const context = {
471
- features: {}
415
+ content,
416
+ features: {},
417
+ contentLineOffset: 0
472
418
  };
473
419
  this.hooks.beforeParse.call(this.md, context);
474
- const tokens = this.md.parse(content, {});
420
+ const tokens = this.md.parse(context.content, {});
475
421
  this.hooks.afterParse.call(this.md, context);
476
422
  let root = this.buildTree(tokens);
477
423
  cleanNode(root);
478
424
  if (((_root$children = root.children) == null ? void 0 : _root$children.length) === 1) root = root.children[0];
479
425
  resetDepth(root);
480
- return _extends__default({}, context, {
426
+ return _extends({}, context, {
481
427
  root
482
428
  });
483
429
  }
430
+
484
431
  /**
485
432
  * Get all assets from enabled plugins or filter them by plugin names as keys.
486
433
  */
487
-
488
-
489
434
  getAssets(keys) {
490
435
  var _keys;
491
-
492
436
  const styles = [];
493
437
  const scripts = [];
494
438
  (_keys = keys) != null ? _keys : keys = this.plugins.map(plugin => plugin.name);
495
-
496
439
  for (const assets of keys.map(key => this.assetsMap[key])) {
497
440
  if (assets) {
498
441
  if (assets.styles) styles.push(...assets.styles);
499
442
  if (assets.scripts) scripts.push(...assets.scripts);
500
443
  }
501
444
  }
502
-
503
445
  return {
504
446
  styles,
505
447
  scripts
506
448
  };
507
449
  }
450
+
508
451
  /**
509
452
  * Get used assets by features object returned by `transform`.
510
453
  */
511
-
512
-
513
454
  getUsedAssets(features) {
514
455
  const keys = this.plugins.map(plugin => plugin.name).filter(name => features[name]);
515
456
  return this.getAssets(keys);
516
457
  }
517
-
518
458
  }
519
459
 
520
460
  const transformerVersions = {
521
- 'markmap-lib': '0.14.4-alpha.1+8a57fea',
522
- d3: "6.7.0"
461
+ 'markmap-lib': '0.15.0-alpha.0',
462
+ d3: "6.2.0"
523
463
  };
524
464
 
525
465
  exports.Transformer = Transformer;
466
+ exports.baseJsPaths = baseJsPaths;
526
467
  exports.builtInPlugins = plugins;
527
468
  exports.fillTemplate = fillTemplate;
528
469
  exports.transformerVersions = transformerVersions;