markmap-lib 0.15.8 → 0.15.9-alpha.11

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.
@@ -0,0 +1,185 @@
1
+ import { Hook, UrlBuilder, wrapFunction, buildJSItem, persistCSS, persistJS } from "markmap-common";
2
+ import { buildTree } from "markmap-html-parser";
3
+ import { Remarkable } from "remarkable";
4
+ 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';
5
+ const baseJsPaths = [
6
+ `d3@${"7.8.5"}/dist/d3.min.js`,
7
+ `markmap-view@${"0.15.8"}/dist/browser/index.js`
8
+ ];
9
+ function createTransformHooks(transformer) {
10
+ return {
11
+ transformer,
12
+ parser: new Hook(),
13
+ beforeParse: new Hook(),
14
+ afterParse: new Hook(),
15
+ htmltag: new Hook(),
16
+ retransform: new Hook()
17
+ };
18
+ }
19
+ function patchJSItem(urlBuilder, item) {
20
+ if (item.type === "script" && item.data.src) {
21
+ return {
22
+ ...item,
23
+ data: {
24
+ ...item.data,
25
+ src: urlBuilder.getFullUrl(item.data.src)
26
+ }
27
+ };
28
+ }
29
+ return item;
30
+ }
31
+ function patchCSSItem(urlBuilder, item) {
32
+ if (item.type === "stylesheet" && item.data.href) {
33
+ return {
34
+ ...item,
35
+ data: {
36
+ ...item.data,
37
+ href: urlBuilder.getFullUrl(item.data.href)
38
+ }
39
+ };
40
+ }
41
+ return item;
42
+ }
43
+ const builtInPlugins = [];
44
+ function cleanNode(node) {
45
+ while (!node.content && node.children.length === 1) {
46
+ node = node.children[0];
47
+ }
48
+ while (node.children.length === 1 && !node.children[0].content) {
49
+ node = {
50
+ ...node,
51
+ children: node.children[0].children
52
+ };
53
+ }
54
+ return {
55
+ ...node,
56
+ children: node.children.map(cleanNode)
57
+ };
58
+ }
59
+ class Transformer {
60
+ constructor(plugins = builtInPlugins) {
61
+ this.assetsMap = {};
62
+ this.urlBuilder = new UrlBuilder();
63
+ this.hooks = createTransformHooks(this);
64
+ this.plugins = plugins.map(
65
+ (plugin) => typeof plugin === "function" ? plugin() : plugin
66
+ );
67
+ const assetsMap = {};
68
+ for (const { name, transform } of this.plugins) {
69
+ assetsMap[name] = transform(this.hooks);
70
+ }
71
+ this.assetsMap = assetsMap;
72
+ const md = new Remarkable("full", {
73
+ html: true,
74
+ breaks: true,
75
+ maxNesting: Infinity
76
+ });
77
+ md.renderer.rules.htmltag = wrapFunction(
78
+ md.renderer.rules.htmltag,
79
+ (render, ...args) => {
80
+ const result = render(...args);
81
+ this.hooks.htmltag.call({ args, result });
82
+ return result;
83
+ }
84
+ );
85
+ this.md = md;
86
+ this.hooks.parser.call(md);
87
+ }
88
+ transform(content, opts) {
89
+ var _a, _b;
90
+ const context = {
91
+ content,
92
+ features: {},
93
+ contentLineOffset: 0
94
+ };
95
+ this.hooks.beforeParse.call(this.md, context);
96
+ const html = this.md.render(context.content, {});
97
+ this.hooks.afterParse.call(this.md, context);
98
+ const root = cleanNode(
99
+ buildTree(html, {
100
+ ...(_b = (_a = context.frontmatter) == null ? void 0 : _a.markmap) == null ? void 0 : _b.htmlParser,
101
+ ...opts
102
+ })
103
+ );
104
+ return { ...context, root };
105
+ }
106
+ /**
107
+ * Get all assets from enabled plugins or filter them by plugin names as keys.
108
+ */
109
+ getAssets(keys) {
110
+ const styles = [];
111
+ const scripts = [];
112
+ keys ?? (keys = this.plugins.map((plugin) => plugin.name));
113
+ for (const assets of keys.map((key) => this.assetsMap[key])) {
114
+ if (assets) {
115
+ if (assets.styles)
116
+ styles.push(...assets.styles);
117
+ if (assets.scripts)
118
+ scripts.push(...assets.scripts);
119
+ }
120
+ }
121
+ return {
122
+ styles: styles.map((item) => patchCSSItem(this.urlBuilder, item)),
123
+ scripts: scripts.map((item) => patchJSItem(this.urlBuilder, item))
124
+ };
125
+ }
126
+ /**
127
+ * Get used assets by features object returned by `transform`.
128
+ */
129
+ getUsedAssets(features) {
130
+ const keys = this.plugins.map((plugin) => plugin.name).filter((name) => features[name]);
131
+ return this.getAssets(keys);
132
+ }
133
+ fillTemplate(root, assets, extra) {
134
+ extra = {
135
+ ...extra
136
+ };
137
+ extra.baseJs ?? (extra.baseJs = baseJsPaths.map((path) => this.urlBuilder.getFullUrl(path)).map((path) => buildJSItem(path)));
138
+ const { scripts, styles } = assets;
139
+ const cssList = [...styles ? persistCSS(styles) : []];
140
+ const context = {
141
+ getMarkmap: () => window.markmap,
142
+ getOptions: extra.getOptions,
143
+ jsonOptions: extra.jsonOptions,
144
+ root
145
+ };
146
+ const jsList = [
147
+ ...persistJS(
148
+ [
149
+ ...extra.baseJs,
150
+ ...scripts || [],
151
+ {
152
+ type: "iife",
153
+ data: {
154
+ fn: (getMarkmap, getOptions, root2, jsonOptions) => {
155
+ const markmap = getMarkmap();
156
+ window.mm = markmap.Markmap.create(
157
+ "svg#mindmap",
158
+ (getOptions || markmap.deriveOptions)(jsonOptions),
159
+ root2
160
+ );
161
+ },
162
+ getParams: ({ getMarkmap, getOptions, root: root2, jsonOptions }) => {
163
+ return [getMarkmap, getOptions, root2, jsonOptions];
164
+ }
165
+ }
166
+ }
167
+ ],
168
+ context
169
+ )
170
+ ];
171
+ const html = template.replace("<!--CSS-->", () => cssList.join("")).replace("<!--JS-->", () => jsList.join(""));
172
+ return html;
173
+ }
174
+ }
175
+ const transformerVersions = {
176
+ "markmap-lib": "process.env.VERSION",
177
+ d3: "7.8.5"
178
+ };
179
+ export {
180
+ Transformer,
181
+ baseJsPaths,
182
+ builtInPlugins,
183
+ template,
184
+ transformerVersions
185
+ };
@@ -0,0 +1,2 @@
1
+ declare const plugin: import("../../types").ITransformPlugin;
2
+ export default plugin;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../../types").ITransformPlugin;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare const plugin: import("../../types").ITransformPlugin;
2
+ export default plugin;
@@ -0,0 +1,2 @@
1
+ declare const plugin: import("../../types").ITransformPlugin;
2
+ export default plugin;
@@ -0,0 +1,2 @@
1
+ declare const plugin: import("../../types").ITransformPlugin;
2
+ export default plugin;
@@ -0,0 +1,2 @@
1
+ declare const plugin: import("../../types").ITransformPlugin;
2
+ export default plugin;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../../types").ITransformPlugin;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare const plugin: import("../../types").ITransformPlugin;
2
+ export default plugin;
@@ -0,0 +1,2 @@
1
+ declare const plugin: import("../../types").ITransformPlugin;
2
+ export default plugin;
@@ -0,0 +1,2 @@
1
+ declare const plugin: import("../../types").ITransformPlugin;
2
+ export default plugin;
@@ -0,0 +1,299 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const markmapCommon = require("markmap-common");
4
+ const yaml = require("js-yaml");
5
+ const hljs$1 = require("highlight.js");
6
+ const remarkableKatex = require("remarkable-katex");
7
+ function createTransformHooks(transformer) {
8
+ return {
9
+ transformer,
10
+ parser: new markmapCommon.Hook(),
11
+ beforeParse: new markmapCommon.Hook(),
12
+ afterParse: new markmapCommon.Hook(),
13
+ htmltag: new markmapCommon.Hook(),
14
+ retransform: new markmapCommon.Hook()
15
+ };
16
+ }
17
+ function definePlugin(plugin2) {
18
+ return plugin2;
19
+ }
20
+ 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>`;
21
+ 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>`;
22
+ const name$5 = "checkbox";
23
+ const plugin$3 = definePlugin({
24
+ name: name$5,
25
+ transform(transformHooks) {
26
+ transformHooks.parser.tap((md) => {
27
+ md.core.ruler.before(
28
+ "inline",
29
+ "checkbox",
30
+ (state) => {
31
+ for (let i = 2; i < state.tokens.length; i += 1) {
32
+ const token = state.tokens[i];
33
+ if (token.type === "inline" && token.content) {
34
+ const prevType = state.tokens[i - 1].type;
35
+ const prevPrevType = state.tokens[i - 2].type;
36
+ if (prevType === "heading_open" || prevType === "paragraph_open" && prevPrevType === "list_item_open") {
37
+ if (token.content.startsWith("[ ]")) {
38
+ token.content = `${svg_unmarked} ${token.content.slice(3)}`;
39
+ } else if (token.content.startsWith("[x]")) {
40
+ token.content = `${svg_marked} ${token.content.slice(3)}`;
41
+ }
42
+ }
43
+ }
44
+ }
45
+ return false;
46
+ },
47
+ {}
48
+ );
49
+ });
50
+ return {};
51
+ }
52
+ });
53
+ const checkbox = plugin$3;
54
+ const name$4 = "frontmatter";
55
+ const frontmatter = definePlugin({
56
+ name: name$4,
57
+ transform(transformHooks) {
58
+ transformHooks.beforeParse.tap((md, context) => {
59
+ const { content } = context;
60
+ if (!/^---\r?\n/.test(content))
61
+ return;
62
+ const match = /\n---\r?\n/.exec(content);
63
+ if (!match)
64
+ return;
65
+ const raw = content.slice(4, match.index);
66
+ let frontmatter2;
67
+ try {
68
+ frontmatter2 = yaml.load(raw);
69
+ if (frontmatter2 == null ? void 0 : frontmatter2.markmap) {
70
+ frontmatter2.markmap = normalizeMarkmapJsonOptions(
71
+ frontmatter2.markmap
72
+ );
73
+ }
74
+ } catch {
75
+ return;
76
+ }
77
+ context.frontmatter = frontmatter2;
78
+ context.content = content.slice(match.index + match[0].length);
79
+ context.contentLineOffset = content.slice(0, match.index).split("\n").length + 1;
80
+ });
81
+ return {};
82
+ }
83
+ });
84
+ function normalizeMarkmapJsonOptions(options) {
85
+ if (!options)
86
+ return;
87
+ ["color", "extraJs", "extraCss"].forEach((key) => {
88
+ if (options[key] != null)
89
+ options[key] = normalizeStringArray(options[key]);
90
+ });
91
+ ["duration", "maxWidth", "initialExpandLevel"].forEach((key) => {
92
+ if (options[key] != null)
93
+ options[key] = normalizeNumber(options[key]);
94
+ });
95
+ return options;
96
+ }
97
+ function normalizeStringArray(value) {
98
+ let result;
99
+ if (typeof value === "string")
100
+ result = [value];
101
+ else if (Array.isArray(value))
102
+ result = value.filter((item) => item && typeof item === "string");
103
+ return (result == null ? void 0 : result.length) ? result : void 0;
104
+ }
105
+ function normalizeNumber(value) {
106
+ if (isNaN(+value))
107
+ return;
108
+ return +value;
109
+ }
110
+ const name$3 = "hljs";
111
+ const preloadScripts$1 = [
112
+ `@highlightjs/cdn-assets@${"11.8.0"}/highlight.min.js`
113
+ ].map((path) => markmapCommon.buildJSItem(path));
114
+ const styles$1 = [
115
+ `@highlightjs/cdn-assets@${"11.8.0"}/styles/default.min.css`
116
+ // `highlight.js@${process.env.HLJS_VERSION}/styles/default.css`,
117
+ ].map((path) => markmapCommon.buildCSSItem(path));
118
+ const config$1 = {
119
+ versions: {
120
+ hljs: "11.8.0"
121
+ },
122
+ preloadScripts: preloadScripts$1,
123
+ styles: styles$1
124
+ };
125
+ const plugin$2 = definePlugin({
126
+ name: name$3,
127
+ config: config$1,
128
+ transform(transformHooks) {
129
+ var _a;
130
+ let enableFeature = markmapCommon.noop;
131
+ transformHooks.parser.tap((md) => {
132
+ md.set({
133
+ highlight: (str, language) => {
134
+ enableFeature();
135
+ return hljs$1.highlightAuto(str, language ? [language] : void 0).value;
136
+ }
137
+ });
138
+ });
139
+ transformHooks.beforeParse.tap((_, context) => {
140
+ enableFeature = () => {
141
+ context.features[name$3] = true;
142
+ };
143
+ });
144
+ return {
145
+ styles: (_a = plugin$2.config) == null ? void 0 : _a.styles
146
+ };
147
+ }
148
+ });
149
+ const hljs = plugin$2;
150
+ const name$2 = "katex";
151
+ const preloadScripts = [
152
+ `katex@${"0.16.8"}/dist/katex.min.js`
153
+ ].map((path) => markmapCommon.buildJSItem(path));
154
+ const webfontloader = markmapCommon.buildJSItem(
155
+ `webfontloader@${"1.6.28"}/webfontloader.js`
156
+ );
157
+ webfontloader.data.defer = true;
158
+ const styles = [`katex@${"0.16.8"}/dist/katex.min.css`].map(
159
+ (path) => markmapCommon.buildCSSItem(path)
160
+ );
161
+ const config = {
162
+ versions: {
163
+ katex: "0.16.8",
164
+ webfontloader: "1.6.28"
165
+ },
166
+ preloadScripts,
167
+ scripts: [
168
+ {
169
+ type: "iife",
170
+ data: {
171
+ fn: (getMarkmap) => {
172
+ window.WebFontConfig = {
173
+ custom: {
174
+ families: [
175
+ "KaTeX_AMS",
176
+ "KaTeX_Caligraphic:n4,n7",
177
+ "KaTeX_Fraktur:n4,n7",
178
+ "KaTeX_Main:n4,n7,i4,i7",
179
+ "KaTeX_Math:i4,i7",
180
+ "KaTeX_Script",
181
+ "KaTeX_SansSerif:n4,n7,i4",
182
+ "KaTeX_Size1",
183
+ "KaTeX_Size2",
184
+ "KaTeX_Size3",
185
+ "KaTeX_Size4",
186
+ "KaTeX_Typewriter"
187
+ ]
188
+ },
189
+ active: () => {
190
+ getMarkmap().refreshHook.call();
191
+ }
192
+ };
193
+ },
194
+ getParams({ getMarkmap }) {
195
+ return [getMarkmap];
196
+ }
197
+ }
198
+ },
199
+ webfontloader
200
+ ],
201
+ styles
202
+ };
203
+ const plugin$1 = definePlugin({
204
+ name: name$2,
205
+ config,
206
+ transform(transformHooks) {
207
+ var _a, _b;
208
+ let enableFeature = markmapCommon.noop;
209
+ transformHooks.parser.tap((md) => {
210
+ md.use(remarkableKatex);
211
+ md.renderer.rules.katex = markmapCommon.wrapFunction(
212
+ md.renderer.rules.katex,
213
+ (render, ...args) => {
214
+ enableFeature();
215
+ return render(...args);
216
+ }
217
+ );
218
+ });
219
+ transformHooks.beforeParse.tap((_, context) => {
220
+ enableFeature = () => {
221
+ context.features[name$2] = true;
222
+ };
223
+ });
224
+ return {
225
+ styles: (_a = plugin$1.config) == null ? void 0 : _a.styles,
226
+ scripts: (_b = plugin$1.config) == null ? void 0 : _b.scripts
227
+ };
228
+ }
229
+ });
230
+ const katex = plugin$1;
231
+ const name$1 = "npmUrl";
232
+ const npmUrl = definePlugin({
233
+ name: name$1,
234
+ transform(transformHooks) {
235
+ transformHooks.afterParse.tap((_, context) => {
236
+ const { frontmatter: frontmatter2 } = context;
237
+ const markmap = frontmatter2 == null ? void 0 : frontmatter2.markmap;
238
+ if (markmap) {
239
+ ["extraJs", "extraCss"].forEach((key) => {
240
+ const value = markmap[key];
241
+ if (value) {
242
+ markmap[key] = value.map((path) => {
243
+ if (path.startsWith("npm:")) {
244
+ return transformHooks.transformer.urlBuilder.getFullUrl(
245
+ path.slice(4)
246
+ );
247
+ }
248
+ return path;
249
+ });
250
+ }
251
+ });
252
+ }
253
+ });
254
+ return {};
255
+ }
256
+ });
257
+ const name = "sourceLines";
258
+ const plugin = definePlugin({
259
+ name,
260
+ transform(transformHooks) {
261
+ transformHooks.parser.tap((md) => {
262
+ Object.entries(md.renderer.rules).forEach(([key, value]) => {
263
+ if (typeof value === "function") {
264
+ md.renderer.rules[key] = patchRule(value);
265
+ } else {
266
+ Object.entries(value).forEach(([k, v]) => {
267
+ value[k] = patchRule(v);
268
+ });
269
+ }
270
+ });
271
+ });
272
+ return {};
273
+ }
274
+ });
275
+ function patchRule(rule, _key) {
276
+ return markmapCommon.wrapFunction(rule, (render, tokens, idx, ...rest) => {
277
+ let html = render(tokens, idx, ...rest);
278
+ const { lines } = tokens[idx];
279
+ if (lines) {
280
+ html = html.replace(
281
+ /^<[\w-]+/,
282
+ (m) => `${m} data-lines="${lines.join(",")}"`
283
+ );
284
+ }
285
+ return html;
286
+ });
287
+ }
288
+ const sourceLines = plugin;
289
+ const plugins = [
290
+ frontmatter,
291
+ katex,
292
+ hljs,
293
+ npmUrl,
294
+ checkbox,
295
+ sourceLines
296
+ ];
297
+ exports.createTransformHooks = createTransformHooks;
298
+ exports.definePlugin = definePlugin;
299
+ exports.plugins = plugins;