markmap-lib 0.14.1 → 0.14.2-alpha.4

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,7 +1,8 @@
1
- /*! markmap-lib v0.14.1 | MIT License */
1
+ /*! markmap-lib v0.14.2-alpha.4+569dd11 | MIT License */
2
2
  'use strict';
3
3
 
4
- var _extends$1 = require('@babel/runtime/helpers/extends');
4
+ var _extends = require('@babel/runtime/helpers/extends');
5
+ var markmapCommon = require('markmap-common');
5
6
  var remarkable = require('remarkable');
6
7
  var remarkableKatex = require('remarkable-katex');
7
8
  var Prism = require('prismjs');
@@ -10,182 +11,14 @@ var yaml = require('js-yaml');
10
11
 
11
12
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
12
13
 
13
- var _extends__default = /*#__PURE__*/_interopDefaultLegacy(_extends$1);
14
+ var _extends__default = /*#__PURE__*/_interopDefaultLegacy(_extends);
14
15
  var remarkableKatex__default = /*#__PURE__*/_interopDefaultLegacy(remarkableKatex);
15
16
  var Prism__default = /*#__PURE__*/_interopDefaultLegacy(Prism);
16
17
  var loadLanguages__default = /*#__PURE__*/_interopDefaultLegacy(loadLanguages);
17
18
  var yaml__default = /*#__PURE__*/_interopDefaultLegacy(yaml);
18
19
 
19
- /*! markmap-common v0.14.0 | MIT License */
20
- class Hook {
21
- constructor() {
22
- this.listeners = [];
23
- }
24
-
25
- tap(fn) {
26
- this.listeners.push(fn);
27
- return () => this.revoke(fn);
28
- }
29
-
30
- revoke(fn) {
31
- const i = this.listeners.indexOf(fn);
32
- if (i >= 0) this.listeners.splice(i, 1);
33
- }
34
-
35
- revokeAll() {
36
- this.listeners.splice(0);
37
- }
38
-
39
- call(...args) {
40
- for (const fn of this.listeners) {
41
- fn(...args);
42
- }
43
- }
44
-
45
- }
46
-
47
- function _extends() {
48
- _extends = Object.assign || function (target) {
49
- for (var i = 1; i < arguments.length; i++) {
50
- var source = arguments[i];
51
-
52
- for (var key in source) {
53
- if (Object.prototype.hasOwnProperty.call(source, key)) {
54
- target[key] = source[key];
55
- }
56
- }
57
- }
58
-
59
- return target;
60
- };
61
-
62
- return _extends.apply(this, arguments);
63
- }
64
-
65
- function _objectWithoutPropertiesLoose(source, excluded) {
66
- if (source == null) return {};
67
- var target = {};
68
- var sourceKeys = Object.keys(source);
69
- var key, i;
70
-
71
- for (i = 0; i < sourceKeys.length; i++) {
72
- key = sourceKeys[i];
73
- if (excluded.indexOf(key) >= 0) continue;
74
- target[key] = source[key];
75
- }
76
-
77
- return target;
78
- }
79
-
80
- const _excluded = ["textContent"];
81
-
82
- function escapeHtml(html) {
83
- return html.replace(/[&<"]/g, m => ({
84
- '&': '&amp;',
85
- '<': '&lt;',
86
- '"': '&quot;'
87
- })[m]);
88
- }
89
-
90
- function escapeScript(content) {
91
- return content.replace(/<(\/script>)/g, '\\x3c$2');
92
- }
93
-
94
- function htmlOpen(tagName, attrs) {
95
- const attrStr = attrs ? Object.entries(attrs).map(([key, value]) => {
96
- if (value == null || value === false) return;
97
- key = ` ${escapeHtml(key)}`;
98
- if (value === true) return key;
99
- return `${key}="${escapeHtml(value)}"`;
100
- }).filter(Boolean).join('') : '';
101
- return `<${tagName}${attrStr}>`;
102
- }
103
-
104
- function htmlClose(tagName) {
105
- return `</${tagName}>`;
106
- }
107
-
108
- function wrapHtml(tagName, content, attrs) {
109
- if (content == null) return htmlOpen(tagName, attrs);
110
- return htmlOpen(tagName, attrs) + (content || '') + htmlClose(tagName);
111
- }
112
-
113
- function buildCode(fn, args) {
114
- const params = args.map(arg => {
115
- if (typeof arg === 'function') return arg.toString();
116
- return JSON.stringify(arg != null ? arg : null);
117
- }).join(',');
118
- return `(${fn.toString()})(${params})`;
119
- }
120
-
121
- function persistJS(items, context) {
122
- return items.map(item => {
123
- if (item.type === 'script') {
124
- const _item$data = item.data,
125
- {
126
- textContent
127
- } = _item$data,
128
- rest = _objectWithoutPropertiesLoose(_item$data, _excluded);
129
-
130
- return wrapHtml('script', textContent || '', rest);
131
- }
132
-
133
- if (item.type === 'iife') {
134
- const {
135
- fn,
136
- getParams
137
- } = item.data;
138
- return wrapHtml('script', escapeScript(buildCode(fn, (getParams == null ? void 0 : getParams(context)) || [])));
139
- }
140
-
141
- return '';
142
- });
143
- }
144
-
145
- function persistCSS(items) {
146
- return items.map(item => {
147
- if (item.type === 'stylesheet') {
148
- return wrapHtml('link', null, _extends({
149
- rel: 'stylesheet'
150
- }, item.data));
151
- }
152
- /* else if (item.type === 'style') */
153
-
154
-
155
- return wrapHtml('style', item.data);
156
- });
157
- }
158
-
159
- Math.random().toString(36).slice(2, 8);
160
-
161
- function wrapFunction(fn, {
162
- before,
163
- after
164
- }) {
165
- return function wrapped(...args) {
166
- const ctx = {
167
- args,
168
- thisObj: this
169
- };
170
-
171
- try {
172
- if (before) before(ctx);
173
- } catch (_unused) {// ignore
174
- }
175
-
176
- ctx.result = fn.apply(ctx.thisObj, ctx.args);
177
-
178
- try {
179
- if (after) after(ctx);
180
- } catch (_unused2) {// ignore
181
- }
182
-
183
- return ctx.result;
184
- };
185
- }
186
-
187
20
  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";
188
- const BASE_JS = [`https://cdn.jsdelivr.net/npm/d3@${"6.7.0"}`, `https://cdn.jsdelivr.net/npm/markmap-view@${"0.14.0"}`].map(src => ({
21
+ const BASE_JS = [`https://cdn.jsdelivr.net/npm/d3@${"6.7.0"}`, `https://cdn.jsdelivr.net/npm/markmap-view@${"0.14.2-alpha.4+569dd11"}`].map(src => ({
189
22
  type: 'script',
190
23
  data: {
191
24
  src
@@ -199,14 +32,14 @@ function fillTemplate(root, assets, extra) {
199
32
  scripts,
200
33
  styles
201
34
  } = assets;
202
- const cssList = [...(styles ? persistCSS(styles) : [])];
35
+ const cssList = [...(styles ? markmapCommon.persistCSS(styles) : [])];
203
36
  const context = {
204
37
  getMarkmap: () => window.markmap,
205
38
  getOptions: extra.getOptions,
206
39
  jsonOptions: extra.jsonOptions,
207
40
  root
208
41
  };
209
- const jsList = [...persistJS([...extra.baseJs, ...(scripts || []), {
42
+ const jsList = [...markmapCommon.persistJS([...extra.baseJs, ...(scripts || []), {
210
43
  type: 'iife',
211
44
  data: {
212
45
  fn: (getMarkmap, getOptions, root, jsonOptions) => {
@@ -274,66 +107,50 @@ var config$1 = {
274
107
  }]
275
108
  };
276
109
 
277
- const npmPrefix = 'https://cdn.jsdelivr.net/npm/';
110
+ function createTransformHooks() {
111
+ return {
112
+ parser: new markmapCommon.Hook(),
113
+ beforeParse: new markmapCommon.Hook(),
114
+ afterParse: new markmapCommon.Hook(),
115
+ htmltag: new markmapCommon.Hook(),
116
+ retransform: new markmapCommon.Hook()
117
+ };
118
+ }
278
119
  /**
279
- * Find NPM paths and resolve them to full URLs with the same package version as in this library.
120
+ * This function is only used to help type checking.
280
121
  */
281
122
 
282
- function resolveNpmPaths(paths, name, version) {
283
- return paths.map(path => {
284
- if (typeof path === 'string') {
285
- if (path.startsWith(`${name}/`)) {
286
- path = `${npmPrefix}${name}@${version}${path.slice(name.length)}`;
287
- } else if (path.startsWith(`${name}@`)) {
288
- path = `${npmPrefix}${path}`;
289
- }
290
- }
291
-
292
- return path;
293
- });
123
+ function definePlugin(plugin) {
124
+ return plugin;
294
125
  }
295
126
 
296
127
  const name$2 = 'katex';
297
- function transform$2(transformHooks) {
298
- let enableFeature = () => {};
299
-
300
- transformHooks.parser.tap(md => {
301
- md.use(remarkableKatex__default);
302
- md.renderer.rules.katex = wrapFunction(md.renderer.rules.katex, {
303
- after: () => {
304
- enableFeature();
305
- }
306
- });
307
- });
308
- transformHooks.beforeParse.tap((_, context) => {
309
- enableFeature = () => {
310
- context.features[name$2] = true;
311
- };
312
- });
313
- transformHooks.afterParse.tap((_, context) => {
314
- const {
315
- frontmatter
316
- } = context;
317
-
318
- if (frontmatter != null && frontmatter.markmap) {
319
- ['extraJs', 'extraCss'].forEach(key => {
320
- if (frontmatter.markmap[key]) {
321
- frontmatter.markmap[key] = resolveNpmPaths(frontmatter.markmap[key], name$2, config$1.versions.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: () => {
139
+ enableFeature();
322
140
  }
323
141
  });
324
- }
325
- });
326
- return {
327
- styles: config$1.styles,
328
- scripts: config$1.scripts
329
- };
330
- }
142
+ });
143
+ transformHooks.beforeParse.tap((_, context) => {
144
+ enableFeature = () => {
145
+ context.features[name$2] = true;
146
+ };
147
+ });
148
+ return {
149
+ styles: config$1.styles,
150
+ scripts: config$1.scripts
151
+ };
152
+ }
331
153
 
332
- var katex = /*#__PURE__*/Object.freeze({
333
- __proto__: null,
334
- config: config$1,
335
- name: name$2,
336
- transform: transform$2
337
154
  });
338
155
 
339
156
  var config = {
@@ -360,83 +177,87 @@ var config = {
360
177
  };
361
178
 
362
179
  const name$1 = 'prism';
363
- function transform$1(transformHooks) {
364
- let enableFeature = () => {};
365
-
366
- transformHooks.parser.tap(md => {
367
- md.set({
368
- highlight: (str, lang) => {
369
- enableFeature();
370
- let grammar = Prism__default.languages[lang];
371
-
372
- if (!grammar && lang) {
373
- loadLanguages__default([lang]);
374
- grammar = Prism__default.languages[lang];
375
- }
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];
192
+
193
+ if (!grammar && lang) {
194
+ loadLanguages__default([lang]);
195
+ grammar = Prism__default.languages[lang];
196
+ }
376
197
 
377
- if (grammar) {
378
- return Prism__default.highlight(str, grammar, lang);
379
- }
198
+ if (grammar) {
199
+ return Prism__default.highlight(str, grammar, lang);
200
+ }
380
201
 
381
- return '';
382
- }
202
+ return '';
203
+ }
204
+ });
383
205
  });
384
- });
385
- transformHooks.beforeParse.tap((_, context) => {
386
- enableFeature = () => {
387
- context.features[name$1] = true;
206
+ transformHooks.beforeParse.tap((_, context) => {
207
+ enableFeature = () => {
208
+ context.features[name$1] = true;
209
+ };
210
+ });
211
+ return {
212
+ styles: config.styles
388
213
  };
389
- });
390
- return {
391
- styles: config.styles
392
- };
393
- }
214
+ }
394
215
 
395
- var prism = /*#__PURE__*/Object.freeze({
396
- __proto__: null,
397
- config: config,
398
- name: name$1,
399
- transform: transform$1
400
216
  });
401
217
 
402
218
  const name = 'frontmatter';
403
- function transform(transformHooks) {
404
- transformHooks.beforeParse.tap((md, context) => {
405
- const origParse = md.parse;
406
- md.parse = wrapFunction(origParse, {
407
- before(ctx) {
408
- const [content] = ctx.args;
409
- if (!content.startsWith('---\n')) return;
410
- const endOffset = content.indexOf('\n---\n');
411
- if (endOffset < 0) return;
412
- const raw = content.slice(4, endOffset);
413
- let frontmatter;
414
-
415
- try {
416
- var _frontmatter;
417
-
418
- frontmatter = yaml__default.load(raw);
419
-
420
- if ((_frontmatter = frontmatter) != null && _frontmatter.markmap) {
421
- frontmatter.markmap = normalizeMarkmapJsonOptions(frontmatter.markmap);
219
+ var frontmatter = definePlugin({
220
+ name,
221
+
222
+ transform(transformHooks) {
223
+ 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;
422
244
  }
423
- } catch (_unused) {
424
- return;
425
- }
426
245
 
427
- context.frontmatter = frontmatter;
428
- const offset = endOffset + 5;
429
- ctx.args[0] = content.slice(offset);
430
- },
246
+ context.frontmatter = frontmatter;
247
+ const offset = endOffset + 5;
248
+ ctx.args[0] = content.slice(offset);
249
+ },
431
250
 
432
- after() {
433
- md.parse = origParse;
434
- }
251
+ after() {
252
+ md.parse = origParse;
253
+ }
435
254
 
255
+ });
436
256
  });
437
- });
438
- return {};
439
- }
257
+ return {};
258
+ }
259
+
260
+ });
440
261
 
441
262
  function normalizeMarkmapJsonOptions(options) {
442
263
  if (!options) return;
@@ -462,22 +283,6 @@ function normalizeNumber(value) {
462
283
  return +value;
463
284
  }
464
285
 
465
- var frontmatter = /*#__PURE__*/Object.freeze({
466
- __proto__: null,
467
- name: name,
468
- transform: transform
469
- });
470
-
471
- function createTransformHooks() {
472
- return {
473
- parser: new Hook(),
474
- beforeParse: new Hook(),
475
- afterParse: new Hook(),
476
- htmltag: new Hook(),
477
- retransform: new Hook()
478
- };
479
- }
480
-
481
286
  const plugins = [frontmatter, katex, prism];
482
287
 
483
288
  function cleanNode(node) {
@@ -540,6 +345,7 @@ function resetDepth(node, depth = 0) {
540
345
 
541
346
  class Transformer {
542
347
  constructor(plugins$1 = plugins) {
348
+ this.assetsMap = {};
543
349
  this.plugins = plugins$1;
544
350
  this.hooks = createTransformHooks();
545
351
  const assetsMap = {};
@@ -557,7 +363,7 @@ class Transformer {
557
363
  breaks: true,
558
364
  maxNesting: Infinity
559
365
  });
560
- md.renderer.rules.htmltag = wrapFunction(md.renderer.rules.htmltag, {
366
+ md.renderer.rules.htmltag = markmapCommon.wrapFunction(md.renderer.rules.htmltag, {
561
367
  after: ctx => {
562
368
  this.hooks.htmltag.call(ctx);
563
369
  }
@@ -712,7 +518,7 @@ class Transformer {
712
518
  }
713
519
 
714
520
  const transformerVersions = {
715
- 'markmap-lib': '0.14.1',
521
+ 'markmap-lib': '0.14.2-alpha.4+569dd11',
716
522
  d3: "6.7.0"
717
523
  };
718
524
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markmap-lib",
3
- "version": "0.14.1",
3
+ "version": "0.14.2-alpha.4+569dd11",
4
4
  "description": "Visualize your Markdown as mindmaps with Markmap",
5
5
  "author": "Gerald <gera2ld@live.com>",
6
6
  "license": "MIT",
@@ -44,7 +44,7 @@
44
44
  "devDependencies": {
45
45
  "@types/remarkable": "^2.0.3",
46
46
  "markmap-common": "^0.14.0",
47
- "markmap-view": "^0.14.0",
47
+ "markmap-view": "^0.14.2-alpha.4+569dd11",
48
48
  "webfontloader": "^1.6.28"
49
49
  },
50
50
  "dependencies": {
@@ -58,5 +58,5 @@
58
58
  "peerDependencies": {
59
59
  "markmap-common": "*"
60
60
  },
61
- "gitHead": "16b6efac0b7d82e63567968eddd110ac0afe1377"
61
+ "gitHead": "569dd1128d16297708b4fb3789dbc43b082c2ee9"
62
62
  }
@@ -1,2 +1,6 @@
1
- import { ITransformHooks } from '../types';
1
+ import { ITransformHooks, ITransformPlugin } from '../types';
2
2
  export declare function createTransformHooks(): ITransformHooks;
3
+ /**
4
+ * This function is only used to help type checking.
5
+ */
6
+ export declare function definePlugin(plugin: ITransformPlugin): ITransformPlugin;
@@ -1,3 +1,2 @@
1
- import { IAssets, ITransformHooks } from '../types';
2
- export declare const name = "frontmatter";
3
- export declare function transform(transformHooks: ITransformHooks): IAssets;
1
+ declare const _default: import("../types").ITransformPlugin;
2
+ export default _default;
@@ -1,5 +1,2 @@
1
- import * as katex from './katex';
2
- import * as prism from './prism';
3
- import * as frontmatter from './frontmatter';
4
1
  export * from './base';
5
- export declare const plugins: (typeof katex | typeof prism | typeof frontmatter)[];
2
+ export declare const plugins: import("..").ITransformPlugin[];
@@ -1,5 +1,2 @@
1
- import { IAssets, ITransformHooks } from '../types';
2
- import config from './katex.config';
3
- export { config };
4
- export declare const name = "katex";
5
- export declare function transform(transformHooks: ITransformHooks): IAssets;
1
+ declare const _default: import("../types").ITransformPlugin;
2
+ export default _default;
@@ -1,5 +1,2 @@
1
- import { IAssets, ITransformHooks } from '../types';
2
- import config from './katex.config';
3
- export { config };
4
- export declare const name = "katex";
5
- export declare function transform(transformHooks: ITransformHooks): IAssets;
1
+ declare const _default: import("../types").ITransformPlugin;
2
+ export default _default;
@@ -1,5 +1,2 @@
1
- import { IAssets, ITransformHooks } from '../types';
2
- import config from './prism.config';
3
- export { config };
4
- export declare const name = "prism";
5
- export declare function transform(transformHooks: ITransformHooks): IAssets;
1
+ declare const _default: import("../types").ITransformPlugin;
2
+ export default _default;
@@ -1,5 +1,2 @@
1
- import { IAssets, ITransformHooks } from '../types';
2
- import config from './prism.config';
3
- export { config };
4
- export declare const name = "prism";
5
- export declare function transform(transformHooks: ITransformHooks): IAssets;
1
+ declare const _default: import("../types").ITransformPlugin;
2
+ export default _default;
@@ -1,13 +1,13 @@
1
1
  import { Remarkable } from 'remarkable';
2
2
  import { INode } from 'markmap-common';
3
- import { ITransformResult, ITransformPlugin, IAssets, IAssetsMap, ITransformHooks, IFeatures } from './types';
3
+ import { ITransformResult, ITransformPlugin, IAssets, ITransformHooks, IFeatures } from './types';
4
4
  import { plugins as builtInPlugins } from './plugins';
5
5
  export { builtInPlugins };
6
6
  export declare class Transformer {
7
7
  plugins: ITransformPlugin[];
8
8
  hooks: ITransformHooks;
9
9
  md: Remarkable;
10
- assetsMap: IAssetsMap;
10
+ assetsMap: Record<string, IAssets>;
11
11
  constructor(plugins?: ITransformPlugin[]);
12
12
  buildTree(tokens: Remarkable.Token[]): INode;
13
13
  transform(content: string): ITransformResult;
package/types/types.d.ts CHANGED
@@ -38,9 +38,6 @@ export interface IMarkmapCreateOptions {
38
38
  */
39
39
  output?: string;
40
40
  }
41
- export interface IAssetsMap {
42
- [key: string]: IAssets;
43
- }
44
41
  export interface IFeatures {
45
42
  [key: string]: boolean;
46
43
  }
@@ -55,6 +52,13 @@ export interface ITransformResult extends ITransformContext {
55
52
  }
56
53
  export interface ITransformPlugin {
57
54
  name: string;
55
+ config?: IAssets & {
56
+ version?: Record<string, string>;
57
+ preloadScripts?: JSItem[];
58
+ };
59
+ /**
60
+ * @returns The assets that should be loaded for rendering the output.
61
+ */
58
62
  transform: (transformHooks: ITransformHooks) => IAssets;
59
63
  }
60
64
  export {};