@anlijiu/remark-markmap 1.0.1 → 1.0.3

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/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # @anlijiu/remark-markmap
2
+
3
+ A [remark](https://github.com/remarkjs/remark) plugin that renders `markmap` code blocks into inline SVG mindmaps.
4
+
5
+ Part of the [remark-markmap](https://github.com/anlijiu/remark-markmap) monorepo:
6
+
7
+ - `@anlijiu/markmap` — core renderer (tree building, layout, SVG drawing)
8
+ - `@anlijiu/markmap-isomorphic` — browser / Node.js renderer (Playwright on Node.js)
9
+ - `@anlijiu/remark-markmap` — this package
10
+
11
+ ## Installation
12
+
13
+ ```sh
14
+ npm install @anlijiu/remark-markmap
15
+ ```
16
+
17
+ `playwright-core` is a dependency of this package, but Node.js rendering still needs a Chromium
18
+ executable. Point it to your browser via `launchOptions.executablePath`, or configure Playwright's
19
+ environment (`PLAYWRIGHT_BROWSERS_PATH`, etc.).
20
+
21
+ ## Usage
22
+
23
+ ```js
24
+ import { unified } from 'unified'
25
+ import remarkParse from 'remark-parse'
26
+ import remarkRehype from 'remark-rehype'
27
+ import rehypeStringify from 'rehype-stringify'
28
+ import markmap from '@anlijiu/markmap'
29
+ import { remarkMarkmap } from '@anlijiu/remark-markmap'
30
+
31
+ const markdown = `
32
+ ~~~markmap
33
+ ---
34
+ theme: "56000000"
35
+ border: "11110000"
36
+ shape: "10000000"
37
+ linkshape: "00000000"
38
+ ---
39
+ # Root
40
+ ## Branch
41
+ ~~~
42
+ `
43
+
44
+ const html = String(
45
+ await unified()
46
+ .use(remarkParse)
47
+ .use(remarkMarkmap)
48
+ .use(remarkRehype, { handlers: { markmap: markmap.markmapHandler } })
49
+ .use(rehypeStringify)
50
+ .process(markdown),
51
+ )
52
+ ```
53
+
54
+ ### Using a specific browser
55
+
56
+ ```js
57
+ .use(remarkMarkmap, {
58
+ launchOptions: { executablePath: '/usr/bin/google-chrome' },
59
+ })
60
+ ```
61
+
62
+ ## Options
63
+
64
+ `RemarkMarkmapOptions`:
65
+
66
+ - `browserType` / `launchOptions` — forwarded to `createMarkmapRenderer` in
67
+ `@anlijiu/markmap-isomorphic`.
68
+ - `css` — one URL or an iterable of custom CSS URLs.
69
+ - `prefix` — ID prefix for generated SVGs. Defaults to `markmap`.
70
+ - `theme`, `border`, `shape`, `linkshape`, `sitetheme` — forwarded to `@anlijiu/markmap`.
71
+ - `errorFallback(node, error, file)` — return a fallback node when a diagram fails to render; if it
72
+ returns nothing, the code block is removed.
73
+
74
+ `theme` / `border` / `shape` / `linkshape` are eight hex digit strings (one per depth level); see
75
+ the [monorepo README](https://github.com/anlijiu/remark-markmap#config) for the full list of values.
76
+
77
+ ## License
78
+
79
+ MIT
@@ -4,25 +4,18 @@ import { fromHtmlIsomorphic } from "hast-util-from-html-isomorphic";
4
4
  import {
5
5
  createMarkmapRenderer
6
6
  } from "@anlijiu/markmap-isomorphic";
7
- import remarkGfm from "remark-gfm";
7
+ import matter from "gray-matter";
8
+ import rehypeMathjax from "rehype-mathjax";
8
9
  import remarkFrontmatter from "remark-frontmatter";
9
- import remarkParse from "remark-parse";
10
+ import remarkGfm from "remark-gfm";
10
11
  import remarkMath from "remark-math";
12
+ import remarkParse from "remark-parse";
11
13
  import remarkRehype from "remark-rehype";
12
- import matter from "gray-matter";
13
- import rehypeMathjax from "rehype-mathjax";
14
14
  import { unified } from "unified";
15
15
  import { visit } from "unist-util-visit";
16
16
  import { buildTree } from "@anlijiu/markmap";
17
17
  import { inlineCode } from "./handlers.js";
18
18
  import { objectValueToString } from "./utils.js";
19
- const shikiConfig = {
20
- langs: [],
21
- theme: "github-dark",
22
- themes: {},
23
- wrap: false,
24
- transformers: []
25
- };
26
19
  const parser = unified().use(remarkParse).use(remarkGfm).use(remarkFrontmatter, { type: "yaml", marker: "-" }).use(remarkMath).use(remarkRehype, {
27
20
  allowDangerousHtml: true,
28
21
  handlers: {
@@ -33,44 +26,78 @@ const parser = unified().use(remarkParse).use(remarkGfm).use(remarkFrontmatter,
33
26
  scale: 1.4
34
27
  }
35
28
  });
36
- async function visitor(node, idx, parent, render) {
37
- const totalSize = parent.children.length;
38
- const nodeIndex = parent.children.indexOf(node);
39
- const frontmatter = matter(node.value);
40
- const { content, data, isEmpty, excerpt } = frontmatter;
41
- const vfile = { value: content };
42
- const tree = parser.parse(vfile);
43
- const opts = objectValueToString(data);
44
- let result = await parser.run(tree);
45
- console.log("xxxx remark-markmap result: ", JSON.stringify(result, void 0, 2));
46
- console.log("xxxx remark-markmap frontmatter : ", JSON.stringify(frontmatter, void 0, 2), ", opts:", JSON.stringify(opts, void 0, 2));
47
- let mindmapTree = buildTree(result.children, opts);
48
- let svgResults = await render(mindmapTree, opts);
49
- let svgResult = svgResults[0];
50
- if (svgResult.status === "fulfilled") {
51
- let { svg } = svgResult.value;
52
- console.log("remark-markmap visit svg: ", svg);
53
- const hChildren = fromHtmlIsomorphic(svg, { fragment: true, space: "svg" }).children;
54
- parent.children[nodeIndex] = {
55
- type: "paragraph",
56
- children: [{ type: "html", value: svg }],
57
- data: { hChildren }
58
- };
59
- }
60
- return node;
29
+ async function parseMarkmapDiagram(source) {
30
+ const { content, data } = matter(source);
31
+ const tree = parser.parse({ value: content });
32
+ const hast = await parser.run(tree);
33
+ const markmapOptions = objectValueToString({ ...data });
34
+ return {
35
+ diagram: buildTree(hast.children, markmapOptions),
36
+ renderOptions: markmapOptions
37
+ };
61
38
  }
62
- __name(visitor, "visitor");
39
+ __name(parseMarkmapDiagram, "parseMarkmapDiagram");
63
40
  const remarkMarkmap = /* @__PURE__ */ __name((options = {}) => {
64
41
  const render = createMarkmapRenderer(options);
65
42
  return /* @__PURE__ */ __name(async function transformer(ast, file) {
66
- console.log("ast: ", JSON.stringify(ast, void 0, 2));
67
- const matches = [];
68
- visit(ast, { type: "code", lang: "markmap" }, (...args) => {
69
- matches.push([...args, render]);
43
+ const instances = [];
44
+ visit(ast, { type: "code", lang: "markmap" }, (node, _index, parent) => {
45
+ if (parent) {
46
+ instances.push({ node, parent });
47
+ }
70
48
  });
71
- const promises = matches.map((match) => visitor(...match));
72
- await Promise.all(promises);
73
- return ast;
49
+ if (!instances.length) {
50
+ return;
51
+ }
52
+ const prepared = await Promise.all(
53
+ instances.map(async (ancestors) => {
54
+ const node = ancestors.node;
55
+ const parsed = await parseMarkmapDiagram(node.value);
56
+ return {
57
+ instance: ancestors,
58
+ diagram: parsed.diagram,
59
+ renderOptions: {
60
+ ...options,
61
+ ...parsed.renderOptions
62
+ }
63
+ };
64
+ })
65
+ );
66
+ const results = await Promise.all(
67
+ prepared.map(async ({ instance, diagram, renderOptions }) => {
68
+ const [result] = await render([diagram], renderOptions);
69
+ return { instance, result };
70
+ })
71
+ );
72
+ for (const { instance, result } of results) {
73
+ const { node, parent } = instance;
74
+ const nodeIndex = parent.children.indexOf(node);
75
+ if (result.status === "fulfilled") {
76
+ const { svg } = result.value;
77
+ const hChildren = fromHtmlIsomorphic(svg, { fragment: true, space: "svg" }).children;
78
+ parent.children[nodeIndex] = {
79
+ type: "paragraph",
80
+ children: [{ type: "html", value: svg }],
81
+ data: { hChildren }
82
+ };
83
+ } else if (options.errorFallback) {
84
+ const fallback = options.errorFallback(node, String(result.reason), file);
85
+ if (fallback) {
86
+ parent.children[nodeIndex] = fallback;
87
+ } else {
88
+ parent.children.splice(nodeIndex, 1);
89
+ }
90
+ } else {
91
+ const message = file.message(String(result.reason), {
92
+ ruleId: "remark-markmap",
93
+ source: "remark-markmap",
94
+ ancestors: [parent, node]
95
+ });
96
+ message.fatal = true;
97
+ message.url = "https://github.com/anlijiu/remark-markmap";
98
+ throw message;
99
+ }
100
+ }
74
101
  }, "transformer");
75
102
  }, "remarkMarkmap");
76
103
  var remark_markmap_default = remarkMarkmap;
@@ -0,0 +1,2 @@
1
+ export declare function inlineCode(state: any, node: any): any;
2
+ //# sourceMappingURL=handlers.d.ts.map
@@ -0,0 +1,2 @@
1
+ export * from './remark-markmap.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,25 @@
1
+ import { type CreateMarkmapRendererOptions, type RenderOptions } from '@anlijiu/markmap-isomorphic';
2
+ import type { VFile } from 'vfile';
3
+ export interface RemarkMarkmapOptions extends CreateMarkmapRendererOptions, Omit<RenderOptions, 'screenshot'> {
4
+ /**
5
+ * Create a fallback node if processing of a mermaid diagram fails.
6
+ *
7
+ * @param node
8
+ * The mdast `code` node that couldn’t be rendered.
9
+ * @param error
10
+ * The error message that was thrown.
11
+ * @param file
12
+ * The file on which the error occurred.
13
+ * @returns
14
+ * A fallback node to render instead of the invalid diagram. If nothing is returned, the code
15
+ * block is removed
16
+ */
17
+ errorFallback?: (node: any, error: string, file: VFile) => any;
18
+ }
19
+ /**
20
+ * @param options
21
+ * Options that may be used to tweak the output.
22
+ */
23
+ export declare const remarkMarkmap: (options?: RemarkMarkmapOptions) => (ast: any, file: VFile) => Promise<void>;
24
+ export default remarkMarkmap;
25
+ //# sourceMappingURL=remark-markmap.d.ts.map
@@ -0,0 +1,8 @@
1
+ export declare function parseMeta(str?: string): {
2
+ [k: string]: any;
3
+ };
4
+ /**
5
+ * object 的所有value 转成 string
6
+ */
7
+ export declare function objectValueToString(o: any): any;
8
+ //# sourceMappingURL=utils.d.ts.map
package/dist/utils.js CHANGED
@@ -4,9 +4,11 @@ const regexp = /((?<k1>(?!=)\S+)=((?<v1>(["'`])(.*?)\5)|(?<v2>\S+)))|(?<k2>\S+)/
4
4
  const mapToObject = /* @__PURE__ */ __name((map) => Object.fromEntries(map.entries()), "mapToObject");
5
5
  function parseMeta(str) {
6
6
  const io = (str ?? "").matchAll(regexp);
7
- return mapToObject(new Map(
8
- [...io].map((item) => item?.groups).map(({ k1, k2, v1, v2 }) => [k1 ?? k2, v1 ?? v2])
9
- ));
7
+ return mapToObject(
8
+ new Map(
9
+ [...io].map((item) => item?.groups).map(({ k1, k2, v1, v2 }) => [k1 ?? k2, v1 ?? v2])
10
+ )
11
+ );
10
12
  }
11
13
  __name(parseMeta, "parseMeta");
12
14
  function objectValueToString(o) {
package/package.json CHANGED
@@ -1,9 +1,23 @@
1
1
  {
2
2
  "name": "@anlijiu/remark-markmap",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "A code library for render markmap to svg",
5
5
  "main": "dist/index.js",
6
+ "types": "./dist/types/index.d.ts",
6
7
  "type": "module",
8
+ "sideEffects": false,
9
+ "files": [
10
+ "dist",
11
+ "!dist/**/*.map",
12
+ "!dist/tsconfig.tsbuildinfo"
13
+ ],
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/types/index.d.ts",
17
+ "import": "./dist/index.js",
18
+ "default": "./dist/index.js"
19
+ }
20
+ },
7
21
  "keywords": [],
8
22
  "author": "anlijiu@gmail.com",
9
23
  "license": "MIT",
@@ -28,6 +42,9 @@
28
42
  "script:build"
29
43
  ]
30
44
  },
45
+ "build:types": {
46
+ "command": "tsc --emitDeclarationOnly --outDir dist/types"
47
+ },
31
48
  "build:watch": {
32
49
  "command": "node scripts/build.mjs -w",
33
50
  "dependencies": [
@@ -39,34 +56,25 @@
39
56
  }
40
57
  },
41
58
  "devDependencies": {
42
- "@types/cytoscape": "^3.21.9",
43
- "@types/d3-selection": "^3.0.11",
44
- "@types/jest": "^29.5.14",
45
- "@types/lodash": "^4.17.5",
59
+ "@types/d3-selection": "^3.0.13",
60
+ "@types/jest": "^30.0.0",
61
+ "@types/lodash": "^4.17.27",
46
62
  "@types/lodash.uppercase": "^4.3.9",
47
- "@types/node": "^20.14.5",
48
- "@types/nodemon": "^1.19.6",
49
- "esbuild": "^0.25.5",
50
- "esbuild-node-externals": "^1.18.0",
51
- "jest": "^29.7.0",
52
- "nodemon": "^3.1.3",
53
- "rimraf": "^5.0.7",
54
- "ts-jest": "^29.1.5",
55
- "typescript": "^5.8.3",
56
- "wireit": "^0.14.12"
63
+ "@types/node": "^26.6.11",
64
+ "@types/nodemon": "^3.1.1",
65
+ "esbuild": "^0.28.2",
66
+ "esbuild-node-externals": "^2.0.0",
67
+ "jest": "^30.5.2",
68
+ "nodemon": "^3.1.14",
69
+ "rimraf": "^6.1.4",
70
+ "ts-jest": "^29.4.15",
71
+ "typescript": "^7.0.2",
72
+ "wireit": "^0.15.0"
57
73
  },
58
74
  "dependencies": {
59
- "@astrojs/markdown-remark": "^6.3.2",
60
- "cytoscape": "^3.32.0",
61
- "cytoscape-cose-bilkent": "^4.1.0",
62
- "d3-selection": "^3.0.0",
63
- "dompurify": "^3.2.6",
64
75
  "gray-matter": "^4.0.3",
65
76
  "hast-util-from-html-isomorphic": "^2.0.0",
66
- "hast-util-to-html": "^9.0.5",
67
- "hast-util-to-text": "^4.0.2",
68
- "playwright-core": "^1.52.0",
69
- "rehype-katex": "^7.0.1",
77
+ "playwright-core": "^1.65.1",
70
78
  "rehype-mathjax": "^7.1.0",
71
79
  "remark-frontmatter": "^5.0.0",
72
80
  "remark-gfm": "^4.0.1",
@@ -74,22 +82,28 @@
74
82
  "remark-parse": "^11.0.0",
75
83
  "remark-rehype": "^11.1.2",
76
84
  "unified": "^11.0.5",
77
- "unist-util-visit": "^5.0.0",
85
+ "unist-util-visit": "^5.2.0",
78
86
  "vfile": "^6.0.3",
79
- "@anlijiu/markmap-isomorphic": "1.0.1",
80
- "@anlijiu/markmap": "1.0.1"
87
+ "@anlijiu/markmap": "1.0.4",
88
+ "@anlijiu/markmap-isomorphic": "1.0.3"
81
89
  },
82
90
  "repository": {
83
91
  "type": "git",
84
- "url": "https://github.com/anlijiu/remark-markmap.git"
92
+ "url": "https://github.com/anlijiu/remark-markmap.git",
93
+ "directory": "packages/remark-markmap"
94
+ },
95
+ "publishConfig": {
96
+ "access": "public"
85
97
  },
86
98
  "scripts": {
87
99
  "start": "wireit",
88
100
  "dev": "wireit",
89
101
  "build": "wireit",
102
+ "build:types": "wireit",
103
+ "typecheck": "tsc --noEmit",
90
104
  "build:watch": "wireit",
91
105
  "script:build": "wireit",
92
- "test": "jest",
106
+ "test": "jest --passWithNoTests",
93
107
  "test:watch": "jest --watch",
94
108
  "clean": "rimraf dist coverage .wireit dist scripts/build.mjs node_modules"
95
109
  }