markmap-lib 0.15.9-alpha.0 → 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,30 +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.0"}/dist/browser/index.js`
9
+ `markmap-view@${"0.15.9-alpha.6"}/dist/browser/index.js`
10
10
  ];
11
- const name$3 = "katex";
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
+ 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
+ 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";
12
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 = [
13
193
  `katex@${"0.16.8"}/dist/katex.min.js`
14
194
  ].map((path) => buildJSItem(path));
15
195
  const webfontloader = buildJSItem(
16
196
  `webfontloader@${"1.6.28"}/webfontloader.js`
17
197
  );
18
198
  webfontloader.data.defer = true;
19
- const styles$1 = [`katex@${"0.16.8"}/dist/katex.min.css`].map(
199
+ const styles = [`katex@${"0.16.8"}/dist/katex.min.css`].map(
20
200
  (path) => buildCSSItem(path)
21
201
  );
22
- const config$1 = {
202
+ const config = {
23
203
  versions: {
24
204
  katex: "0.16.8",
25
205
  webfontloader: "1.6.28"
26
206
  },
27
- preloadScripts: preloadScripts$1,
207
+ preloadScripts,
28
208
  scripts: [
29
209
  {
30
210
  type: "iife",
@@ -59,7 +239,7 @@ const config$1 = {
59
239
  },
60
240
  webfontloader
61
241
  ],
62
- styles: styles$1
242
+ styles
63
243
  };
64
244
  function addDefaultVersions(paths, name2, version) {
65
245
  return paths.map((path) => {
@@ -77,46 +257,9 @@ function addDefaultVersions(paths, name2, version) {
77
257
  return path;
78
258
  });
79
259
  }
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
260
  const plugin$1 = definePlugin({
118
- name: name$3,
119
- config: config$1,
261
+ name: name$2,
262
+ config,
120
263
  transform(transformHooks) {
121
264
  var _a, _b, _c, _d;
122
265
  let loading;
@@ -151,7 +294,7 @@ const plugin$1 = definePlugin({
151
294
  });
152
295
  transformHooks.beforeParse.tap((_, context) => {
153
296
  enableFeature = () => {
154
- context.features[name$3] = true;
297
+ context.features[name$2] = true;
155
298
  };
156
299
  });
157
300
  transformHooks.afterParse.tap((_, context) => {
@@ -164,7 +307,7 @@ const plugin$1 = definePlugin({
164
307
  if (value) {
165
308
  markmap[key] = addDefaultVersions(
166
309
  value,
167
- name$3,
310
+ name$2,
168
311
  ((_b2 = (_a3 = plugin$1.config) == null ? void 0 : _a3.versions) == null ? void 0 : _b2.katex) || ""
169
312
  );
170
313
  }
@@ -177,62 +320,6 @@ const plugin$1 = definePlugin({
177
320
  };
178
321
  }
179
322
  });
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
- }
236
323
  const name$1 = "npmUrl";
237
324
  const npmUrl = definePlugin({
238
325
  name: name$1,
@@ -259,61 +346,45 @@ const npmUrl = definePlugin({
259
346
  return {};
260
347
  }
261
348
  });
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
- };
349
+ const name = "sourceLines";
277
350
  const plugin = definePlugin({
278
351
  name,
279
- config,
280
352
  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
353
  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();
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);
301
360
  });
302
- return str;
303
361
  }
304
362
  });
305
363
  });
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
- };
364
+ return {};
314
365
  }
315
366
  });
316
- const plugins = [frontmatter, plugin$1, plugin, npmUrl];
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
+ ];
317
388
  function cleanNode(node) {
318
389
  while (!node.content && node.children.length === 1) {
319
390
  node = node.children[0];
@@ -453,6 +524,8 @@ export {
453
524
  Transformer,
454
525
  baseJsPaths,
455
526
  plugins as builtInPlugins,
527
+ svg_marked,
528
+ svg_unmarked,
456
529
  template,
457
530
  transformerVersions
458
531
  };
@@ -1,2 +1,4 @@
1
1
  export declare const template: string;
2
2
  export declare const baseJsPaths: string[];
3
+ export declare 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>";
4
+ export declare 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>";