@markmap/parse 1.0.0-next.0 → 1.1.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/index.js +19 -3
- package/dist/parse.d.ts +2 -0
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -348,14 +348,30 @@ function readLabel(label) {
|
|
|
348
348
|
}
|
|
349
349
|
// src/parse.ts
|
|
350
350
|
function parse(markdown, options = {}) {
|
|
351
|
-
const
|
|
352
|
-
const mdast = processor.runSync(processor.parse(markdown));
|
|
351
|
+
const { mdast, gfm } = parseMdast(markdown, options.plugins ?? []);
|
|
353
352
|
const frontmatter = readFrontmatter(mdast);
|
|
354
353
|
return {
|
|
355
354
|
root: toTree(mdast, { title: typeof frontmatter.title === "string" ? frontmatter.title : void 0 }),
|
|
356
|
-
frontmatter
|
|
355
|
+
frontmatter,
|
|
356
|
+
gfm
|
|
357
357
|
};
|
|
358
358
|
}
|
|
359
|
+
/**
|
|
360
|
+
* GFM depends on the most third-party code, and old browsers reject syntax it may use, like
|
|
361
|
+
* regex lookbehind before Safari 16.4. A map without GFM is better than no map at all.
|
|
362
|
+
*/
|
|
363
|
+
function parseMdast(markdown, plugins, gfm = true) {
|
|
364
|
+
const processor = unified().use(remarkParse).use(gfm ? [remarkGfm] : []).use(remarkFrontmatter, ["yaml"]).use(remarkHtmlImages).use(remarkMark).use(remarkResolveReferences).use(plugins);
|
|
365
|
+
try {
|
|
366
|
+
return {
|
|
367
|
+
mdast: processor.runSync(processor.parse(markdown)),
|
|
368
|
+
gfm
|
|
369
|
+
};
|
|
370
|
+
} catch (error) {
|
|
371
|
+
if (!gfm || !(error instanceof SyntaxError)) throw error;
|
|
372
|
+
return parseMdast(markdown, plugins, false);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
359
375
|
function readFrontmatter(mdast) {
|
|
360
376
|
const node = mdast.children.find((child) => child.type === "yaml");
|
|
361
377
|
if (!node) return {};
|
package/dist/parse.d.ts
CHANGED
|
@@ -8,5 +8,7 @@ export interface ParseResult {
|
|
|
8
8
|
root: MarkmapNode;
|
|
9
9
|
/** Parsed YAML frontmatter. Markmap options live under `markmap`; validate them with the theme in use. */
|
|
10
10
|
frontmatter: Record<string, unknown>;
|
|
11
|
+
/** Whether GFM was applied. It is skipped when the browser cannot run it, see `parseMdast`. */
|
|
12
|
+
gfm: boolean;
|
|
11
13
|
}
|
|
12
14
|
export declare function parse(markdown: string, options?: ParseOptions): ParseResult;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markmap/parse",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Parse Markdown into a Markmap tree",
|
|
5
5
|
"homepage": "https://github.com/markmap/markmap#readme",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"access": "public"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@markmap/core": "1.
|
|
27
|
+
"@markmap/core": "1.1.0",
|
|
28
28
|
"remark-frontmatter": "^5.0.0",
|
|
29
29
|
"remark-gfm": "^4.0.1",
|
|
30
30
|
"remark-parse": "^11.0.0",
|