markmap-lib 0.15.9-alpha.5 → 0.16.0
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/browser/index.iife.js +417 -482
- package/dist/browser/index.mjs +242 -248
- package/dist/index.d.ts +1 -3
- package/dist/index.js +17 -314
- package/dist/index.mjs +18 -315
- package/dist/index.no-plugins.js +136 -0
- package/dist/index.no-plugins.mjs +136 -0
- package/dist/plugins/checkbox/index.d.ts +2 -0
- package/dist/plugins/frontmatter/index.d.ts +2 -0
- package/dist/plugins/hljs/index.browser.d.ts +2 -0
- package/dist/plugins/hljs/index.d.ts +2 -0
- package/dist/plugins/index.d.ts +7 -0
- package/dist/plugins/katex/index.browser.d.ts +2 -0
- package/dist/plugins/katex/index.d.ts +2 -0
- package/dist/plugins/npm-url/index.d.ts +2 -0
- package/dist/plugins/prism/index.browser.d.ts +2 -0
- package/dist/plugins/prism/index.d.ts +2 -0
- package/dist/plugins/source-lines/index.d.ts +2 -0
- package/dist/plugins.js +307 -0
- package/dist/plugins.mjs +307 -0
- package/dist/transform.d.ts +3 -9
- package/package.json +23 -7
- package/dist/constants.d.ts +0 -4
- package/dist/plugins/checkbox.d.ts +0 -2
- package/dist/plugins/frontmatter.d.ts +0 -2
- package/dist/plugins/hljs.browser.d.ts +0 -2
- package/dist/plugins/hljs.d.ts +0 -2
- package/dist/plugins/katex.browser.d.ts +0 -2
- package/dist/plugins/katex.d.ts +0 -2
- package/dist/plugins/npm-url.d.ts +0 -2
- package/dist/plugins/prism.browser.d.ts +0 -2
- package/dist/plugins/prism.d.ts +0 -2
- /package/dist/plugins/{hljs.config.d.ts → hljs/config.d.ts} +0 -0
- /package/dist/plugins/{katex.config.d.ts → katex/config.d.ts} +0 -0
- /package/dist/plugins/{prism.config.d.ts → prism/config.d.ts} +0 -0
package/dist/index.mjs
CHANGED
|
@@ -1,262 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UrlBuilder, wrapFunction } from "markmap-common";
|
|
2
2
|
import { buildTree } from "markmap-html-parser";
|
|
3
3
|
import { Remarkable } from "remarkable";
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
const baseJsPaths = [
|
|
9
|
-
`d3@${"7.8.5"}/dist/d3.min.js`,
|
|
10
|
-
`markmap-view@${"0.15.9-alpha.5"}/dist/browser/index.js`
|
|
11
|
-
];
|
|
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";
|
|
15
|
-
const preloadScripts$1 = [
|
|
16
|
-
`katex@${"0.16.8"}/dist/katex.min.js`
|
|
17
|
-
].map((path) => buildJSItem(path));
|
|
18
|
-
const webfontloader = buildJSItem(
|
|
19
|
-
`webfontloader@${"1.6.28"}/webfontloader.js`
|
|
20
|
-
);
|
|
21
|
-
webfontloader.data.defer = true;
|
|
22
|
-
const styles$1 = [`katex@${"0.16.8"}/dist/katex.min.css`].map(
|
|
23
|
-
(path) => buildCSSItem(path)
|
|
24
|
-
);
|
|
25
|
-
const config$1 = {
|
|
26
|
-
versions: {
|
|
27
|
-
katex: "0.16.8",
|
|
28
|
-
webfontloader: "1.6.28"
|
|
29
|
-
},
|
|
30
|
-
preloadScripts: preloadScripts$1,
|
|
31
|
-
scripts: [
|
|
32
|
-
{
|
|
33
|
-
type: "iife",
|
|
34
|
-
data: {
|
|
35
|
-
fn: (getMarkmap) => {
|
|
36
|
-
window.WebFontConfig = {
|
|
37
|
-
custom: {
|
|
38
|
-
families: [
|
|
39
|
-
"KaTeX_AMS",
|
|
40
|
-
"KaTeX_Caligraphic:n4,n7",
|
|
41
|
-
"KaTeX_Fraktur:n4,n7",
|
|
42
|
-
"KaTeX_Main:n4,n7,i4,i7",
|
|
43
|
-
"KaTeX_Math:i4,i7",
|
|
44
|
-
"KaTeX_Script",
|
|
45
|
-
"KaTeX_SansSerif:n4,n7,i4",
|
|
46
|
-
"KaTeX_Size1",
|
|
47
|
-
"KaTeX_Size2",
|
|
48
|
-
"KaTeX_Size3",
|
|
49
|
-
"KaTeX_Size4",
|
|
50
|
-
"KaTeX_Typewriter"
|
|
51
|
-
]
|
|
52
|
-
},
|
|
53
|
-
active: () => {
|
|
54
|
-
getMarkmap().refreshHook.call();
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
},
|
|
58
|
-
getParams({ getMarkmap }) {
|
|
59
|
-
return [getMarkmap];
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
webfontloader
|
|
64
|
-
],
|
|
65
|
-
styles: styles$1
|
|
66
|
-
};
|
|
67
|
-
function createTransformHooks(transformer) {
|
|
68
|
-
return {
|
|
69
|
-
transformer,
|
|
70
|
-
parser: new Hook(),
|
|
71
|
-
beforeParse: new Hook(),
|
|
72
|
-
afterParse: new Hook(),
|
|
73
|
-
htmltag: new Hook(),
|
|
74
|
-
retransform: new Hook()
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
function definePlugin(plugin2) {
|
|
78
|
-
return plugin2;
|
|
79
|
-
}
|
|
80
|
-
const plugin$2 = definePlugin({
|
|
81
|
-
name: name$4,
|
|
82
|
-
config: config$1,
|
|
83
|
-
transform(transformHooks) {
|
|
84
|
-
var _a, _b;
|
|
85
|
-
let enableFeature = noop;
|
|
86
|
-
transformHooks.parser.tap((md) => {
|
|
87
|
-
md.use(remarkableKatex);
|
|
88
|
-
md.renderer.rules.katex = wrapFunction(
|
|
89
|
-
md.renderer.rules.katex,
|
|
90
|
-
(render, ...args) => {
|
|
91
|
-
enableFeature();
|
|
92
|
-
return render(...args);
|
|
93
|
-
}
|
|
94
|
-
);
|
|
95
|
-
});
|
|
96
|
-
transformHooks.beforeParse.tap((_, context) => {
|
|
97
|
-
enableFeature = () => {
|
|
98
|
-
context.features[name$4] = true;
|
|
99
|
-
};
|
|
100
|
-
});
|
|
101
|
-
return {
|
|
102
|
-
styles: (_a = plugin$2.config) == null ? void 0 : _a.styles,
|
|
103
|
-
scripts: (_b = plugin$2.config) == null ? void 0 : _b.scripts
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
const name$3 = "frontmatter";
|
|
108
|
-
const frontmatter = definePlugin({
|
|
109
|
-
name: name$3,
|
|
110
|
-
transform(transformHooks) {
|
|
111
|
-
transformHooks.beforeParse.tap((md, context) => {
|
|
112
|
-
const { content } = context;
|
|
113
|
-
if (!/^---\r?\n/.test(content))
|
|
114
|
-
return;
|
|
115
|
-
const match = /\n---\r?\n/.exec(content);
|
|
116
|
-
if (!match)
|
|
117
|
-
return;
|
|
118
|
-
const raw = content.slice(4, match.index);
|
|
119
|
-
let frontmatter2;
|
|
120
|
-
try {
|
|
121
|
-
frontmatter2 = yaml.load(raw);
|
|
122
|
-
if (frontmatter2 == null ? void 0 : frontmatter2.markmap) {
|
|
123
|
-
frontmatter2.markmap = normalizeMarkmapJsonOptions(
|
|
124
|
-
frontmatter2.markmap
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
} catch {
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
context.frontmatter = frontmatter2;
|
|
131
|
-
context.content = content.slice(match.index + match[0].length);
|
|
132
|
-
context.contentLineOffset = content.slice(0, match.index).split("\n").length + 1;
|
|
133
|
-
});
|
|
134
|
-
return {};
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
function normalizeMarkmapJsonOptions(options) {
|
|
138
|
-
if (!options)
|
|
139
|
-
return;
|
|
140
|
-
["color", "extraJs", "extraCss"].forEach((key) => {
|
|
141
|
-
if (options[key] != null)
|
|
142
|
-
options[key] = normalizeStringArray(options[key]);
|
|
143
|
-
});
|
|
144
|
-
["duration", "maxWidth", "initialExpandLevel"].forEach((key) => {
|
|
145
|
-
if (options[key] != null)
|
|
146
|
-
options[key] = normalizeNumber(options[key]);
|
|
147
|
-
});
|
|
148
|
-
return options;
|
|
149
|
-
}
|
|
150
|
-
function normalizeStringArray(value) {
|
|
151
|
-
let result;
|
|
152
|
-
if (typeof value === "string")
|
|
153
|
-
result = [value];
|
|
154
|
-
else if (Array.isArray(value))
|
|
155
|
-
result = value.filter((item) => item && typeof item === "string");
|
|
156
|
-
return (result == null ? void 0 : result.length) ? result : void 0;
|
|
157
|
-
}
|
|
158
|
-
function normalizeNumber(value) {
|
|
159
|
-
if (isNaN(+value))
|
|
160
|
-
return;
|
|
161
|
-
return +value;
|
|
162
|
-
}
|
|
163
|
-
const name$2 = "npmUrl";
|
|
164
|
-
const npmUrl = definePlugin({
|
|
165
|
-
name: name$2,
|
|
166
|
-
transform(transformHooks) {
|
|
167
|
-
transformHooks.afterParse.tap((_, context) => {
|
|
168
|
-
const { frontmatter: frontmatter2 } = context;
|
|
169
|
-
const markmap = frontmatter2 == null ? void 0 : frontmatter2.markmap;
|
|
170
|
-
if (markmap) {
|
|
171
|
-
["extraJs", "extraCss"].forEach((key) => {
|
|
172
|
-
const value = markmap[key];
|
|
173
|
-
if (value) {
|
|
174
|
-
markmap[key] = value.map((path) => {
|
|
175
|
-
if (path.startsWith("npm:")) {
|
|
176
|
-
return transformHooks.transformer.urlBuilder.getFullUrl(
|
|
177
|
-
path.slice(4)
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
return path;
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
return {};
|
|
187
|
-
}
|
|
188
|
-
});
|
|
189
|
-
const name$1 = "hljs";
|
|
190
|
-
const preloadScripts = [
|
|
191
|
-
`@highlightjs/cdn-assets@${"11.8.0"}/highlight.min.js`
|
|
192
|
-
].map((path) => buildJSItem(path));
|
|
193
|
-
const styles = [
|
|
194
|
-
`@highlightjs/cdn-assets@${"11.8.0"}/styles/default.min.css`
|
|
195
|
-
// `highlight.js@${process.env.HLJS_VERSION}/styles/default.css`,
|
|
196
|
-
].map((path) => buildCSSItem(path));
|
|
197
|
-
const config = {
|
|
198
|
-
versions: {
|
|
199
|
-
hljs: "11.8.0"
|
|
200
|
-
},
|
|
201
|
-
preloadScripts,
|
|
202
|
-
styles
|
|
203
|
-
};
|
|
204
|
-
const plugin$1 = definePlugin({
|
|
205
|
-
name: name$1,
|
|
206
|
-
config,
|
|
207
|
-
transform(transformHooks) {
|
|
208
|
-
var _a;
|
|
209
|
-
let enableFeature = noop;
|
|
210
|
-
transformHooks.parser.tap((md) => {
|
|
211
|
-
md.set({
|
|
212
|
-
highlight: (str, language) => {
|
|
213
|
-
enableFeature();
|
|
214
|
-
return hljs.highlightAuto(str, language ? [language] : void 0).value;
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
});
|
|
218
|
-
transformHooks.beforeParse.tap((_, context) => {
|
|
219
|
-
enableFeature = () => {
|
|
220
|
-
context.features[name$1] = true;
|
|
221
|
-
};
|
|
222
|
-
});
|
|
223
|
-
return {
|
|
224
|
-
styles: (_a = plugin$1.config) == null ? void 0 : _a.styles
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
});
|
|
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];
|
|
4
|
+
import { plugins, createTransformHooks } from "./plugins.mjs";
|
|
5
|
+
import "js-yaml";
|
|
6
|
+
import "highlight.js";
|
|
7
|
+
import "remarkable-katex";
|
|
260
8
|
function patchJSItem(urlBuilder, item) {
|
|
261
9
|
if (item.type === "script" && item.data.src) {
|
|
262
10
|
return {
|
|
@@ -281,6 +29,7 @@ function patchCSSItem(urlBuilder, item) {
|
|
|
281
29
|
}
|
|
282
30
|
return item;
|
|
283
31
|
}
|
|
32
|
+
const builtInPlugins = plugins;
|
|
284
33
|
function cleanNode(node) {
|
|
285
34
|
while (!node.content && node.children.length === 1) {
|
|
286
35
|
node = node.children[0];
|
|
@@ -297,16 +46,16 @@ function cleanNode(node) {
|
|
|
297
46
|
};
|
|
298
47
|
}
|
|
299
48
|
class Transformer {
|
|
300
|
-
constructor(
|
|
49
|
+
constructor(plugins2 = builtInPlugins) {
|
|
301
50
|
this.assetsMap = {};
|
|
302
51
|
this.urlBuilder = new UrlBuilder();
|
|
303
52
|
this.hooks = createTransformHooks(this);
|
|
304
|
-
this.plugins =
|
|
305
|
-
(
|
|
53
|
+
this.plugins = plugins2.map(
|
|
54
|
+
(plugin) => typeof plugin === "function" ? plugin() : plugin
|
|
306
55
|
);
|
|
307
56
|
const assetsMap = {};
|
|
308
|
-
for (const { name
|
|
309
|
-
assetsMap[
|
|
57
|
+
for (const { name, transform } of this.plugins) {
|
|
58
|
+
assetsMap[name] = transform(this.hooks);
|
|
310
59
|
}
|
|
311
60
|
this.assetsMap = assetsMap;
|
|
312
61
|
const md = new Remarkable("full", {
|
|
@@ -347,19 +96,19 @@ class Transformer {
|
|
|
347
96
|
* Get all assets from enabled plugins or filter them by plugin names as keys.
|
|
348
97
|
*/
|
|
349
98
|
getAssets(keys) {
|
|
350
|
-
const
|
|
99
|
+
const styles = [];
|
|
351
100
|
const scripts = [];
|
|
352
|
-
keys ?? (keys = this.plugins.map((
|
|
101
|
+
keys ?? (keys = this.plugins.map((plugin) => plugin.name));
|
|
353
102
|
for (const assets of keys.map((key) => this.assetsMap[key])) {
|
|
354
103
|
if (assets) {
|
|
355
104
|
if (assets.styles)
|
|
356
|
-
|
|
105
|
+
styles.push(...assets.styles);
|
|
357
106
|
if (assets.scripts)
|
|
358
107
|
scripts.push(...assets.scripts);
|
|
359
108
|
}
|
|
360
109
|
}
|
|
361
110
|
return {
|
|
362
|
-
styles:
|
|
111
|
+
styles: styles.map((item) => patchCSSItem(this.urlBuilder, item)),
|
|
363
112
|
scripts: scripts.map((item) => patchJSItem(this.urlBuilder, item))
|
|
364
113
|
};
|
|
365
114
|
}
|
|
@@ -367,61 +116,15 @@ class Transformer {
|
|
|
367
116
|
* Get used assets by features object returned by `transform`.
|
|
368
117
|
*/
|
|
369
118
|
getUsedAssets(features) {
|
|
370
|
-
const keys = this.plugins.map((
|
|
119
|
+
const keys = this.plugins.map((plugin) => plugin.name).filter((name) => features[name]);
|
|
371
120
|
return this.getAssets(keys);
|
|
372
121
|
}
|
|
373
|
-
fillTemplate(root, assets, extra) {
|
|
374
|
-
extra = {
|
|
375
|
-
...extra
|
|
376
|
-
};
|
|
377
|
-
extra.baseJs ?? (extra.baseJs = baseJsPaths.map((path) => this.urlBuilder.getFullUrl(path)).map((path) => buildJSItem(path)));
|
|
378
|
-
const { scripts, styles: styles2 } = assets;
|
|
379
|
-
const cssList = [...styles2 ? persistCSS(styles2) : []];
|
|
380
|
-
const context = {
|
|
381
|
-
getMarkmap: () => window.markmap,
|
|
382
|
-
getOptions: extra.getOptions,
|
|
383
|
-
jsonOptions: extra.jsonOptions,
|
|
384
|
-
root
|
|
385
|
-
};
|
|
386
|
-
const jsList = [
|
|
387
|
-
...persistJS(
|
|
388
|
-
[
|
|
389
|
-
...extra.baseJs,
|
|
390
|
-
...scripts || [],
|
|
391
|
-
{
|
|
392
|
-
type: "iife",
|
|
393
|
-
data: {
|
|
394
|
-
fn: (getMarkmap, getOptions, root2, jsonOptions) => {
|
|
395
|
-
const markmap = getMarkmap();
|
|
396
|
-
window.mm = markmap.Markmap.create(
|
|
397
|
-
"svg#mindmap",
|
|
398
|
-
(getOptions || markmap.deriveOptions)(jsonOptions),
|
|
399
|
-
root2
|
|
400
|
-
);
|
|
401
|
-
},
|
|
402
|
-
getParams: ({ getMarkmap, getOptions, root: root2, jsonOptions }) => {
|
|
403
|
-
return [getMarkmap, getOptions, root2, jsonOptions];
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
],
|
|
408
|
-
context
|
|
409
|
-
)
|
|
410
|
-
];
|
|
411
|
-
const html = template.replace("<!--CSS-->", () => cssList.join("")).replace("<!--JS-->", () => jsList.join(""));
|
|
412
|
-
return html;
|
|
413
|
-
}
|
|
414
122
|
}
|
|
415
123
|
const transformerVersions = {
|
|
416
|
-
"markmap-lib": "
|
|
417
|
-
d3: "7.8.5"
|
|
124
|
+
"markmap-lib": "0.16.0"
|
|
418
125
|
};
|
|
419
126
|
export {
|
|
420
127
|
Transformer,
|
|
421
|
-
|
|
422
|
-
plugins as builtInPlugins,
|
|
423
|
-
svg_marked,
|
|
424
|
-
svg_unmarked,
|
|
425
|
-
template,
|
|
128
|
+
builtInPlugins,
|
|
426
129
|
transformerVersions
|
|
427
130
|
};
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const markmapCommon = require("markmap-common");
|
|
4
|
+
const markmapHtmlParser = require("markmap-html-parser");
|
|
5
|
+
const remarkable = require("remarkable");
|
|
6
|
+
function createTransformHooks(transformer) {
|
|
7
|
+
return {
|
|
8
|
+
transformer,
|
|
9
|
+
parser: new markmapCommon.Hook(),
|
|
10
|
+
beforeParse: new markmapCommon.Hook(),
|
|
11
|
+
afterParse: new markmapCommon.Hook(),
|
|
12
|
+
htmltag: new markmapCommon.Hook(),
|
|
13
|
+
retransform: new markmapCommon.Hook()
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function patchJSItem(urlBuilder, item) {
|
|
17
|
+
if (item.type === "script" && item.data.src) {
|
|
18
|
+
return {
|
|
19
|
+
...item,
|
|
20
|
+
data: {
|
|
21
|
+
...item.data,
|
|
22
|
+
src: urlBuilder.getFullUrl(item.data.src)
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return item;
|
|
27
|
+
}
|
|
28
|
+
function patchCSSItem(urlBuilder, item) {
|
|
29
|
+
if (item.type === "stylesheet" && item.data.href) {
|
|
30
|
+
return {
|
|
31
|
+
...item,
|
|
32
|
+
data: {
|
|
33
|
+
...item.data,
|
|
34
|
+
href: urlBuilder.getFullUrl(item.data.href)
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return item;
|
|
39
|
+
}
|
|
40
|
+
const builtInPlugins = [];
|
|
41
|
+
function cleanNode(node) {
|
|
42
|
+
while (!node.content && node.children.length === 1) {
|
|
43
|
+
node = node.children[0];
|
|
44
|
+
}
|
|
45
|
+
while (node.children.length === 1 && !node.children[0].content) {
|
|
46
|
+
node = {
|
|
47
|
+
...node,
|
|
48
|
+
children: node.children[0].children
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
...node,
|
|
53
|
+
children: node.children.map(cleanNode)
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
class Transformer {
|
|
57
|
+
constructor(plugins = builtInPlugins) {
|
|
58
|
+
this.assetsMap = {};
|
|
59
|
+
this.urlBuilder = new markmapCommon.UrlBuilder();
|
|
60
|
+
this.hooks = createTransformHooks(this);
|
|
61
|
+
this.plugins = plugins.map(
|
|
62
|
+
(plugin) => typeof plugin === "function" ? plugin() : plugin
|
|
63
|
+
);
|
|
64
|
+
const assetsMap = {};
|
|
65
|
+
for (const { name, transform } of this.plugins) {
|
|
66
|
+
assetsMap[name] = transform(this.hooks);
|
|
67
|
+
}
|
|
68
|
+
this.assetsMap = assetsMap;
|
|
69
|
+
const md = new remarkable.Remarkable("full", {
|
|
70
|
+
html: true,
|
|
71
|
+
breaks: true,
|
|
72
|
+
maxNesting: Infinity
|
|
73
|
+
});
|
|
74
|
+
md.renderer.rules.htmltag = markmapCommon.wrapFunction(
|
|
75
|
+
md.renderer.rules.htmltag,
|
|
76
|
+
(render, ...args) => {
|
|
77
|
+
const result = render(...args);
|
|
78
|
+
this.hooks.htmltag.call({ args, result });
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
);
|
|
82
|
+
this.md = md;
|
|
83
|
+
this.hooks.parser.call(md);
|
|
84
|
+
}
|
|
85
|
+
transform(content, opts) {
|
|
86
|
+
var _a, _b;
|
|
87
|
+
const context = {
|
|
88
|
+
content,
|
|
89
|
+
features: {},
|
|
90
|
+
contentLineOffset: 0
|
|
91
|
+
};
|
|
92
|
+
this.hooks.beforeParse.call(this.md, context);
|
|
93
|
+
const html = this.md.render(context.content, {});
|
|
94
|
+
this.hooks.afterParse.call(this.md, context);
|
|
95
|
+
const root = cleanNode(
|
|
96
|
+
markmapHtmlParser.buildTree(html, {
|
|
97
|
+
...(_b = (_a = context.frontmatter) == null ? void 0 : _a.markmap) == null ? void 0 : _b.htmlParser,
|
|
98
|
+
...opts
|
|
99
|
+
})
|
|
100
|
+
);
|
|
101
|
+
return { ...context, root };
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Get all assets from enabled plugins or filter them by plugin names as keys.
|
|
105
|
+
*/
|
|
106
|
+
getAssets(keys) {
|
|
107
|
+
const styles = [];
|
|
108
|
+
const scripts = [];
|
|
109
|
+
keys ?? (keys = this.plugins.map((plugin) => plugin.name));
|
|
110
|
+
for (const assets of keys.map((key) => this.assetsMap[key])) {
|
|
111
|
+
if (assets) {
|
|
112
|
+
if (assets.styles)
|
|
113
|
+
styles.push(...assets.styles);
|
|
114
|
+
if (assets.scripts)
|
|
115
|
+
scripts.push(...assets.scripts);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
styles: styles.map((item) => patchCSSItem(this.urlBuilder, item)),
|
|
120
|
+
scripts: scripts.map((item) => patchJSItem(this.urlBuilder, item))
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Get used assets by features object returned by `transform`.
|
|
125
|
+
*/
|
|
126
|
+
getUsedAssets(features) {
|
|
127
|
+
const keys = this.plugins.map((plugin) => plugin.name).filter((name) => features[name]);
|
|
128
|
+
return this.getAssets(keys);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const transformerVersions = {
|
|
132
|
+
"markmap-lib": "0.16.0"
|
|
133
|
+
};
|
|
134
|
+
exports.Transformer = Transformer;
|
|
135
|
+
exports.builtInPlugins = builtInPlugins;
|
|
136
|
+
exports.transformerVersions = transformerVersions;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { Hook, UrlBuilder, wrapFunction } from "markmap-common";
|
|
2
|
+
import { buildTree } from "markmap-html-parser";
|
|
3
|
+
import { Remarkable } from "remarkable";
|
|
4
|
+
function createTransformHooks(transformer) {
|
|
5
|
+
return {
|
|
6
|
+
transformer,
|
|
7
|
+
parser: new Hook(),
|
|
8
|
+
beforeParse: new Hook(),
|
|
9
|
+
afterParse: new Hook(),
|
|
10
|
+
htmltag: new Hook(),
|
|
11
|
+
retransform: new Hook()
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function patchJSItem(urlBuilder, item) {
|
|
15
|
+
if (item.type === "script" && item.data.src) {
|
|
16
|
+
return {
|
|
17
|
+
...item,
|
|
18
|
+
data: {
|
|
19
|
+
...item.data,
|
|
20
|
+
src: urlBuilder.getFullUrl(item.data.src)
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return item;
|
|
25
|
+
}
|
|
26
|
+
function patchCSSItem(urlBuilder, item) {
|
|
27
|
+
if (item.type === "stylesheet" && item.data.href) {
|
|
28
|
+
return {
|
|
29
|
+
...item,
|
|
30
|
+
data: {
|
|
31
|
+
...item.data,
|
|
32
|
+
href: urlBuilder.getFullUrl(item.data.href)
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return item;
|
|
37
|
+
}
|
|
38
|
+
const builtInPlugins = [];
|
|
39
|
+
function cleanNode(node) {
|
|
40
|
+
while (!node.content && node.children.length === 1) {
|
|
41
|
+
node = node.children[0];
|
|
42
|
+
}
|
|
43
|
+
while (node.children.length === 1 && !node.children[0].content) {
|
|
44
|
+
node = {
|
|
45
|
+
...node,
|
|
46
|
+
children: node.children[0].children
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
...node,
|
|
51
|
+
children: node.children.map(cleanNode)
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
class Transformer {
|
|
55
|
+
constructor(plugins = builtInPlugins) {
|
|
56
|
+
this.assetsMap = {};
|
|
57
|
+
this.urlBuilder = new UrlBuilder();
|
|
58
|
+
this.hooks = createTransformHooks(this);
|
|
59
|
+
this.plugins = plugins.map(
|
|
60
|
+
(plugin) => typeof plugin === "function" ? plugin() : plugin
|
|
61
|
+
);
|
|
62
|
+
const assetsMap = {};
|
|
63
|
+
for (const { name, transform } of this.plugins) {
|
|
64
|
+
assetsMap[name] = transform(this.hooks);
|
|
65
|
+
}
|
|
66
|
+
this.assetsMap = assetsMap;
|
|
67
|
+
const md = new Remarkable("full", {
|
|
68
|
+
html: true,
|
|
69
|
+
breaks: true,
|
|
70
|
+
maxNesting: Infinity
|
|
71
|
+
});
|
|
72
|
+
md.renderer.rules.htmltag = wrapFunction(
|
|
73
|
+
md.renderer.rules.htmltag,
|
|
74
|
+
(render, ...args) => {
|
|
75
|
+
const result = render(...args);
|
|
76
|
+
this.hooks.htmltag.call({ args, result });
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
);
|
|
80
|
+
this.md = md;
|
|
81
|
+
this.hooks.parser.call(md);
|
|
82
|
+
}
|
|
83
|
+
transform(content, opts) {
|
|
84
|
+
var _a, _b;
|
|
85
|
+
const context = {
|
|
86
|
+
content,
|
|
87
|
+
features: {},
|
|
88
|
+
contentLineOffset: 0
|
|
89
|
+
};
|
|
90
|
+
this.hooks.beforeParse.call(this.md, context);
|
|
91
|
+
const html = this.md.render(context.content, {});
|
|
92
|
+
this.hooks.afterParse.call(this.md, context);
|
|
93
|
+
const root = cleanNode(
|
|
94
|
+
buildTree(html, {
|
|
95
|
+
...(_b = (_a = context.frontmatter) == null ? void 0 : _a.markmap) == null ? void 0 : _b.htmlParser,
|
|
96
|
+
...opts
|
|
97
|
+
})
|
|
98
|
+
);
|
|
99
|
+
return { ...context, root };
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Get all assets from enabled plugins or filter them by plugin names as keys.
|
|
103
|
+
*/
|
|
104
|
+
getAssets(keys) {
|
|
105
|
+
const styles = [];
|
|
106
|
+
const scripts = [];
|
|
107
|
+
keys ?? (keys = this.plugins.map((plugin) => plugin.name));
|
|
108
|
+
for (const assets of keys.map((key) => this.assetsMap[key])) {
|
|
109
|
+
if (assets) {
|
|
110
|
+
if (assets.styles)
|
|
111
|
+
styles.push(...assets.styles);
|
|
112
|
+
if (assets.scripts)
|
|
113
|
+
scripts.push(...assets.scripts);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
styles: styles.map((item) => patchCSSItem(this.urlBuilder, item)),
|
|
118
|
+
scripts: scripts.map((item) => patchJSItem(this.urlBuilder, item))
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Get used assets by features object returned by `transform`.
|
|
123
|
+
*/
|
|
124
|
+
getUsedAssets(features) {
|
|
125
|
+
const keys = this.plugins.map((plugin) => plugin.name).filter((name) => features[name]);
|
|
126
|
+
return this.getAssets(keys);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const transformerVersions = {
|
|
130
|
+
"markmap-lib": "0.16.0"
|
|
131
|
+
};
|
|
132
|
+
export {
|
|
133
|
+
Transformer,
|
|
134
|
+
builtInPlugins,
|
|
135
|
+
transformerVersions
|
|
136
|
+
};
|
package/dist/plugins/index.d.ts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
|
+
import pluginCheckbox from './checkbox';
|
|
2
|
+
import pluginFrontmatter from './frontmatter';
|
|
3
|
+
import pluginHljs from './hljs';
|
|
4
|
+
import pluginKatex from './katex';
|
|
5
|
+
import pluginNpmUrl from './npm-url';
|
|
6
|
+
import pluginSourceLines from './source-lines';
|
|
1
7
|
export * from './base';
|
|
8
|
+
export { pluginCheckbox, pluginFrontmatter, pluginHljs, pluginKatex, pluginNpmUrl, pluginSourceLines, };
|
|
2
9
|
export declare const plugins: import("..").ITransformPlugin[];
|