markmap-lib 0.15.8 → 0.15.9-alpha.5

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.mjs CHANGED
@@ -1,14 +1,17 @@
1
- import { Remarkable } from "remarkable";
2
1
  import { buildJSItem, buildCSSItem, Hook, wrapFunction, noop, UrlBuilder, persistCSS, persistJS } from "markmap-common";
2
+ import { buildTree } from "markmap-html-parser";
3
+ import { Remarkable } from "remarkable";
3
4
  import remarkableKatex from "remarkable-katex";
4
5
  import yaml from "js-yaml";
5
6
  import hljs from "highlight.js";
6
7
  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
8
  const baseJsPaths = [
8
9
  `d3@${"7.8.5"}/dist/d3.min.js`,
9
- `markmap-view@${"0.15.8"}/dist/browser/index.js`
10
+ `markmap-view@${"0.15.9-alpha.5"}/dist/browser/index.js`
10
11
  ];
11
- const name$3 = "katex";
12
+ 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>`;
13
+ 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>`;
14
+ const name$4 = "katex";
12
15
  const preloadScripts$1 = [
13
16
  `katex@${"0.16.8"}/dist/katex.min.js`
14
17
  ].map((path) => buildJSItem(path));
@@ -74,8 +77,8 @@ function createTransformHooks(transformer) {
74
77
  function definePlugin(plugin2) {
75
78
  return plugin2;
76
79
  }
77
- const plugin$1 = definePlugin({
78
- name: name$3,
80
+ const plugin$2 = definePlugin({
81
+ name: name$4,
79
82
  config: config$1,
80
83
  transform(transformHooks) {
81
84
  var _a, _b;
@@ -92,18 +95,18 @@ const plugin$1 = definePlugin({
92
95
  });
93
96
  transformHooks.beforeParse.tap((_, context) => {
94
97
  enableFeature = () => {
95
- context.features[name$3] = true;
98
+ context.features[name$4] = true;
96
99
  };
97
100
  });
98
101
  return {
99
- styles: (_a = plugin$1.config) == null ? void 0 : _a.styles,
100
- scripts: (_b = plugin$1.config) == null ? void 0 : _b.scripts
102
+ styles: (_a = plugin$2.config) == null ? void 0 : _a.styles,
103
+ scripts: (_b = plugin$2.config) == null ? void 0 : _b.scripts
101
104
  };
102
105
  }
103
106
  });
104
- const name$2 = "frontmatter";
107
+ const name$3 = "frontmatter";
105
108
  const frontmatter = definePlugin({
106
- name: name$2,
109
+ name: name$3,
107
110
  transform(transformHooks) {
108
111
  transformHooks.beforeParse.tap((md, context) => {
109
112
  const { content } = context;
@@ -157,9 +160,9 @@ function normalizeNumber(value) {
157
160
  return;
158
161
  return +value;
159
162
  }
160
- const name$1 = "npmUrl";
163
+ const name$2 = "npmUrl";
161
164
  const npmUrl = definePlugin({
162
- name: name$1,
165
+ name: name$2,
163
166
  transform(transformHooks) {
164
167
  transformHooks.afterParse.tap((_, context) => {
165
168
  const { frontmatter: frontmatter2 } = context;
@@ -183,7 +186,7 @@ const npmUrl = definePlugin({
183
186
  return {};
184
187
  }
185
188
  });
186
- const name = "hljs";
189
+ const name$1 = "hljs";
187
190
  const preloadScripts = [
188
191
  `@highlightjs/cdn-assets@${"11.8.0"}/highlight.min.js`
189
192
  ].map((path) => buildJSItem(path));
@@ -198,8 +201,8 @@ const config = {
198
201
  preloadScripts,
199
202
  styles
200
203
  };
201
- const plugin = definePlugin({
202
- name,
204
+ const plugin$1 = definePlugin({
205
+ name: name$1,
203
206
  config,
204
207
  transform(transformHooks) {
205
208
  var _a;
@@ -214,15 +217,46 @@ const plugin = definePlugin({
214
217
  });
215
218
  transformHooks.beforeParse.tap((_, context) => {
216
219
  enableFeature = () => {
217
- context.features[name] = true;
220
+ context.features[name$1] = true;
218
221
  };
219
222
  });
220
223
  return {
221
- styles: (_a = plugin.config) == null ? void 0 : _a.styles
224
+ styles: (_a = plugin$1.config) == null ? void 0 : _a.styles
222
225
  };
223
226
  }
224
227
  });
225
- const plugins = [frontmatter, plugin$1, plugin, npmUrl];
228
+ const name = "checkbox";
229
+ const plugin = definePlugin({
230
+ name,
231
+ transform(transformHooks) {
232
+ transformHooks.parser.tap((md) => {
233
+ md.core.ruler.before(
234
+ "inline",
235
+ "checkbox",
236
+ (state) => {
237
+ for (let i = 2; i < state.tokens.length; i += 1) {
238
+ const token = state.tokens[i];
239
+ if (token.type === "inline" && token.content) {
240
+ const prevType = state.tokens[i - 1].type;
241
+ const prevPrevType = state.tokens[i - 2].type;
242
+ if (prevType === "heading_open" || prevType === "paragraph_open" && prevPrevType === "list_item_open") {
243
+ if (token.content.startsWith("[ ]")) {
244
+ token.content = `${svg_unmarked} ${token.content.slice(3)}`;
245
+ } else if (token.content.startsWith("[x]")) {
246
+ token.content = `${svg_marked} ${token.content.slice(3)}`;
247
+ }
248
+ }
249
+ }
250
+ }
251
+ return false;
252
+ },
253
+ {}
254
+ );
255
+ });
256
+ return {};
257
+ }
258
+ });
259
+ const plugins = [frontmatter, plugin$2, plugin$1, npmUrl, plugin];
226
260
  function patchJSItem(urlBuilder, item) {
227
261
  if (item.type === "script" && item.data.src) {
228
262
  return {
@@ -248,50 +282,19 @@ function patchCSSItem(urlBuilder, item) {
248
282
  return item;
249
283
  }
250
284
  function cleanNode(node) {
251
- var _a, _b;
252
- if (node.type === "heading") {
253
- node.children = node.children.filter((item) => item.type !== "paragraph");
254
- } else if (node.type === "list_item") {
255
- node.children = node.children.filter((item) => {
256
- if (["paragraph", "fence"].includes(item.type)) {
257
- if (!node.content) {
258
- node.content = item.content;
259
- node.payload = {
260
- ...node.payload,
261
- ...item.payload
262
- };
263
- }
264
- return false;
265
- }
266
- return true;
267
- });
268
- if (((_a = node.payload) == null ? void 0 : _a.index) != null) {
269
- node.content = `${node.payload.index}. ${node.content}`;
270
- }
271
- } else if (node.type === "ordered_list") {
272
- let index = ((_b = node.payload) == null ? void 0 : _b.startIndex) ?? 1;
273
- node.children.forEach((item) => {
274
- if (item.type === "list_item") {
275
- item.payload = {
276
- ...item.payload,
277
- index
278
- };
279
- index += 1;
280
- }
281
- });
285
+ while (!node.content && node.children.length === 1) {
286
+ node = node.children[0];
282
287
  }
283
- if (node.children.length > 0) {
284
- node.children.forEach((child) => cleanNode(child));
285
- if (node.children.length === 1 && !node.children[0].content) {
286
- node.children = node.children[0].children;
287
- }
288
+ while (node.children.length === 1 && !node.children[0].content) {
289
+ node = {
290
+ ...node,
291
+ children: node.children[0].children
292
+ };
288
293
  }
289
- }
290
- function resetDepth(node, depth = 0) {
291
- node.depth = depth;
292
- node.children.forEach((child) => {
293
- resetDepth(child, depth + 1);
294
- });
294
+ return {
295
+ ...node,
296
+ children: node.children.map(cleanNode)
297
+ };
295
298
  }
296
299
  class Transformer {
297
300
  constructor(plugins$1 = plugins) {
@@ -322,100 +325,22 @@ class Transformer {
322
325
  this.md = md;
323
326
  this.hooks.parser.call(md);
324
327
  }
325
- buildTree(tokens) {
326
- const { md } = this;
327
- const root = {
328
- type: "root",
329
- depth: 0,
330
- content: "",
331
- children: [],
332
- payload: {}
333
- };
334
- const stack = [root];
335
- let depth = 0;
336
- for (const token of tokens) {
337
- const payload = {};
338
- if (token.lines) {
339
- payload.lines = token.lines;
340
- }
341
- let current = stack[stack.length - 1];
342
- if (token.type.endsWith("_open")) {
343
- const type = token.type.slice(0, -5);
344
- if (type === "heading") {
345
- depth = token.hLevel;
346
- while ((current == null ? void 0 : current.depth) >= depth) {
347
- stack.pop();
348
- current = stack[stack.length - 1];
349
- }
350
- } else {
351
- depth = Math.max(depth, (current == null ? void 0 : current.depth) || 0) + 1;
352
- if (type === "ordered_list") {
353
- payload.startIndex = token.order;
354
- }
355
- }
356
- const item = {
357
- type,
358
- depth,
359
- payload,
360
- content: "",
361
- children: []
362
- };
363
- current.children.push(item);
364
- stack.push(item);
365
- } else if (!current) {
366
- continue;
367
- } else if (token.type === `${current.type}_close`) {
368
- if (current.type === "heading") {
369
- depth = current.depth;
370
- } else {
371
- stack.pop();
372
- depth = 0;
373
- }
374
- } else if (token.type === "inline") {
375
- const revoke = this.hooks.htmltag.tap((ctx) => {
376
- var _a;
377
- const comment = (_a = ctx.result) == null ? void 0 : _a.match(/^<!--([\s\S]*?)-->$/);
378
- const data = comment == null ? void 0 : comment[1].trim().split(" ");
379
- if ((data == null ? void 0 : data[0]) === "fold") {
380
- current.payload = {
381
- ...current.payload,
382
- fold: ["all", "recursively"].includes(data[1]) ? 2 : 1
383
- };
384
- ctx.result = "";
385
- }
386
- });
387
- const text = md.renderer.render([token], md.options, {});
388
- revoke();
389
- current.content = `${current.content || ""}${text}`;
390
- } else if (token.type === "fence") {
391
- const result = md.renderer.render([token], md.options, {});
392
- current.children.push({
393
- type: token.type,
394
- depth: depth + 1,
395
- content: result,
396
- children: [],
397
- payload
398
- });
399
- } else
400
- ;
401
- }
402
- return root;
403
- }
404
- transform(content) {
405
- var _a;
328
+ transform(content, opts) {
329
+ var _a, _b;
406
330
  const context = {
407
331
  content,
408
332
  features: {},
409
333
  contentLineOffset: 0
410
334
  };
411
335
  this.hooks.beforeParse.call(this.md, context);
412
- const tokens = this.md.parse(context.content, {});
336
+ const html = this.md.render(context.content, {});
413
337
  this.hooks.afterParse.call(this.md, context);
414
- let root = this.buildTree(tokens);
415
- cleanNode(root);
416
- if (((_a = root.children) == null ? void 0 : _a.length) === 1)
417
- root = root.children[0];
418
- resetDepth(root);
338
+ const root = cleanNode(
339
+ buildTree(html, {
340
+ ...(_b = (_a = context.frontmatter) == null ? void 0 : _a.markmap) == null ? void 0 : _b.htmlParser,
341
+ ...opts
342
+ })
343
+ );
419
344
  return { ...context, root };
420
345
  }
421
346
  /**
@@ -495,6 +420,8 @@ export {
495
420
  Transformer,
496
421
  baseJsPaths,
497
422
  plugins as builtInPlugins,
423
+ svg_marked,
424
+ svg_unmarked,
498
425
  template,
499
426
  transformerVersions
500
427
  };
@@ -0,0 +1,2 @@
1
+ declare const plugin: import("../types").ITransformPlugin;
2
+ export default plugin;
@@ -1,7 +1,8 @@
1
+ import { IMarkmapOptions, IPureNode, JSItem, UrlBuilder } from 'markmap-common';
2
+ import { IHtmlParserOptions } from 'markmap-html-parser';
1
3
  import { Remarkable } from 'remarkable';
2
- import { UrlBuilder, JSItem, IMarkmapOptions, IMarkmapJSONOptions, IPureNode } from 'markmap-common';
3
- import { ITransformResult, ITransformPlugin, IAssets, ITransformHooks, IFeatures, ITransformer } from './types';
4
4
  import { plugins as builtInPlugins } from './plugins';
5
+ import { IAssets, IFeatures, ITransformHooks, ITransformPlugin, ITransformResult, ITransformer, IMarkmapJSONOptions } from './types';
5
6
  export { builtInPlugins };
6
7
  export declare class Transformer implements ITransformer {
7
8
  hooks: ITransformHooks;
@@ -10,8 +11,7 @@ export declare class Transformer implements ITransformer {
10
11
  urlBuilder: UrlBuilder;
11
12
  plugins: ITransformPlugin[];
12
13
  constructor(plugins?: Array<ITransformPlugin | (() => ITransformPlugin)>);
13
- buildTree(tokens: Remarkable.Token[]): IPureNode;
14
- transform(content: string): ITransformResult;
14
+ transform(content: string, opts?: Partial<IHtmlParserOptions>): ITransformResult;
15
15
  /**
16
16
  * Get all assets from enabled plugins or filter them by plugin names as keys.
17
17
  */
package/dist/types.d.ts CHANGED
@@ -1,5 +1,7 @@
1
+ import { CSSItem, Hook, IPureNode, IWrapContext, JSItem, UrlBuilder } from 'markmap-common';
2
+ import { IHtmlParserOptions } from 'markmap-html-parser';
3
+ import { IMarkmapJSONOptions as IMarkmapJSONOptionsForView } from 'markmap-view';
1
4
  import type { Remarkable } from 'remarkable';
2
- import { UrlBuilder, CSSItem, JSItem, IPureNode, IWrapContext, Hook, IMarkmapJSONOptions } from 'markmap-common';
3
5
  type Htmltag = Remarkable.Rule<Remarkable.HtmlTagToken, string>;
4
6
  export interface ITransformHooks {
5
7
  transformer: ITransformer;
@@ -45,6 +47,9 @@ export interface IFeatures {
45
47
  export interface ITransformer {
46
48
  urlBuilder: UrlBuilder;
47
49
  }
50
+ export interface IMarkmapJSONOptions extends IMarkmapJSONOptionsForView {
51
+ htmlParser?: Partial<IHtmlParserOptions>;
52
+ }
48
53
  export interface ITransformContext {
49
54
  features: IFeatures;
50
55
  content: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markmap-lib",
3
- "version": "0.15.8",
3
+ "version": "0.15.9-alpha.5+c64866f",
4
4
  "description": "Visualize your Markdown as mindmaps with Markmap",
5
5
  "author": "Gerald <gera2ld@live.com>",
6
6
  "license": "MIT",
@@ -47,8 +47,8 @@
47
47
  "devDependencies": {
48
48
  "@highlightjs/cdn-assets": "^11.8.0",
49
49
  "@types/remarkable": "^2.0.3",
50
- "markmap-common": "0.15.6",
51
- "markmap-view": "0.15.8",
50
+ "markmap-common": "0.15.9-alpha.5+c64866f",
51
+ "markmap-view": "0.15.9-alpha.5+c64866f",
52
52
  "webfontloader": "^1.6.28"
53
53
  },
54
54
  "dependencies": {
@@ -56,6 +56,7 @@
56
56
  "highlight.js": "^11.8.0",
57
57
  "js-yaml": "^4.1.0",
58
58
  "katex": "^0.16.8",
59
+ "markmap-html-parser": "0.15.9-alpha.5+c64866f",
59
60
  "prismjs": "^1.29.0",
60
61
  "remarkable": "^2.0.1",
61
62
  "remarkable-katex": "^1.2.1"
@@ -73,5 +74,5 @@
73
74
  ]
74
75
  }
75
76
  },
76
- "gitHead": "d5a29ab4064a5496f879de04d730d7192461d3c5"
77
+ "gitHead": "c64866fb4873a99923be18ea70715aa49a6dabef"
77
78
  }