markmap-lib 0.15.9-alpha.5 → 0.15.9-alpha.6

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,32 +1,210 @@
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.5"}/dist/browser/index.js`
9
+ `markmap-view@${"0.15.9-alpha.6"}/dist/browser/index.js`
10
10
  ];
11
11
  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>`;
12
12
  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>`;
13
- const name$4 = "katex";
13
+ function createTransformHooks(transformer) {
14
+ return {
15
+ transformer,
16
+ parser: new Hook(),
17
+ beforeParse: new Hook(),
18
+ afterParse: new Hook(),
19
+ htmltag: new Hook(),
20
+ retransform: new Hook()
21
+ };
22
+ }
23
+ function definePlugin(plugin2) {
24
+ return plugin2;
25
+ }
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 name$4 = "frontmatter";
58
+ const frontmatter = definePlugin({
59
+ name: name$4,
60
+ transform(transformHooks) {
61
+ transformHooks.beforeParse.tap((md, context) => {
62
+ const { content } = context;
63
+ if (!/^---\r?\n/.test(content))
64
+ return;
65
+ const match = /\n---\r?\n/.exec(content);
66
+ if (!match)
67
+ return;
68
+ const raw = content.slice(4, match.index);
69
+ let frontmatter2;
70
+ try {
71
+ frontmatter2 = yaml.load(raw);
72
+ if (frontmatter2 == null ? void 0 : frontmatter2.markmap) {
73
+ frontmatter2.markmap = normalizeMarkmapJsonOptions(
74
+ frontmatter2.markmap
75
+ );
76
+ }
77
+ } catch {
78
+ return;
79
+ }
80
+ context.frontmatter = frontmatter2;
81
+ context.content = content.slice(match.index + match[0].length);
82
+ context.contentLineOffset = content.slice(0, match.index).split("\n").length + 1;
83
+ });
84
+ return {};
85
+ }
86
+ });
87
+ function normalizeMarkmapJsonOptions(options) {
88
+ if (!options)
89
+ return;
90
+ ["color", "extraJs", "extraCss"].forEach((key) => {
91
+ if (options[key] != null)
92
+ options[key] = normalizeStringArray(options[key]);
93
+ });
94
+ ["duration", "maxWidth", "initialExpandLevel"].forEach((key) => {
95
+ if (options[key] != null)
96
+ options[key] = normalizeNumber(options[key]);
97
+ });
98
+ return options;
99
+ }
100
+ function normalizeStringArray(value) {
101
+ let result;
102
+ if (typeof value === "string")
103
+ result = [value];
104
+ else if (Array.isArray(value))
105
+ result = value.filter((item) => item && typeof item === "string");
106
+ return (result == null ? void 0 : result.length) ? result : void 0;
107
+ }
108
+ function normalizeNumber(value) {
109
+ if (isNaN(+value))
110
+ return;
111
+ return +value;
112
+ }
113
+ function patchJSItem(urlBuilder, item) {
114
+ if (item.type === "script" && item.data.src) {
115
+ return {
116
+ ...item,
117
+ data: {
118
+ ...item.data,
119
+ src: urlBuilder.getFullUrl(item.data.src)
120
+ }
121
+ };
122
+ }
123
+ return item;
124
+ }
125
+ function patchCSSItem(urlBuilder, item) {
126
+ if (item.type === "stylesheet" && item.data.href) {
127
+ return {
128
+ ...item,
129
+ data: {
130
+ ...item.data,
131
+ href: urlBuilder.getFullUrl(item.data.href)
132
+ }
133
+ };
134
+ }
135
+ return item;
136
+ }
137
+ const name$3 = "hljs";
14
138
  const preloadScripts$1 = [
139
+ `@highlightjs/cdn-assets@${"11.8.0"}/highlight.min.js`
140
+ ].map((path) => buildJSItem(path));
141
+ const styles$1 = [
142
+ `@highlightjs/cdn-assets@${"11.8.0"}/styles/default.min.css`
143
+ // `highlight.js@${process.env.HLJS_VERSION}/styles/default.css`,
144
+ ].map((path) => buildCSSItem(path));
145
+ const config$1 = {
146
+ versions: {
147
+ hljs: "11.8.0"
148
+ },
149
+ preloadScripts: preloadScripts$1,
150
+ styles: styles$1
151
+ };
152
+ const plugin$2 = definePlugin({
153
+ name: name$3,
154
+ config: config$1,
155
+ transform(transformHooks) {
156
+ var _a, _b, _c;
157
+ let loading;
158
+ const preloadScripts2 = ((_b = (_a = plugin$2.config) == null ? void 0 : _a.preloadScripts) == null ? void 0 : _b.map(
159
+ (item) => patchJSItem(transformHooks.transformer.urlBuilder, item)
160
+ )) || [];
161
+ const autoload = () => {
162
+ loading || (loading = loadJS(preloadScripts2));
163
+ return loading;
164
+ };
165
+ let enableFeature = noop;
166
+ transformHooks.parser.tap((md) => {
167
+ md.set({
168
+ highlight: (str, language) => {
169
+ enableFeature();
170
+ const { hljs } = window;
171
+ if (hljs) {
172
+ return hljs.highlightAuto(str, language ? [language] : void 0).value;
173
+ }
174
+ autoload().then(() => {
175
+ transformHooks.retransform.call();
176
+ });
177
+ return str;
178
+ }
179
+ });
180
+ });
181
+ transformHooks.beforeParse.tap((_, context) => {
182
+ enableFeature = () => {
183
+ context.features[name$3] = true;
184
+ };
185
+ });
186
+ return {
187
+ styles: (_c = plugin$2.config) == null ? void 0 : _c.styles
188
+ };
189
+ }
190
+ });
191
+ const name$2 = "katex";
192
+ const preloadScripts = [
15
193
  `katex@${"0.16.8"}/dist/katex.min.js`
16
194
  ].map((path) => buildJSItem(path));
17
195
  const webfontloader = buildJSItem(
18
196
  `webfontloader@${"1.6.28"}/webfontloader.js`
19
197
  );
20
198
  webfontloader.data.defer = true;
21
- const styles$1 = [`katex@${"0.16.8"}/dist/katex.min.css`].map(
199
+ const styles = [`katex@${"0.16.8"}/dist/katex.min.css`].map(
22
200
  (path) => buildCSSItem(path)
23
201
  );
24
- const config$1 = {
202
+ const config = {
25
203
  versions: {
26
204
  katex: "0.16.8",
27
205
  webfontloader: "1.6.28"
28
206
  },
29
- preloadScripts: preloadScripts$1,
207
+ preloadScripts,
30
208
  scripts: [
31
209
  {
32
210
  type: "iife",
@@ -61,7 +239,7 @@ const config$1 = {
61
239
  },
62
240
  webfontloader
63
241
  ],
64
- styles: styles$1
242
+ styles
65
243
  };
66
244
  function addDefaultVersions(paths, name2, version) {
67
245
  return paths.map((path) => {
@@ -79,50 +257,13 @@ function addDefaultVersions(paths, name2, version) {
79
257
  return path;
80
258
  });
81
259
  }
82
- function patchJSItem(urlBuilder, item) {
83
- if (item.type === "script" && item.data.src) {
84
- return {
85
- ...item,
86
- data: {
87
- ...item.data,
88
- src: urlBuilder.getFullUrl(item.data.src)
89
- }
90
- };
91
- }
92
- return item;
93
- }
94
- function patchCSSItem(urlBuilder, item) {
95
- if (item.type === "stylesheet" && item.data.href) {
96
- return {
97
- ...item,
98
- data: {
99
- ...item.data,
100
- href: urlBuilder.getFullUrl(item.data.href)
101
- }
102
- };
103
- }
104
- return item;
105
- }
106
- function createTransformHooks(transformer) {
107
- return {
108
- transformer,
109
- parser: new Hook(),
110
- beforeParse: new Hook(),
111
- afterParse: new Hook(),
112
- htmltag: new Hook(),
113
- retransform: new Hook()
114
- };
115
- }
116
- function definePlugin(plugin2) {
117
- return plugin2;
118
- }
119
- const plugin$2 = definePlugin({
120
- name: name$4,
121
- config: config$1,
260
+ const plugin$1 = definePlugin({
261
+ name: name$2,
262
+ config,
122
263
  transform(transformHooks) {
123
264
  var _a, _b, _c, _d;
124
265
  let loading;
125
- const preloadScripts2 = ((_b = (_a = plugin$2.config) == null ? void 0 : _a.preloadScripts) == null ? void 0 : _b.map(
266
+ const preloadScripts2 = ((_b = (_a = plugin$1.config) == null ? void 0 : _a.preloadScripts) == null ? void 0 : _b.map(
126
267
  (item) => patchJSItem(transformHooks.transformer.urlBuilder, item)
127
268
  )) || [];
128
269
  const autoload = () => {
@@ -153,7 +294,7 @@ const plugin$2 = definePlugin({
153
294
  });
154
295
  transformHooks.beforeParse.tap((_, context) => {
155
296
  enableFeature = () => {
156
- context.features[name$4] = true;
297
+ context.features[name$2] = true;
157
298
  };
158
299
  });
159
300
  transformHooks.afterParse.tap((_, context) => {
@@ -166,78 +307,22 @@ const plugin$2 = definePlugin({
166
307
  if (value) {
167
308
  markmap[key] = addDefaultVersions(
168
309
  value,
169
- name$4,
170
- ((_b2 = (_a3 = plugin$2.config) == null ? void 0 : _a3.versions) == null ? void 0 : _b2.katex) || ""
310
+ name$2,
311
+ ((_b2 = (_a3 = plugin$1.config) == null ? void 0 : _a3.versions) == null ? void 0 : _b2.katex) || ""
171
312
  );
172
313
  }
173
314
  });
174
315
  }
175
316
  });
176
317
  return {
177
- styles: (_c = plugin$2.config) == null ? void 0 : _c.styles,
178
- scripts: (_d = plugin$2.config) == null ? void 0 : _d.scripts
318
+ styles: (_c = plugin$1.config) == null ? void 0 : _c.styles,
319
+ scripts: (_d = plugin$1.config) == null ? void 0 : _d.scripts
179
320
  };
180
321
  }
181
322
  });
182
- const name$3 = "frontmatter";
183
- const frontmatter = definePlugin({
184
- name: name$3,
185
- transform(transformHooks) {
186
- transformHooks.beforeParse.tap((md, context) => {
187
- const { content } = context;
188
- if (!/^---\r?\n/.test(content))
189
- return;
190
- const match = /\n---\r?\n/.exec(content);
191
- if (!match)
192
- return;
193
- const raw = content.slice(4, match.index);
194
- let frontmatter2;
195
- try {
196
- frontmatter2 = yaml.load(raw);
197
- if (frontmatter2 == null ? void 0 : frontmatter2.markmap) {
198
- frontmatter2.markmap = normalizeMarkmapJsonOptions(
199
- frontmatter2.markmap
200
- );
201
- }
202
- } catch {
203
- return;
204
- }
205
- context.frontmatter = frontmatter2;
206
- context.content = content.slice(match.index + match[0].length);
207
- context.contentLineOffset = content.slice(0, match.index).split("\n").length + 1;
208
- });
209
- return {};
210
- }
211
- });
212
- function normalizeMarkmapJsonOptions(options) {
213
- if (!options)
214
- return;
215
- ["color", "extraJs", "extraCss"].forEach((key) => {
216
- if (options[key] != null)
217
- options[key] = normalizeStringArray(options[key]);
218
- });
219
- ["duration", "maxWidth", "initialExpandLevel"].forEach((key) => {
220
- if (options[key] != null)
221
- options[key] = normalizeNumber(options[key]);
222
- });
223
- return options;
224
- }
225
- function normalizeStringArray(value) {
226
- let result;
227
- if (typeof value === "string")
228
- result = [value];
229
- else if (Array.isArray(value))
230
- result = value.filter((item) => item && typeof item === "string");
231
- return (result == null ? void 0 : result.length) ? result : void 0;
232
- }
233
- function normalizeNumber(value) {
234
- if (isNaN(+value))
235
- return;
236
- return +value;
237
- }
238
- const name$2 = "npmUrl";
323
+ const name$1 = "npmUrl";
239
324
  const npmUrl = definePlugin({
240
- name: name$2,
325
+ name: name$1,
241
326
  transform(transformHooks) {
242
327
  transformHooks.afterParse.tap((_, context) => {
243
328
  const { frontmatter: frontmatter2 } = context;
@@ -261,92 +346,45 @@ const npmUrl = definePlugin({
261
346
  return {};
262
347
  }
263
348
  });
264
- const name$1 = "hljs";
265
- const preloadScripts = [
266
- `@highlightjs/cdn-assets@${"11.8.0"}/highlight.min.js`
267
- ].map((path) => buildJSItem(path));
268
- const styles = [
269
- `@highlightjs/cdn-assets@${"11.8.0"}/styles/default.min.css`
270
- // `highlight.js@${process.env.HLJS_VERSION}/styles/default.css`,
271
- ].map((path) => buildCSSItem(path));
272
- const config = {
273
- versions: {
274
- hljs: "11.8.0"
275
- },
276
- preloadScripts,
277
- styles
278
- };
279
- const plugin$1 = definePlugin({
280
- name: name$1,
281
- config,
349
+ const name = "sourceLines";
350
+ const plugin = definePlugin({
351
+ name,
282
352
  transform(transformHooks) {
283
- var _a, _b, _c;
284
- let loading;
285
- const preloadScripts2 = ((_b = (_a = plugin$1.config) == null ? void 0 : _a.preloadScripts) == null ? void 0 : _b.map(
286
- (item) => patchJSItem(transformHooks.transformer.urlBuilder, item)
287
- )) || [];
288
- const autoload = () => {
289
- loading || (loading = loadJS(preloadScripts2));
290
- return loading;
291
- };
292
- let enableFeature = noop;
293
353
  transformHooks.parser.tap((md) => {
294
- md.set({
295
- highlight: (str, language) => {
296
- enableFeature();
297
- const { hljs } = window;
298
- if (hljs) {
299
- return hljs.highlightAuto(str, language ? [language] : void 0).value;
300
- }
301
- autoload().then(() => {
302
- transformHooks.retransform.call();
354
+ Object.entries(md.renderer.rules).forEach(([key, value]) => {
355
+ if (typeof value === "function") {
356
+ md.renderer.rules[key] = patchRule(value);
357
+ } else {
358
+ Object.entries(value).forEach(([k, v]) => {
359
+ value[k] = patchRule(v);
303
360
  });
304
- return str;
305
361
  }
306
362
  });
307
363
  });
308
- transformHooks.beforeParse.tap((_, context) => {
309
- enableFeature = () => {
310
- context.features[name$1] = true;
311
- };
312
- });
313
- return {
314
- styles: (_c = plugin$1.config) == null ? void 0 : _c.styles
315
- };
316
- }
317
- });
318
- const name = "checkbox";
319
- const plugin = definePlugin({
320
- name,
321
- transform(transformHooks) {
322
- transformHooks.parser.tap((md) => {
323
- md.core.ruler.before(
324
- "inline",
325
- "checkbox",
326
- (state) => {
327
- for (let i = 2; i < state.tokens.length; i += 1) {
328
- const token = state.tokens[i];
329
- if (token.type === "inline" && token.content) {
330
- const prevType = state.tokens[i - 1].type;
331
- const prevPrevType = state.tokens[i - 2].type;
332
- if (prevType === "heading_open" || prevType === "paragraph_open" && prevPrevType === "list_item_open") {
333
- if (token.content.startsWith("[ ]")) {
334
- token.content = `${svg_unmarked} ${token.content.slice(3)}`;
335
- } else if (token.content.startsWith("[x]")) {
336
- token.content = `${svg_marked} ${token.content.slice(3)}`;
337
- }
338
- }
339
- }
340
- }
341
- return false;
342
- },
343
- {}
344
- );
345
- });
346
364
  return {};
347
365
  }
348
366
  });
349
- const plugins = [frontmatter, plugin$2, plugin$1, npmUrl, plugin];
367
+ function patchRule(rule) {
368
+ return wrapFunction(rule, (render, tokens, idx, ...rest) => {
369
+ let html = render(tokens, idx, ...rest);
370
+ const { lines } = tokens[idx];
371
+ if (lines) {
372
+ html = html.replace(
373
+ /^<[\w-]+/,
374
+ (m) => `${m} data-lines="${lines.join(",")}"`
375
+ );
376
+ }
377
+ return html;
378
+ });
379
+ }
380
+ const plugins = [
381
+ frontmatter,
382
+ plugin$1,
383
+ plugin$2,
384
+ npmUrl,
385
+ plugin$3,
386
+ plugin
387
+ ];
350
388
  function cleanNode(node) {
351
389
  while (!node.content && node.children.length === 1) {
352
390
  node = node.children[0];