markmap-lib 0.15.9-alpha.6 → 0.16.1
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 +135 -213
- package/dist/browser/index.mjs +37 -81
- package/dist/index.d.ts +1 -3
- package/dist/index.js +17 -352
- package/dist/index.mjs +18 -353
- 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/source-lines.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,300 +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.6"}/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
|
-
function createTransformHooks(transformer) {
|
|
15
|
-
return {
|
|
16
|
-
transformer,
|
|
17
|
-
parser: new Hook(),
|
|
18
|
-
beforeParse: new Hook(),
|
|
19
|
-
afterParse: new Hook(),
|
|
20
|
-
htmltag: new Hook(),
|
|
21
|
-
retransform: new Hook()
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
function definePlugin(plugin2) {
|
|
25
|
-
return plugin2;
|
|
26
|
-
}
|
|
27
|
-
const name$5 = "checkbox";
|
|
28
|
-
const plugin$3 = definePlugin({
|
|
29
|
-
name: name$5,
|
|
30
|
-
transform(transformHooks) {
|
|
31
|
-
transformHooks.parser.tap((md) => {
|
|
32
|
-
md.core.ruler.before(
|
|
33
|
-
"inline",
|
|
34
|
-
"checkbox",
|
|
35
|
-
(state) => {
|
|
36
|
-
for (let i = 2; i < state.tokens.length; i += 1) {
|
|
37
|
-
const token = state.tokens[i];
|
|
38
|
-
if (token.type === "inline" && token.content) {
|
|
39
|
-
const prevType = state.tokens[i - 1].type;
|
|
40
|
-
const prevPrevType = state.tokens[i - 2].type;
|
|
41
|
-
if (prevType === "heading_open" || prevType === "paragraph_open" && prevPrevType === "list_item_open") {
|
|
42
|
-
if (token.content.startsWith("[ ]")) {
|
|
43
|
-
token.content = `${svg_unmarked} ${token.content.slice(3)}`;
|
|
44
|
-
} else if (token.content.startsWith("[x]")) {
|
|
45
|
-
token.content = `${svg_marked} ${token.content.slice(3)}`;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return false;
|
|
51
|
-
},
|
|
52
|
-
{}
|
|
53
|
-
);
|
|
54
|
-
});
|
|
55
|
-
return {};
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
const name$4 = "frontmatter";
|
|
59
|
-
const frontmatter = definePlugin({
|
|
60
|
-
name: name$4,
|
|
61
|
-
transform(transformHooks) {
|
|
62
|
-
transformHooks.beforeParse.tap((md, context) => {
|
|
63
|
-
const { content } = context;
|
|
64
|
-
if (!/^---\r?\n/.test(content))
|
|
65
|
-
return;
|
|
66
|
-
const match = /\n---\r?\n/.exec(content);
|
|
67
|
-
if (!match)
|
|
68
|
-
return;
|
|
69
|
-
const raw = content.slice(4, match.index);
|
|
70
|
-
let frontmatter2;
|
|
71
|
-
try {
|
|
72
|
-
frontmatter2 = yaml.load(raw);
|
|
73
|
-
if (frontmatter2 == null ? void 0 : frontmatter2.markmap) {
|
|
74
|
-
frontmatter2.markmap = normalizeMarkmapJsonOptions(
|
|
75
|
-
frontmatter2.markmap
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
} catch {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
context.frontmatter = frontmatter2;
|
|
82
|
-
context.content = content.slice(match.index + match[0].length);
|
|
83
|
-
context.contentLineOffset = content.slice(0, match.index).split("\n").length + 1;
|
|
84
|
-
});
|
|
85
|
-
return {};
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
function normalizeMarkmapJsonOptions(options) {
|
|
89
|
-
if (!options)
|
|
90
|
-
return;
|
|
91
|
-
["color", "extraJs", "extraCss"].forEach((key) => {
|
|
92
|
-
if (options[key] != null)
|
|
93
|
-
options[key] = normalizeStringArray(options[key]);
|
|
94
|
-
});
|
|
95
|
-
["duration", "maxWidth", "initialExpandLevel"].forEach((key) => {
|
|
96
|
-
if (options[key] != null)
|
|
97
|
-
options[key] = normalizeNumber(options[key]);
|
|
98
|
-
});
|
|
99
|
-
return options;
|
|
100
|
-
}
|
|
101
|
-
function normalizeStringArray(value) {
|
|
102
|
-
let result;
|
|
103
|
-
if (typeof value === "string")
|
|
104
|
-
result = [value];
|
|
105
|
-
else if (Array.isArray(value))
|
|
106
|
-
result = value.filter((item) => item && typeof item === "string");
|
|
107
|
-
return (result == null ? void 0 : result.length) ? result : void 0;
|
|
108
|
-
}
|
|
109
|
-
function normalizeNumber(value) {
|
|
110
|
-
if (isNaN(+value))
|
|
111
|
-
return;
|
|
112
|
-
return +value;
|
|
113
|
-
}
|
|
114
|
-
const name$3 = "hljs";
|
|
115
|
-
const preloadScripts$1 = [
|
|
116
|
-
`@highlightjs/cdn-assets@${"11.8.0"}/highlight.min.js`
|
|
117
|
-
].map((path) => buildJSItem(path));
|
|
118
|
-
const styles$1 = [
|
|
119
|
-
`@highlightjs/cdn-assets@${"11.8.0"}/styles/default.min.css`
|
|
120
|
-
// `highlight.js@${process.env.HLJS_VERSION}/styles/default.css`,
|
|
121
|
-
].map((path) => buildCSSItem(path));
|
|
122
|
-
const config$1 = {
|
|
123
|
-
versions: {
|
|
124
|
-
hljs: "11.8.0"
|
|
125
|
-
},
|
|
126
|
-
preloadScripts: preloadScripts$1,
|
|
127
|
-
styles: styles$1
|
|
128
|
-
};
|
|
129
|
-
const plugin$2 = definePlugin({
|
|
130
|
-
name: name$3,
|
|
131
|
-
config: config$1,
|
|
132
|
-
transform(transformHooks) {
|
|
133
|
-
var _a;
|
|
134
|
-
let enableFeature = noop;
|
|
135
|
-
transformHooks.parser.tap((md) => {
|
|
136
|
-
md.set({
|
|
137
|
-
highlight: (str, language) => {
|
|
138
|
-
enableFeature();
|
|
139
|
-
return hljs.highlightAuto(str, language ? [language] : void 0).value;
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
transformHooks.beforeParse.tap((_, context) => {
|
|
144
|
-
enableFeature = () => {
|
|
145
|
-
context.features[name$3] = true;
|
|
146
|
-
};
|
|
147
|
-
});
|
|
148
|
-
return {
|
|
149
|
-
styles: (_a = plugin$2.config) == null ? void 0 : _a.styles
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
const name$2 = "katex";
|
|
154
|
-
const preloadScripts = [
|
|
155
|
-
`katex@${"0.16.8"}/dist/katex.min.js`
|
|
156
|
-
].map((path) => buildJSItem(path));
|
|
157
|
-
const webfontloader = buildJSItem(
|
|
158
|
-
`webfontloader@${"1.6.28"}/webfontloader.js`
|
|
159
|
-
);
|
|
160
|
-
webfontloader.data.defer = true;
|
|
161
|
-
const styles = [`katex@${"0.16.8"}/dist/katex.min.css`].map(
|
|
162
|
-
(path) => buildCSSItem(path)
|
|
163
|
-
);
|
|
164
|
-
const config = {
|
|
165
|
-
versions: {
|
|
166
|
-
katex: "0.16.8",
|
|
167
|
-
webfontloader: "1.6.28"
|
|
168
|
-
},
|
|
169
|
-
preloadScripts,
|
|
170
|
-
scripts: [
|
|
171
|
-
{
|
|
172
|
-
type: "iife",
|
|
173
|
-
data: {
|
|
174
|
-
fn: (getMarkmap) => {
|
|
175
|
-
window.WebFontConfig = {
|
|
176
|
-
custom: {
|
|
177
|
-
families: [
|
|
178
|
-
"KaTeX_AMS",
|
|
179
|
-
"KaTeX_Caligraphic:n4,n7",
|
|
180
|
-
"KaTeX_Fraktur:n4,n7",
|
|
181
|
-
"KaTeX_Main:n4,n7,i4,i7",
|
|
182
|
-
"KaTeX_Math:i4,i7",
|
|
183
|
-
"KaTeX_Script",
|
|
184
|
-
"KaTeX_SansSerif:n4,n7,i4",
|
|
185
|
-
"KaTeX_Size1",
|
|
186
|
-
"KaTeX_Size2",
|
|
187
|
-
"KaTeX_Size3",
|
|
188
|
-
"KaTeX_Size4",
|
|
189
|
-
"KaTeX_Typewriter"
|
|
190
|
-
]
|
|
191
|
-
},
|
|
192
|
-
active: () => {
|
|
193
|
-
getMarkmap().refreshHook.call();
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
},
|
|
197
|
-
getParams({ getMarkmap }) {
|
|
198
|
-
return [getMarkmap];
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
},
|
|
202
|
-
webfontloader
|
|
203
|
-
],
|
|
204
|
-
styles
|
|
205
|
-
};
|
|
206
|
-
const plugin$1 = definePlugin({
|
|
207
|
-
name: name$2,
|
|
208
|
-
config,
|
|
209
|
-
transform(transformHooks) {
|
|
210
|
-
var _a, _b;
|
|
211
|
-
let enableFeature = noop;
|
|
212
|
-
transformHooks.parser.tap((md) => {
|
|
213
|
-
md.use(remarkableKatex);
|
|
214
|
-
md.renderer.rules.katex = wrapFunction(
|
|
215
|
-
md.renderer.rules.katex,
|
|
216
|
-
(render, ...args) => {
|
|
217
|
-
enableFeature();
|
|
218
|
-
return render(...args);
|
|
219
|
-
}
|
|
220
|
-
);
|
|
221
|
-
});
|
|
222
|
-
transformHooks.beforeParse.tap((_, context) => {
|
|
223
|
-
enableFeature = () => {
|
|
224
|
-
context.features[name$2] = true;
|
|
225
|
-
};
|
|
226
|
-
});
|
|
227
|
-
return {
|
|
228
|
-
styles: (_a = plugin$1.config) == null ? void 0 : _a.styles,
|
|
229
|
-
scripts: (_b = plugin$1.config) == null ? void 0 : _b.scripts
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
});
|
|
233
|
-
const name$1 = "npmUrl";
|
|
234
|
-
const npmUrl = definePlugin({
|
|
235
|
-
name: name$1,
|
|
236
|
-
transform(transformHooks) {
|
|
237
|
-
transformHooks.afterParse.tap((_, context) => {
|
|
238
|
-
const { frontmatter: frontmatter2 } = context;
|
|
239
|
-
const markmap = frontmatter2 == null ? void 0 : frontmatter2.markmap;
|
|
240
|
-
if (markmap) {
|
|
241
|
-
["extraJs", "extraCss"].forEach((key) => {
|
|
242
|
-
const value = markmap[key];
|
|
243
|
-
if (value) {
|
|
244
|
-
markmap[key] = value.map((path) => {
|
|
245
|
-
if (path.startsWith("npm:")) {
|
|
246
|
-
return transformHooks.transformer.urlBuilder.getFullUrl(
|
|
247
|
-
path.slice(4)
|
|
248
|
-
);
|
|
249
|
-
}
|
|
250
|
-
return path;
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
});
|
|
256
|
-
return {};
|
|
257
|
-
}
|
|
258
|
-
});
|
|
259
|
-
const name = "sourceLines";
|
|
260
|
-
const plugin = definePlugin({
|
|
261
|
-
name,
|
|
262
|
-
transform(transformHooks) {
|
|
263
|
-
transformHooks.parser.tap((md) => {
|
|
264
|
-
Object.entries(md.renderer.rules).forEach(([key, value]) => {
|
|
265
|
-
if (typeof value === "function") {
|
|
266
|
-
md.renderer.rules[key] = patchRule(value);
|
|
267
|
-
} else {
|
|
268
|
-
Object.entries(value).forEach(([k, v]) => {
|
|
269
|
-
value[k] = patchRule(v);
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
});
|
|
273
|
-
});
|
|
274
|
-
return {};
|
|
275
|
-
}
|
|
276
|
-
});
|
|
277
|
-
function patchRule(rule) {
|
|
278
|
-
return wrapFunction(rule, (render, tokens, idx, ...rest) => {
|
|
279
|
-
let html = render(tokens, idx, ...rest);
|
|
280
|
-
const { lines } = tokens[idx];
|
|
281
|
-
if (lines) {
|
|
282
|
-
html = html.replace(
|
|
283
|
-
/^<[\w-]+/,
|
|
284
|
-
(m) => `${m} data-lines="${lines.join(",")}"`
|
|
285
|
-
);
|
|
286
|
-
}
|
|
287
|
-
return html;
|
|
288
|
-
});
|
|
289
|
-
}
|
|
290
|
-
const plugins = [
|
|
291
|
-
frontmatter,
|
|
292
|
-
plugin$1,
|
|
293
|
-
plugin$2,
|
|
294
|
-
npmUrl,
|
|
295
|
-
plugin$3,
|
|
296
|
-
plugin
|
|
297
|
-
];
|
|
4
|
+
import { plugins, createTransformHooks } from "./plugins.mjs";
|
|
5
|
+
import "js-yaml";
|
|
6
|
+
import "highlight.js";
|
|
7
|
+
import "remarkable-katex";
|
|
298
8
|
function patchJSItem(urlBuilder, item) {
|
|
299
9
|
if (item.type === "script" && item.data.src) {
|
|
300
10
|
return {
|
|
@@ -319,6 +29,7 @@ function patchCSSItem(urlBuilder, item) {
|
|
|
319
29
|
}
|
|
320
30
|
return item;
|
|
321
31
|
}
|
|
32
|
+
const builtInPlugins = plugins;
|
|
322
33
|
function cleanNode(node) {
|
|
323
34
|
while (!node.content && node.children.length === 1) {
|
|
324
35
|
node = node.children[0];
|
|
@@ -335,16 +46,16 @@ function cleanNode(node) {
|
|
|
335
46
|
};
|
|
336
47
|
}
|
|
337
48
|
class Transformer {
|
|
338
|
-
constructor(
|
|
49
|
+
constructor(plugins2 = builtInPlugins) {
|
|
339
50
|
this.assetsMap = {};
|
|
340
51
|
this.urlBuilder = new UrlBuilder();
|
|
341
52
|
this.hooks = createTransformHooks(this);
|
|
342
|
-
this.plugins =
|
|
343
|
-
(
|
|
53
|
+
this.plugins = plugins2.map(
|
|
54
|
+
(plugin) => typeof plugin === "function" ? plugin() : plugin
|
|
344
55
|
);
|
|
345
56
|
const assetsMap = {};
|
|
346
|
-
for (const { name
|
|
347
|
-
assetsMap[
|
|
57
|
+
for (const { name, transform } of this.plugins) {
|
|
58
|
+
assetsMap[name] = transform(this.hooks);
|
|
348
59
|
}
|
|
349
60
|
this.assetsMap = assetsMap;
|
|
350
61
|
const md = new Remarkable("full", {
|
|
@@ -385,19 +96,19 @@ class Transformer {
|
|
|
385
96
|
* Get all assets from enabled plugins or filter them by plugin names as keys.
|
|
386
97
|
*/
|
|
387
98
|
getAssets(keys) {
|
|
388
|
-
const
|
|
99
|
+
const styles = [];
|
|
389
100
|
const scripts = [];
|
|
390
|
-
keys ?? (keys = this.plugins.map((
|
|
101
|
+
keys ?? (keys = this.plugins.map((plugin) => plugin.name));
|
|
391
102
|
for (const assets of keys.map((key) => this.assetsMap[key])) {
|
|
392
103
|
if (assets) {
|
|
393
104
|
if (assets.styles)
|
|
394
|
-
|
|
105
|
+
styles.push(...assets.styles);
|
|
395
106
|
if (assets.scripts)
|
|
396
107
|
scripts.push(...assets.scripts);
|
|
397
108
|
}
|
|
398
109
|
}
|
|
399
110
|
return {
|
|
400
|
-
styles:
|
|
111
|
+
styles: styles.map((item) => patchCSSItem(this.urlBuilder, item)),
|
|
401
112
|
scripts: scripts.map((item) => patchJSItem(this.urlBuilder, item))
|
|
402
113
|
};
|
|
403
114
|
}
|
|
@@ -405,61 +116,15 @@ class Transformer {
|
|
|
405
116
|
* Get used assets by features object returned by `transform`.
|
|
406
117
|
*/
|
|
407
118
|
getUsedAssets(features) {
|
|
408
|
-
const keys = this.plugins.map((
|
|
119
|
+
const keys = this.plugins.map((plugin) => plugin.name).filter((name) => features[name]);
|
|
409
120
|
return this.getAssets(keys);
|
|
410
121
|
}
|
|
411
|
-
fillTemplate(root, assets, extra) {
|
|
412
|
-
extra = {
|
|
413
|
-
...extra
|
|
414
|
-
};
|
|
415
|
-
extra.baseJs ?? (extra.baseJs = baseJsPaths.map((path) => this.urlBuilder.getFullUrl(path)).map((path) => buildJSItem(path)));
|
|
416
|
-
const { scripts, styles: styles2 } = assets;
|
|
417
|
-
const cssList = [...styles2 ? persistCSS(styles2) : []];
|
|
418
|
-
const context = {
|
|
419
|
-
getMarkmap: () => window.markmap,
|
|
420
|
-
getOptions: extra.getOptions,
|
|
421
|
-
jsonOptions: extra.jsonOptions,
|
|
422
|
-
root
|
|
423
|
-
};
|
|
424
|
-
const jsList = [
|
|
425
|
-
...persistJS(
|
|
426
|
-
[
|
|
427
|
-
...extra.baseJs,
|
|
428
|
-
...scripts || [],
|
|
429
|
-
{
|
|
430
|
-
type: "iife",
|
|
431
|
-
data: {
|
|
432
|
-
fn: (getMarkmap, getOptions, root2, jsonOptions) => {
|
|
433
|
-
const markmap = getMarkmap();
|
|
434
|
-
window.mm = markmap.Markmap.create(
|
|
435
|
-
"svg#mindmap",
|
|
436
|
-
(getOptions || markmap.deriveOptions)(jsonOptions),
|
|
437
|
-
root2
|
|
438
|
-
);
|
|
439
|
-
},
|
|
440
|
-
getParams: ({ getMarkmap, getOptions, root: root2, jsonOptions }) => {
|
|
441
|
-
return [getMarkmap, getOptions, root2, jsonOptions];
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
],
|
|
446
|
-
context
|
|
447
|
-
)
|
|
448
|
-
];
|
|
449
|
-
const html = template.replace("<!--CSS-->", () => cssList.join("")).replace("<!--JS-->", () => jsList.join(""));
|
|
450
|
-
return html;
|
|
451
|
-
}
|
|
452
122
|
}
|
|
453
123
|
const transformerVersions = {
|
|
454
|
-
"markmap-lib": "
|
|
455
|
-
d3: "7.8.5"
|
|
124
|
+
"markmap-lib": "0.16.1"
|
|
456
125
|
};
|
|
457
126
|
export {
|
|
458
127
|
Transformer,
|
|
459
|
-
|
|
460
|
-
plugins as builtInPlugins,
|
|
461
|
-
svg_marked,
|
|
462
|
-
svg_unmarked,
|
|
463
|
-
template,
|
|
128
|
+
builtInPlugins,
|
|
464
129
|
transformerVersions
|
|
465
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.1"
|
|
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.1"
|
|
131
|
+
};
|
|
132
|
+
export {
|
|
133
|
+
Transformer,
|
|
134
|
+
builtInPlugins,
|
|
135
|
+
transformerVersions
|
|
136
|
+
};
|