markmap-lib 0.15.9-alpha.6 → 0.16.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.
Files changed (36) hide show
  1. package/dist/browser/index.iife.js +38 -150
  2. package/dist/browser/index.mjs +37 -81
  3. package/dist/index.d.ts +1 -3
  4. package/dist/index.js +17 -352
  5. package/dist/index.mjs +18 -353
  6. package/dist/index.no-plugins.js +136 -0
  7. package/dist/index.no-plugins.mjs +136 -0
  8. package/dist/plugins/checkbox/index.d.ts +2 -0
  9. package/dist/plugins/frontmatter/index.d.ts +2 -0
  10. package/dist/plugins/hljs/index.browser.d.ts +2 -0
  11. package/dist/plugins/hljs/index.d.ts +2 -0
  12. package/dist/plugins/index.d.ts +7 -0
  13. package/dist/plugins/katex/index.browser.d.ts +2 -0
  14. package/dist/plugins/katex/index.d.ts +2 -0
  15. package/dist/plugins/npm-url/index.d.ts +2 -0
  16. package/dist/plugins/prism/index.browser.d.ts +2 -0
  17. package/dist/plugins/prism/index.d.ts +2 -0
  18. package/dist/plugins/source-lines/index.d.ts +2 -0
  19. package/dist/plugins.js +307 -0
  20. package/dist/plugins.mjs +307 -0
  21. package/dist/transform.d.ts +3 -9
  22. package/package.json +23 -7
  23. package/dist/constants.d.ts +0 -4
  24. package/dist/plugins/checkbox.d.ts +0 -2
  25. package/dist/plugins/frontmatter.d.ts +0 -2
  26. package/dist/plugins/hljs.browser.d.ts +0 -2
  27. package/dist/plugins/hljs.d.ts +0 -2
  28. package/dist/plugins/katex.browser.d.ts +0 -2
  29. package/dist/plugins/katex.d.ts +0 -2
  30. package/dist/plugins/npm-url.d.ts +0 -2
  31. package/dist/plugins/prism.browser.d.ts +0 -2
  32. package/dist/plugins/prism.d.ts +0 -2
  33. package/dist/plugins/source-lines.d.ts +0 -2
  34. /package/dist/plugins/{hljs.config.d.ts → hljs/config.d.ts} +0 -0
  35. /package/dist/plugins/{katex.config.d.ts → katex/config.d.ts} +0 -0
  36. /package/dist/plugins/{prism.config.d.ts → prism/config.d.ts} +0 -0
package/dist/index.js CHANGED
@@ -3,300 +3,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const markmapCommon = require("markmap-common");
4
4
  const markmapHtmlParser = require("markmap-html-parser");
5
5
  const remarkable = require("remarkable");
6
- const yaml = require("js-yaml");
7
- const hljs = require("highlight.js");
8
- const remarkableKatex = require("remarkable-katex");
9
- 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 baseJsPaths = [
11
- `d3@${"7.8.5"}/dist/d3.min.js`,
12
- `markmap-view@${"0.15.9-alpha.6"}/dist/browser/index.js`
13
- ];
14
- 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>`;
15
- 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>`;
16
- function createTransformHooks(transformer) {
17
- return {
18
- transformer,
19
- parser: new markmapCommon.Hook(),
20
- beforeParse: new markmapCommon.Hook(),
21
- afterParse: new markmapCommon.Hook(),
22
- htmltag: new markmapCommon.Hook(),
23
- retransform: new markmapCommon.Hook()
24
- };
25
- }
26
- function definePlugin(plugin2) {
27
- return plugin2;
28
- }
29
- const name$5 = "checkbox";
30
- const plugin$3 = definePlugin({
31
- name: name$5,
32
- transform(transformHooks) {
33
- transformHooks.parser.tap((md) => {
34
- md.core.ruler.before(
35
- "inline",
36
- "checkbox",
37
- (state) => {
38
- for (let i = 2; i < state.tokens.length; i += 1) {
39
- const token = state.tokens[i];
40
- if (token.type === "inline" && token.content) {
41
- const prevType = state.tokens[i - 1].type;
42
- const prevPrevType = state.tokens[i - 2].type;
43
- if (prevType === "heading_open" || prevType === "paragraph_open" && prevPrevType === "list_item_open") {
44
- if (token.content.startsWith("[ ]")) {
45
- token.content = `${svg_unmarked} ${token.content.slice(3)}`;
46
- } else if (token.content.startsWith("[x]")) {
47
- token.content = `${svg_marked} ${token.content.slice(3)}`;
48
- }
49
- }
50
- }
51
- }
52
- return false;
53
- },
54
- {}
55
- );
56
- });
57
- return {};
58
- }
59
- });
60
- const name$4 = "frontmatter";
61
- const frontmatter = definePlugin({
62
- name: name$4,
63
- transform(transformHooks) {
64
- transformHooks.beforeParse.tap((md, context) => {
65
- const { content } = context;
66
- if (!/^---\r?\n/.test(content))
67
- return;
68
- const match = /\n---\r?\n/.exec(content);
69
- if (!match)
70
- return;
71
- const raw = content.slice(4, match.index);
72
- let frontmatter2;
73
- try {
74
- frontmatter2 = yaml.load(raw);
75
- if (frontmatter2 == null ? void 0 : frontmatter2.markmap) {
76
- frontmatter2.markmap = normalizeMarkmapJsonOptions(
77
- frontmatter2.markmap
78
- );
79
- }
80
- } catch {
81
- return;
82
- }
83
- context.frontmatter = frontmatter2;
84
- context.content = content.slice(match.index + match[0].length);
85
- context.contentLineOffset = content.slice(0, match.index).split("\n").length + 1;
86
- });
87
- return {};
88
- }
89
- });
90
- function normalizeMarkmapJsonOptions(options) {
91
- if (!options)
92
- return;
93
- ["color", "extraJs", "extraCss"].forEach((key) => {
94
- if (options[key] != null)
95
- options[key] = normalizeStringArray(options[key]);
96
- });
97
- ["duration", "maxWidth", "initialExpandLevel"].forEach((key) => {
98
- if (options[key] != null)
99
- options[key] = normalizeNumber(options[key]);
100
- });
101
- return options;
102
- }
103
- function normalizeStringArray(value) {
104
- let result;
105
- if (typeof value === "string")
106
- result = [value];
107
- else if (Array.isArray(value))
108
- result = value.filter((item) => item && typeof item === "string");
109
- return (result == null ? void 0 : result.length) ? result : void 0;
110
- }
111
- function normalizeNumber(value) {
112
- if (isNaN(+value))
113
- return;
114
- return +value;
115
- }
116
- const name$3 = "hljs";
117
- const preloadScripts$1 = [
118
- `@highlightjs/cdn-assets@${"11.8.0"}/highlight.min.js`
119
- ].map((path) => markmapCommon.buildJSItem(path));
120
- const styles$1 = [
121
- `@highlightjs/cdn-assets@${"11.8.0"}/styles/default.min.css`
122
- // `highlight.js@${process.env.HLJS_VERSION}/styles/default.css`,
123
- ].map((path) => markmapCommon.buildCSSItem(path));
124
- const config$1 = {
125
- versions: {
126
- hljs: "11.8.0"
127
- },
128
- preloadScripts: preloadScripts$1,
129
- styles: styles$1
130
- };
131
- const plugin$2 = definePlugin({
132
- name: name$3,
133
- config: config$1,
134
- transform(transformHooks) {
135
- var _a;
136
- let enableFeature = markmapCommon.noop;
137
- transformHooks.parser.tap((md) => {
138
- md.set({
139
- highlight: (str, language) => {
140
- enableFeature();
141
- return hljs.highlightAuto(str, language ? [language] : void 0).value;
142
- }
143
- });
144
- });
145
- transformHooks.beforeParse.tap((_, context) => {
146
- enableFeature = () => {
147
- context.features[name$3] = true;
148
- };
149
- });
150
- return {
151
- styles: (_a = plugin$2.config) == null ? void 0 : _a.styles
152
- };
153
- }
154
- });
155
- const name$2 = "katex";
156
- const preloadScripts = [
157
- `katex@${"0.16.8"}/dist/katex.min.js`
158
- ].map((path) => markmapCommon.buildJSItem(path));
159
- const webfontloader = markmapCommon.buildJSItem(
160
- `webfontloader@${"1.6.28"}/webfontloader.js`
161
- );
162
- webfontloader.data.defer = true;
163
- const styles = [`katex@${"0.16.8"}/dist/katex.min.css`].map(
164
- (path) => markmapCommon.buildCSSItem(path)
165
- );
166
- const config = {
167
- versions: {
168
- katex: "0.16.8",
169
- webfontloader: "1.6.28"
170
- },
171
- preloadScripts,
172
- scripts: [
173
- {
174
- type: "iife",
175
- data: {
176
- fn: (getMarkmap) => {
177
- window.WebFontConfig = {
178
- custom: {
179
- families: [
180
- "KaTeX_AMS",
181
- "KaTeX_Caligraphic:n4,n7",
182
- "KaTeX_Fraktur:n4,n7",
183
- "KaTeX_Main:n4,n7,i4,i7",
184
- "KaTeX_Math:i4,i7",
185
- "KaTeX_Script",
186
- "KaTeX_SansSerif:n4,n7,i4",
187
- "KaTeX_Size1",
188
- "KaTeX_Size2",
189
- "KaTeX_Size3",
190
- "KaTeX_Size4",
191
- "KaTeX_Typewriter"
192
- ]
193
- },
194
- active: () => {
195
- getMarkmap().refreshHook.call();
196
- }
197
- };
198
- },
199
- getParams({ getMarkmap }) {
200
- return [getMarkmap];
201
- }
202
- }
203
- },
204
- webfontloader
205
- ],
206
- styles
207
- };
208
- const plugin$1 = definePlugin({
209
- name: name$2,
210
- config,
211
- transform(transformHooks) {
212
- var _a, _b;
213
- let enableFeature = markmapCommon.noop;
214
- transformHooks.parser.tap((md) => {
215
- md.use(remarkableKatex);
216
- md.renderer.rules.katex = markmapCommon.wrapFunction(
217
- md.renderer.rules.katex,
218
- (render, ...args) => {
219
- enableFeature();
220
- return render(...args);
221
- }
222
- );
223
- });
224
- transformHooks.beforeParse.tap((_, context) => {
225
- enableFeature = () => {
226
- context.features[name$2] = true;
227
- };
228
- });
229
- return {
230
- styles: (_a = plugin$1.config) == null ? void 0 : _a.styles,
231
- scripts: (_b = plugin$1.config) == null ? void 0 : _b.scripts
232
- };
233
- }
234
- });
235
- const name$1 = "npmUrl";
236
- const npmUrl = definePlugin({
237
- name: name$1,
238
- transform(transformHooks) {
239
- transformHooks.afterParse.tap((_, context) => {
240
- const { frontmatter: frontmatter2 } = context;
241
- const markmap = frontmatter2 == null ? void 0 : frontmatter2.markmap;
242
- if (markmap) {
243
- ["extraJs", "extraCss"].forEach((key) => {
244
- const value = markmap[key];
245
- if (value) {
246
- markmap[key] = value.map((path) => {
247
- if (path.startsWith("npm:")) {
248
- return transformHooks.transformer.urlBuilder.getFullUrl(
249
- path.slice(4)
250
- );
251
- }
252
- return path;
253
- });
254
- }
255
- });
256
- }
257
- });
258
- return {};
259
- }
260
- });
261
- const name = "sourceLines";
262
- const plugin = definePlugin({
263
- name,
264
- transform(transformHooks) {
265
- transformHooks.parser.tap((md) => {
266
- Object.entries(md.renderer.rules).forEach(([key, value]) => {
267
- if (typeof value === "function") {
268
- md.renderer.rules[key] = patchRule(value);
269
- } else {
270
- Object.entries(value).forEach(([k, v]) => {
271
- value[k] = patchRule(v);
272
- });
273
- }
274
- });
275
- });
276
- return {};
277
- }
278
- });
279
- function patchRule(rule) {
280
- return markmapCommon.wrapFunction(rule, (render, tokens, idx, ...rest) => {
281
- let html = render(tokens, idx, ...rest);
282
- const { lines } = tokens[idx];
283
- if (lines) {
284
- html = html.replace(
285
- /^<[\w-]+/,
286
- (m) => `${m} data-lines="${lines.join(",")}"`
287
- );
288
- }
289
- return html;
290
- });
291
- }
292
- const plugins = [
293
- frontmatter,
294
- plugin$1,
295
- plugin$2,
296
- npmUrl,
297
- plugin$3,
298
- plugin
299
- ];
6
+ const plugins = require("./plugins.js");
7
+ require("js-yaml");
8
+ require("highlight.js");
9
+ require("remarkable-katex");
300
10
  function patchJSItem(urlBuilder, item) {
301
11
  if (item.type === "script" && item.data.src) {
302
12
  return {
@@ -321,6 +31,7 @@ function patchCSSItem(urlBuilder, item) {
321
31
  }
322
32
  return item;
323
33
  }
34
+ const builtInPlugins = plugins.plugins;
324
35
  function cleanNode(node) {
325
36
  while (!node.content && node.children.length === 1) {
326
37
  node = node.children[0];
@@ -337,16 +48,16 @@ function cleanNode(node) {
337
48
  };
338
49
  }
339
50
  class Transformer {
340
- constructor(plugins$1 = plugins) {
51
+ constructor(plugins$1 = builtInPlugins) {
341
52
  this.assetsMap = {};
342
53
  this.urlBuilder = new markmapCommon.UrlBuilder();
343
- this.hooks = createTransformHooks(this);
54
+ this.hooks = plugins.createTransformHooks(this);
344
55
  this.plugins = plugins$1.map(
345
- (plugin2) => typeof plugin2 === "function" ? plugin2() : plugin2
56
+ (plugin) => typeof plugin === "function" ? plugin() : plugin
346
57
  );
347
58
  const assetsMap = {};
348
- for (const { name: name2, transform } of this.plugins) {
349
- assetsMap[name2] = transform(this.hooks);
59
+ for (const { name, transform } of this.plugins) {
60
+ assetsMap[name] = transform(this.hooks);
350
61
  }
351
62
  this.assetsMap = assetsMap;
352
63
  const md = new remarkable.Remarkable("full", {
@@ -387,19 +98,19 @@ class Transformer {
387
98
  * Get all assets from enabled plugins or filter them by plugin names as keys.
388
99
  */
389
100
  getAssets(keys) {
390
- const styles2 = [];
101
+ const styles = [];
391
102
  const scripts = [];
392
- keys ?? (keys = this.plugins.map((plugin2) => plugin2.name));
103
+ keys ?? (keys = this.plugins.map((plugin) => plugin.name));
393
104
  for (const assets of keys.map((key) => this.assetsMap[key])) {
394
105
  if (assets) {
395
106
  if (assets.styles)
396
- styles2.push(...assets.styles);
107
+ styles.push(...assets.styles);
397
108
  if (assets.scripts)
398
109
  scripts.push(...assets.scripts);
399
110
  }
400
111
  }
401
112
  return {
402
- styles: styles2.map((item) => patchCSSItem(this.urlBuilder, item)),
113
+ styles: styles.map((item) => patchCSSItem(this.urlBuilder, item)),
403
114
  scripts: scripts.map((item) => patchJSItem(this.urlBuilder, item))
404
115
  };
405
116
  }
@@ -407,59 +118,13 @@ class Transformer {
407
118
  * Get used assets by features object returned by `transform`.
408
119
  */
409
120
  getUsedAssets(features) {
410
- const keys = this.plugins.map((plugin2) => plugin2.name).filter((name2) => features[name2]);
121
+ const keys = this.plugins.map((plugin) => plugin.name).filter((name) => features[name]);
411
122
  return this.getAssets(keys);
412
123
  }
413
- fillTemplate(root, assets, extra) {
414
- extra = {
415
- ...extra
416
- };
417
- extra.baseJs ?? (extra.baseJs = baseJsPaths.map((path) => this.urlBuilder.getFullUrl(path)).map((path) => markmapCommon.buildJSItem(path)));
418
- const { scripts, styles: styles2 } = assets;
419
- const cssList = [...styles2 ? markmapCommon.persistCSS(styles2) : []];
420
- const context = {
421
- getMarkmap: () => window.markmap,
422
- getOptions: extra.getOptions,
423
- jsonOptions: extra.jsonOptions,
424
- root
425
- };
426
- const jsList = [
427
- ...markmapCommon.persistJS(
428
- [
429
- ...extra.baseJs,
430
- ...scripts || [],
431
- {
432
- type: "iife",
433
- data: {
434
- fn: (getMarkmap, getOptions, root2, jsonOptions) => {
435
- const markmap = getMarkmap();
436
- window.mm = markmap.Markmap.create(
437
- "svg#mindmap",
438
- (getOptions || markmap.deriveOptions)(jsonOptions),
439
- root2
440
- );
441
- },
442
- getParams: ({ getMarkmap, getOptions, root: root2, jsonOptions }) => {
443
- return [getMarkmap, getOptions, root2, jsonOptions];
444
- }
445
- }
446
- }
447
- ],
448
- context
449
- )
450
- ];
451
- const html = template.replace("<!--CSS-->", () => cssList.join("")).replace("<!--JS-->", () => jsList.join(""));
452
- return html;
453
- }
454
124
  }
455
125
  const transformerVersions = {
456
- "markmap-lib": "process.env.VERSION",
457
- d3: "7.8.5"
126
+ "markmap-lib": "0.16.0"
458
127
  };
459
128
  exports.Transformer = Transformer;
460
- exports.baseJsPaths = baseJsPaths;
461
- exports.builtInPlugins = plugins;
462
- exports.svg_marked = svg_marked;
463
- exports.svg_unmarked = svg_unmarked;
464
- exports.template = template;
129
+ exports.builtInPlugins = builtInPlugins;
465
130
  exports.transformerVersions = transformerVersions;