markmap-lib 0.11.6 → 0.13.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.esm.js CHANGED
@@ -1,25 +1,23 @@
1
- /*! markmap-lib v0.11.6 | MIT License */
1
+ /*! markmap-lib v0.13.0 | MIT License */
2
2
  import _extends from '@babel/runtime/helpers/esm/extends';
3
3
  import { persistCSS, persistJS, wrapFunction, Hook } from 'markmap-common';
4
4
  import { Remarkable } from 'remarkable';
5
5
  import remarkableKatex from 'remarkable-katex';
6
6
  import Prism from 'prismjs';
7
7
  import loadLanguages from 'prismjs/components/';
8
+ import yaml from 'js-yaml';
8
9
 
9
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";
10
- const BASE_JS = [`https://cdn.jsdelivr.net/npm/d3@${"6.6.0"}`, `https://cdn.jsdelivr.net/npm/markmap-view@${"0.2.6"}`].map(src => ({
11
+ const BASE_JS = [`https://cdn.jsdelivr.net/npm/d3@${"6.7.0"}`, `https://cdn.jsdelivr.net/npm/markmap-view@${"0.13.0"}`].map(src => ({
11
12
  type: 'script',
12
13
  data: {
13
14
  src
14
15
  }
15
16
  }));
16
- function fillTemplate(data, assets, extra) {
17
- if (typeof extra === 'function') {
18
- extra = {
19
- getOptions: extra
20
- };
21
- }
22
17
 
18
+ const defaultGetOptions = (markmap, jsonOptions) => markmap.deriveOptions(jsonOptions);
19
+
20
+ function fillTemplate(root, assets, extra) {
23
21
  extra = _extends({
24
22
  baseJs: BASE_JS
25
23
  }, extra);
@@ -30,24 +28,24 @@ function fillTemplate(data, assets, extra) {
30
28
  const cssList = [...(styles ? persistCSS(styles) : [])];
31
29
  const context = {
32
30
  getMarkmap: () => window.markmap,
33
- getOptions: extra.getOptions,
34
- data
31
+ getOptions: extra.getOptions || defaultGetOptions,
32
+ jsonOptions: extra.jsonOptions,
33
+ root
35
34
  };
36
- const jsList = [...persistJS(extra.baseJs), ...persistJS([...(scripts || []), {
35
+ const jsList = [...persistJS([...extra.baseJs, ...(scripts || []), {
37
36
  type: 'iife',
38
37
  data: {
39
- fn: (getMarkmap, getOptions, data) => {
40
- const {
41
- Markmap
42
- } = getMarkmap();
43
- window.mm = Markmap.create('svg#mindmap', getOptions == null ? void 0 : getOptions(), data);
38
+ fn: (getMarkmap, getOptions, root, jsonOptions) => {
39
+ const markmap = getMarkmap();
40
+ window.mm = markmap.Markmap.create('svg#mindmap', getOptions == null ? void 0 : getOptions(markmap, jsonOptions), root);
44
41
  },
45
42
  getParams: ({
46
43
  getMarkmap,
47
44
  getOptions,
48
- data
45
+ root,
46
+ jsonOptions
49
47
  }) => {
50
- return [getMarkmap, getOptions, data];
48
+ return [getMarkmap, getOptions, root, jsonOptions];
51
49
  }
52
50
  }
53
51
  }], context)];
@@ -55,16 +53,23 @@ function fillTemplate(data, assets, extra) {
55
53
  return html;
56
54
  }
57
55
 
58
- const name$1 = 'katex';
59
- function transform$1(transformHooks) {
60
- transformHooks.parser.tap((md, features) => {
56
+ const name$2 = 'katex';
57
+ function transform$2(transformHooks) {
58
+ let enableFeature = () => {};
59
+
60
+ transformHooks.parser.tap(md => {
61
61
  md.use(remarkableKatex);
62
62
  md.renderer.rules.katex = wrapFunction(md.renderer.rules.katex, {
63
63
  after: () => {
64
- features[name$1] = true;
64
+ enableFeature();
65
65
  }
66
66
  });
67
67
  });
68
+ transformHooks.transform.tap((_, context) => {
69
+ enableFeature = () => {
70
+ context.features[name$2] = true;
71
+ };
72
+ });
68
73
  return {
69
74
  styles: [{
70
75
  type: 'stylesheet',
@@ -105,19 +110,21 @@ function transform$1(transformHooks) {
105
110
 
106
111
  var katex = /*#__PURE__*/Object.freeze({
107
112
  __proto__: null,
108
- name: name$1,
109
- transform: transform$1
113
+ name: name$2,
114
+ transform: transform$2
110
115
  });
111
116
 
112
- const name = 'prism';
113
- function transform(transformHooks) {
114
- transformHooks.parser.tap((md, features) => {
117
+ const name$1 = 'prism';
118
+ function transform$1(transformHooks) {
119
+ let enableFeature = () => {};
120
+
121
+ transformHooks.parser.tap(md => {
115
122
  md.set({
116
123
  highlight: (str, lang) => {
117
- features[name] = true;
124
+ enableFeature();
118
125
  let grammar = Prism.languages[lang];
119
126
 
120
- if (!grammar) {
127
+ if (!grammar && lang) {
121
128
  loadLanguages([lang]);
122
129
  grammar = Prism.languages[lang];
123
130
  }
@@ -130,11 +137,16 @@ function transform(transformHooks) {
130
137
  }
131
138
  });
132
139
  });
140
+ transformHooks.transform.tap((_, context) => {
141
+ enableFeature = () => {
142
+ context.features[name$1] = true;
143
+ };
144
+ });
133
145
  return {
134
146
  styles: [{
135
147
  type: 'stylesheet',
136
148
  data: {
137
- href: `https://cdn.jsdelivr.net/npm/prismjs@${"1.23.0"}/themes/prism.css`
149
+ href: `https://cdn.jsdelivr.net/npm/prismjs@${"1.28.0"}/themes/prism.css`
138
150
  }
139
151
  }]
140
152
  };
@@ -142,6 +154,43 @@ function transform(transformHooks) {
142
154
 
143
155
  var prism = /*#__PURE__*/Object.freeze({
144
156
  __proto__: null,
157
+ name: name$1,
158
+ transform: transform$1
159
+ });
160
+
161
+ const name = 'frontmatter';
162
+ function transform(transformHooks) {
163
+ transformHooks.transform.tap((md, context) => {
164
+ const origParse = md.parse;
165
+ md.parse = wrapFunction(origParse, {
166
+ before(ctx) {
167
+ const [content] = ctx.args;
168
+ if (!content.startsWith('---\n')) return;
169
+ const endOffset = content.indexOf('\n---\n');
170
+ if (endOffset < 0) return;
171
+ const raw = content.slice(4, endOffset);
172
+
173
+ try {
174
+ context.frontmatter = yaml.load(raw);
175
+ } catch (_unused) {
176
+ return;
177
+ }
178
+
179
+ const offset = endOffset + 5;
180
+ ctx.args[0] = content.slice(offset);
181
+ },
182
+
183
+ after() {
184
+ md.parse = origParse;
185
+ }
186
+
187
+ });
188
+ });
189
+ return {};
190
+ }
191
+
192
+ var frontmatter = /*#__PURE__*/Object.freeze({
193
+ __proto__: null,
145
194
  name: name,
146
195
  transform: transform
147
196
  });
@@ -149,31 +198,27 @@ transform: transform
149
198
  function createTransformHooks() {
150
199
  return {
151
200
  parser: new Hook(),
201
+ transform: new Hook(),
152
202
  htmltag: new Hook(),
153
-
154
- /**
155
- * Indicate that the last transformation is not complete for reasons like
156
- * lack of resources and is called when it is ready for a new transformation.
157
- */
158
203
  retransform: new Hook()
159
204
  };
160
205
  }
161
206
 
162
- const plugins = [katex, prism];
207
+ const plugins = [katex, prism, frontmatter];
163
208
 
164
209
  function cleanNode(node, depth = 0) {
165
- if (node.t === 'heading') {
210
+ if (node.type === 'heading') {
166
211
  // drop all paragraphs
167
- node.c = node.c.filter(item => item.t !== 'paragraph');
168
- } else if (node.t === 'list_item') {
169
- var _node$p;
212
+ node.children = node.children.filter(item => item.type !== 'paragraph');
213
+ } else if (node.type === 'list_item') {
214
+ var _node$payload;
170
215
 
171
216
  // keep first paragraph as content of list_item, drop others
172
- node.c = node.c.filter(item => {
173
- if (['paragraph', 'fence'].includes(item.t)) {
174
- if (!node.v) {
175
- node.v = item.v;
176
- node.p = _extends({}, node.p, item.p);
217
+ node.children = node.children.filter(item => {
218
+ if (['paragraph', 'fence'].includes(item.type)) {
219
+ if (!node.content) {
220
+ node.content = item.content;
221
+ node.payload = _extends({}, node.payload, item.payload);
177
222
  }
178
223
 
179
224
  return false;
@@ -182,16 +227,16 @@ function cleanNode(node, depth = 0) {
182
227
  return true;
183
228
  });
184
229
 
185
- if (((_node$p = node.p) == null ? void 0 : _node$p.index) != null) {
186
- node.v = `${node.p.index}. ${node.v}`;
230
+ if (((_node$payload = node.payload) == null ? void 0 : _node$payload.index) != null) {
231
+ node.content = `${node.payload.index}. ${node.content}`;
187
232
  }
188
- } else if (node.t === 'ordered_list') {
189
- var _node$p$start, _node$p2;
233
+ } else if (node.type === 'ordered_list') {
234
+ var _node$payload$startIn, _node$payload2;
190
235
 
191
- let index = (_node$p$start = (_node$p2 = node.p) == null ? void 0 : _node$p2.start) != null ? _node$p$start : 1;
192
- node.c.forEach(item => {
193
- if (item.t === 'list_item') {
194
- item.p = _extends({}, item.p, {
236
+ let index = (_node$payload$startIn = (_node$payload2 = node.payload) == null ? void 0 : _node$payload2.startIndex) != null ? _node$payload$startIn : 1;
237
+ node.children.forEach(item => {
238
+ if (item.type === 'list_item') {
239
+ item.payload = _extends({}, item.payload, {
195
240
  index
196
241
  });
197
242
  index += 1;
@@ -199,39 +244,23 @@ function cleanNode(node, depth = 0) {
199
244
  });
200
245
  }
201
246
 
202
- if (node.c.length === 0) {
203
- delete node.c;
247
+ if (node.children.length === 0) {
248
+ delete node.children;
204
249
  } else {
205
- node.c.forEach(child => cleanNode(child, depth + 1));
250
+ node.children.forEach(child => cleanNode(child, depth + 1));
206
251
 
207
- if (node.c.length === 1 && !node.c[0].v) {
208
- node.c = node.c[0].c;
252
+ if (node.children.length === 1 && !node.children[0].content) {
253
+ node.children = node.children[0].children;
209
254
  }
210
255
  }
211
256
 
212
- node.d = depth;
257
+ node.depth = depth;
213
258
  }
214
259
 
215
260
  class Transformer {
216
261
  constructor(plugins$1 = plugins) {
217
- this.plugins = void 0;
218
- this.hooks = void 0;
219
- this.md = void 0;
220
- this.assetsMap = void 0;
221
262
  this.plugins = plugins$1;
222
263
  this.hooks = createTransformHooks();
223
- const md = new Remarkable({
224
- html: true,
225
- breaks: true,
226
- maxNesting: Infinity
227
- });
228
- md.block.ruler.enable(['deflist']);
229
- md.renderer.rules.htmltag = wrapFunction(md.renderer.rules.htmltag, {
230
- after: ctx => {
231
- this.hooks.htmltag.call(ctx);
232
- }
233
- });
234
- this.md = md;
235
264
  const assetsMap = {};
236
265
 
237
266
  for (const {
@@ -242,19 +271,30 @@ class Transformer {
242
271
  }
243
272
 
244
273
  this.assetsMap = assetsMap;
274
+ const md = new Remarkable('full', {
275
+ html: true,
276
+ breaks: true,
277
+ maxNesting: Infinity
278
+ });
279
+ md.renderer.rules.htmltag = wrapFunction(md.renderer.rules.htmltag, {
280
+ after: ctx => {
281
+ this.hooks.htmltag.call(ctx);
282
+ }
283
+ });
284
+ this.md = md;
285
+ this.hooks.parser.call(md);
245
286
  }
246
287
 
247
288
  buildTree(tokens) {
248
289
  const {
249
290
  md
250
- } = this; // TODO deal with <dl><dt>
251
-
291
+ } = this;
252
292
  const root = {
253
- t: 'root',
254
- d: 0,
255
- v: '',
256
- c: [],
257
- p: {}
293
+ type: 'root',
294
+ depth: 0,
295
+ content: '',
296
+ children: [],
297
+ payload: {}
258
298
  };
259
299
  const stack = [root];
260
300
  let depth = 0;
@@ -273,7 +313,7 @@ class Transformer {
273
313
  if (type === 'heading') {
274
314
  depth = token.hLevel;
275
315
 
276
- while (((_current = current) == null ? void 0 : _current.d) >= depth) {
316
+ while (((_current = current) == null ? void 0 : _current.depth) >= depth) {
277
317
  var _current;
278
318
 
279
319
  stack.pop();
@@ -282,27 +322,27 @@ class Transformer {
282
322
  } else {
283
323
  var _current2;
284
324
 
285
- depth = Math.max(depth, ((_current2 = current) == null ? void 0 : _current2.d) || 0) + 1;
325
+ depth = Math.max(depth, ((_current2 = current) == null ? void 0 : _current2.depth) || 0) + 1;
286
326
 
287
327
  if (type === 'ordered_list') {
288
- payload.start = token.order;
328
+ payload.startIndex = token.order;
289
329
  }
290
330
  }
291
331
 
292
332
  const item = {
293
- t: type,
294
- d: depth,
295
- p: payload,
296
- v: '',
297
- c: []
333
+ type,
334
+ depth,
335
+ payload,
336
+ content: '',
337
+ children: []
298
338
  };
299
- current.c.push(item);
339
+ current.children.push(item);
300
340
  stack.push(item);
301
341
  } else if (!current) {
302
342
  continue;
303
- } else if (token.type === `${current.t}_close`) {
304
- if (current.t === 'heading') {
305
- depth = current.d;
343
+ } else if (token.type === `${current.type}_close`) {
344
+ if (current.type === 'heading') {
345
+ depth = current.depth;
306
346
  } else {
307
347
  stack.pop();
308
348
  depth = 0;
@@ -313,23 +353,23 @@ class Transformer {
313
353
  const data = comment == null ? void 0 : comment[1].trim();
314
354
 
315
355
  if (data === 'fold') {
316
- current.p.f = true;
356
+ current.payload.fold = true;
317
357
  ctx.result = '';
318
358
  }
319
359
  });
320
360
  const text = md.renderer.render([token], md.options, {});
321
361
  revoke();
322
- current.v = `${current.v || ''}${text}`;
362
+ current.content = `${current.content || ''}${text}`;
323
363
  } else if (token.type === 'fence') {
324
364
  let result = md.renderer.render([token], md.options, {}); // Remarkable only adds className to `<code>` but not `<pre>`, copy it to make PrismJS style work.
325
365
 
326
366
  const matches = result.match(/<code( class="[^"]*")>/);
327
367
  if (matches) result = result.replace('<pre>', `<pre${matches[1]}>`);
328
- current.c.push({
329
- t: token.type,
330
- d: depth + 1,
331
- v: result,
332
- c: []
368
+ current.children.push({
369
+ type: token.type,
370
+ depth: depth + 1,
371
+ content: result,
372
+ children: []
333
373
  });
334
374
  } else ;
335
375
  }
@@ -338,18 +378,19 @@ class Transformer {
338
378
  }
339
379
 
340
380
  transform(content) {
341
- var _root$c;
381
+ var _root$children;
342
382
 
343
- const features = {};
344
- this.hooks.parser.call(this.md, features);
345
- const tokens = this.md.parse(content || '', {});
383
+ const context = {
384
+ features: {}
385
+ };
386
+ this.hooks.transform.call(this.md, context);
387
+ const tokens = this.md.parse(content, {});
346
388
  let root = this.buildTree(tokens);
347
389
  cleanNode(root);
348
- if (((_root$c = root.c) == null ? void 0 : _root$c.length) === 1) root = root.c[0];
349
- return {
350
- root,
351
- features
352
- };
390
+ if (((_root$children = root.children) == null ? void 0 : _root$children.length) === 1) root = root.children[0];
391
+ return _extends({}, context, {
392
+ root
393
+ });
353
394
  }
354
395
  /**
355
396
  * Get all assets from enabled plugins or filter them by plugin names as keys.