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