markmap-lib 0.15.9-alpha.0 → 0.15.9-alpha.12

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,30 +1,212 @@
1
- import { buildJSItem, buildCSSItem, Hook, noop, loadJS, UrlBuilder, wrapFunction, persistCSS, persistJS } from "markmap-common";
1
+ import { Hook, buildJSItem, buildCSSItem, loadJS, noop, wrapFunction, UrlBuilder, persistCSS, persistJS } from "markmap-common";
2
2
  import { buildTree } from "markmap-html-parser";
3
3
  import { Remarkable } from "remarkable";
4
- import remarkableKatex from "remarkable-katex";
5
4
  import yaml from "js-yaml";
5
+ import remarkableKatex from "remarkable-katex";
6
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';
7
7
  const baseJsPaths = [
8
8
  `d3@${"7.8.5"}/dist/d3.min.js`,
9
- `markmap-view@${"0.15.9-alpha.0"}/dist/browser/index.js`
9
+ `markmap-view@${"0.15.8"}/dist/browser/index.js`
10
10
  ];
11
- 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 pluginCheckbox = plugin$3;
58
+ const name$4 = "frontmatter";
59
+ const pluginFrontmatter = 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 frontmatter;
71
+ try {
72
+ frontmatter = yaml.load(raw);
73
+ if (frontmatter == null ? void 0 : frontmatter.markmap) {
74
+ frontmatter.markmap = normalizeMarkmapJsonOptions(
75
+ frontmatter.markmap
76
+ );
77
+ }
78
+ } catch {
79
+ return;
80
+ }
81
+ context.frontmatter = frontmatter;
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";
12
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 } = window;
172
+ if (hljs) {
173
+ return hljs.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 pluginHljs = plugin$2;
193
+ const name$2 = "katex";
194
+ const preloadScripts = [
13
195
  `katex@${"0.16.8"}/dist/katex.min.js`
14
196
  ].map((path) => buildJSItem(path));
15
197
  const webfontloader = buildJSItem(
16
198
  `webfontloader@${"1.6.28"}/webfontloader.js`
17
199
  );
18
200
  webfontloader.data.defer = true;
19
- const styles$1 = [`katex@${"0.16.8"}/dist/katex.min.css`].map(
201
+ const styles = [`katex@${"0.16.8"}/dist/katex.min.css`].map(
20
202
  (path) => buildCSSItem(path)
21
203
  );
22
- const config$1 = {
204
+ const config = {
23
205
  versions: {
24
206
  katex: "0.16.8",
25
207
  webfontloader: "1.6.28"
26
208
  },
27
- preloadScripts: preloadScripts$1,
209
+ preloadScripts,
28
210
  scripts: [
29
211
  {
30
212
  type: "iife",
@@ -59,7 +241,7 @@ const config$1 = {
59
241
  },
60
242
  webfontloader
61
243
  ],
62
- styles: styles$1
244
+ styles
63
245
  };
64
246
  function addDefaultVersions(paths, name2, version) {
65
247
  return paths.map((path) => {
@@ -77,46 +259,9 @@ function addDefaultVersions(paths, name2, version) {
77
259
  return path;
78
260
  });
79
261
  }
80
- function patchJSItem(urlBuilder, item) {
81
- if (item.type === "script" && item.data.src) {
82
- return {
83
- ...item,
84
- data: {
85
- ...item.data,
86
- src: urlBuilder.getFullUrl(item.data.src)
87
- }
88
- };
89
- }
90
- return item;
91
- }
92
- function patchCSSItem(urlBuilder, item) {
93
- if (item.type === "stylesheet" && item.data.href) {
94
- return {
95
- ...item,
96
- data: {
97
- ...item.data,
98
- href: urlBuilder.getFullUrl(item.data.href)
99
- }
100
- };
101
- }
102
- return item;
103
- }
104
- function createTransformHooks(transformer) {
105
- return {
106
- transformer,
107
- parser: new Hook(),
108
- beforeParse: new Hook(),
109
- afterParse: new Hook(),
110
- htmltag: new Hook(),
111
- retransform: new Hook()
112
- };
113
- }
114
- function definePlugin(plugin2) {
115
- return plugin2;
116
- }
117
262
  const plugin$1 = definePlugin({
118
- name: name$3,
119
- config: config$1,
263
+ name: name$2,
264
+ config,
120
265
  transform(transformHooks) {
121
266
  var _a, _b, _c, _d;
122
267
  let loading;
@@ -151,7 +296,7 @@ const plugin$1 = definePlugin({
151
296
  });
152
297
  transformHooks.beforeParse.tap((_, context) => {
153
298
  enableFeature = () => {
154
- context.features[name$3] = true;
299
+ context.features[name$2] = true;
155
300
  };
156
301
  });
157
302
  transformHooks.afterParse.tap((_, context) => {
@@ -164,7 +309,7 @@ const plugin$1 = definePlugin({
164
309
  if (value) {
165
310
  markmap[key] = addDefaultVersions(
166
311
  value,
167
- name$3,
312
+ name$2,
168
313
  ((_b2 = (_a3 = plugin$1.config) == null ? void 0 : _a3.versions) == null ? void 0 : _b2.katex) || ""
169
314
  );
170
315
  }
@@ -177,69 +322,14 @@ const plugin$1 = definePlugin({
177
322
  };
178
323
  }
179
324
  });
180
- const name$2 = "frontmatter";
181
- const frontmatter = definePlugin({
182
- name: name$2,
183
- transform(transformHooks) {
184
- transformHooks.beforeParse.tap((md, context) => {
185
- const { content } = context;
186
- if (!/^---\r?\n/.test(content))
187
- return;
188
- const match = /\n---\r?\n/.exec(content);
189
- if (!match)
190
- return;
191
- const raw = content.slice(4, match.index);
192
- let frontmatter2;
193
- try {
194
- frontmatter2 = yaml.load(raw);
195
- if (frontmatter2 == null ? void 0 : frontmatter2.markmap) {
196
- frontmatter2.markmap = normalizeMarkmapJsonOptions(
197
- frontmatter2.markmap
198
- );
199
- }
200
- } catch {
201
- return;
202
- }
203
- context.frontmatter = frontmatter2;
204
- context.content = content.slice(match.index + match[0].length);
205
- context.contentLineOffset = content.slice(0, match.index).split("\n").length + 1;
206
- });
207
- return {};
208
- }
209
- });
210
- function normalizeMarkmapJsonOptions(options) {
211
- if (!options)
212
- return;
213
- ["color", "extraJs", "extraCss"].forEach((key) => {
214
- if (options[key] != null)
215
- options[key] = normalizeStringArray(options[key]);
216
- });
217
- ["duration", "maxWidth", "initialExpandLevel"].forEach((key) => {
218
- if (options[key] != null)
219
- options[key] = normalizeNumber(options[key]);
220
- });
221
- return options;
222
- }
223
- function normalizeStringArray(value) {
224
- let result;
225
- if (typeof value === "string")
226
- result = [value];
227
- else if (Array.isArray(value))
228
- result = value.filter((item) => item && typeof item === "string");
229
- return (result == null ? void 0 : result.length) ? result : void 0;
230
- }
231
- function normalizeNumber(value) {
232
- if (isNaN(+value))
233
- return;
234
- return +value;
235
- }
325
+ const pluginKatex = plugin$1;
236
326
  const name$1 = "npmUrl";
237
- const npmUrl = definePlugin({
327
+ const pluginNpmUrl = definePlugin({
238
328
  name: name$1,
239
329
  transform(transformHooks) {
240
330
  transformHooks.afterParse.tap((_, context) => {
241
- const { frontmatter: frontmatter2 } = context;
242
- const markmap = frontmatter2 == null ? void 0 : frontmatter2.markmap;
331
+ const { frontmatter } = context;
332
+ const markmap = frontmatter == null ? void 0 : frontmatter.markmap;
243
333
  if (markmap) {
244
334
  ["extraJs", "extraCss"].forEach((key) => {
245
335
  const value = markmap[key];
@@ -259,61 +349,47 @@ const npmUrl = definePlugin({
259
349
  return {};
260
350
  }
261
351
  });
262
- const name = "hljs";
263
- const preloadScripts = [
264
- `@highlightjs/cdn-assets@${"11.8.0"}/highlight.min.js`
265
- ].map((path) => buildJSItem(path));
266
- const styles = [
267
- `@highlightjs/cdn-assets@${"11.8.0"}/styles/default.min.css`
268
- // `highlight.js@${process.env.HLJS_VERSION}/styles/default.css`,
269
- ].map((path) => buildCSSItem(path));
270
- const config = {
271
- versions: {
272
- hljs: "11.8.0"
273
- },
274
- preloadScripts,
275
- styles
276
- };
352
+ const name = "sourceLines";
277
353
  const plugin = definePlugin({
278
354
  name,
279
- config,
280
355
  transform(transformHooks) {
281
- var _a, _b, _c;
282
- let loading;
283
- const preloadScripts2 = ((_b = (_a = plugin.config) == null ? void 0 : _a.preloadScripts) == null ? void 0 : _b.map(
284
- (item) => patchJSItem(transformHooks.transformer.urlBuilder, item)
285
- )) || [];
286
- const autoload = () => {
287
- loading || (loading = loadJS(preloadScripts2));
288
- return loading;
289
- };
290
- let enableFeature = noop;
291
356
  transformHooks.parser.tap((md) => {
292
- md.set({
293
- highlight: (str, language) => {
294
- enableFeature();
295
- const { hljs } = window;
296
- if (hljs) {
297
- return hljs.highlightAuto(str, language ? [language] : void 0).value;
298
- }
299
- autoload().then(() => {
300
- 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);
301
363
  });
302
- return str;
303
364
  }
304
365
  });
305
366
  });
306
- transformHooks.beforeParse.tap((_, context) => {
307
- enableFeature = () => {
308
- context.features[name] = true;
309
- };
310
- });
311
- return {
312
- styles: (_c = plugin.config) == null ? void 0 : _c.styles
313
- };
367
+ return {};
314
368
  }
315
369
  });
316
- const plugins = [frontmatter, plugin$1, plugin, npmUrl];
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
+ );
379
+ }
380
+ return html;
381
+ });
382
+ }
383
+ const pluginSourceLines = plugin;
384
+ const plugins = [
385
+ pluginFrontmatter,
386
+ pluginKatex,
387
+ pluginHljs,
388
+ pluginNpmUrl,
389
+ pluginCheckbox,
390
+ pluginSourceLines
391
+ ];
392
+ const builtInPlugins = plugins;
317
393
  function cleanNode(node) {
318
394
  while (!node.content && node.children.length === 1) {
319
395
  node = node.children[0];
@@ -330,11 +406,11 @@ function cleanNode(node) {
330
406
  };
331
407
  }
332
408
  class Transformer {
333
- constructor(plugins$1 = plugins) {
409
+ constructor(plugins2 = builtInPlugins) {
334
410
  this.assetsMap = {};
335
411
  this.urlBuilder = new UrlBuilder();
336
412
  this.hooks = createTransformHooks(this);
337
- this.plugins = plugins$1.map(
413
+ this.plugins = plugins2.map(
338
414
  (plugin2) => typeof plugin2 === "function" ? plugin2() : plugin2
339
415
  );
340
416
  const assetsMap = {};
@@ -452,7 +528,7 @@ const transformerVersions = {
452
528
  export {
453
529
  Transformer,
454
530
  baseJsPaths,
455
- plugins as builtInPlugins,
531
+ builtInPlugins,
456
532
  template,
457
533
  transformerVersions
458
534
  };