@temelj/mdx 0.9.0 → 0.10.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/LICENSE +1 -1
- package/dist/{compiler.d.ts → compiler.d.mts} +2 -2
- package/dist/compiler.d.mts.map +1 -0
- package/dist/{compiler.js → compiler.mjs} +1 -1
- package/dist/compiler.mjs.map +1 -0
- package/dist/{mod.d.ts → mod.d.mts} +7 -7
- package/dist/{mod.js → mod.mjs} +6 -6
- package/dist/plugins/heading-id/{plugin.d.ts → plugin.d.mts} +2 -2
- package/dist/plugins/heading-id/plugin.d.mts.map +1 -0
- package/dist/plugins/heading-id/{plugin.js → plugin.mjs} +1 -1
- package/dist/plugins/heading-id/plugin.mjs.map +1 -0
- package/dist/plugins/{mod.d.ts → mod.d.mts} +4 -4
- package/dist/plugins/{mod.js → mod.mjs} +4 -4
- package/dist/plugins/{remove-imports-exports.d.ts → remove-imports-exports.d.mts} +2 -2
- package/dist/plugins/remove-imports-exports.d.mts.map +1 -0
- package/dist/plugins/{remove-imports-exports.js → remove-imports-exports.mjs} +1 -1
- package/dist/plugins/remove-imports-exports.mjs.map +1 -0
- package/dist/plugins/syntax-highlight/{code-meta.d.ts → code-meta.d.mts} +2 -2
- package/dist/plugins/syntax-highlight/code-meta.d.mts.map +1 -0
- package/dist/plugins/syntax-highlight/{code-meta.js → code-meta.mjs} +1 -1
- package/dist/plugins/syntax-highlight/code-meta.mjs.map +1 -0
- package/dist/plugins/syntax-highlight/{plugin.d.ts → plugin.d.mts} +2 -2
- package/dist/plugins/syntax-highlight/plugin.d.mts.map +1 -0
- package/dist/plugins/syntax-highlight/{plugin.js → plugin.mjs} +2 -2
- package/dist/plugins/syntax-highlight/plugin.mjs.map +1 -0
- package/dist/plugins/tree-processor/{plugin.d.ts → plugin.d.mts} +2 -2
- package/dist/plugins/tree-processor/plugin.d.mts.map +1 -0
- package/dist/plugins/tree-processor/{plugin.js → plugin.mjs} +1 -1
- package/dist/plugins/tree-processor/plugin.mjs.map +1 -0
- package/package.json +13 -13
- package/dist/compiler.d.ts.map +0 -1
- package/dist/compiler.js.map +0 -1
- package/dist/plugins/heading-id/plugin.d.ts.map +0 -1
- package/dist/plugins/heading-id/plugin.js.map +0 -1
- package/dist/plugins/remove-imports-exports.d.ts.map +0 -1
- package/dist/plugins/remove-imports-exports.js.map +0 -1
- package/dist/plugins/syntax-highlight/code-meta.d.ts.map +0 -1
- package/dist/plugins/syntax-highlight/code-meta.js.map +0 -1
- package/dist/plugins/syntax-highlight/plugin.d.ts.map +0 -1
- package/dist/plugins/syntax-highlight/plugin.js.map +0 -1
- package/dist/plugins/tree-processor/plugin.d.ts.map +0 -1
- package/dist/plugins/tree-processor/plugin.js.map +0 -1
- /package/dist/{types.d.ts → types.d.mts} +0 -0
- /package/dist/{types.js → types.mjs} +0 -0
package/LICENSE
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { HastNode as Node } from "./types.
|
|
1
|
+
import { HastNode as Node } from "./types.mjs";
|
|
2
2
|
import { CompileOptions } from "@mdx-js/mdx";
|
|
3
3
|
import remarkFrontmatterPlugin from "remark-frontmatter";
|
|
4
4
|
import remarkGfmPlugin from "remark-gfm";
|
|
@@ -42,4 +42,4 @@ declare class MdxCompiler {
|
|
|
42
42
|
}
|
|
43
43
|
//#endregion
|
|
44
44
|
export { MdxArtifact, MdxCompileOptions, MdxCompiler, MdxSource, remarkFrontmatterPlugin, remarkGfmPlugin };
|
|
45
|
-
//# sourceMappingURL=compiler.d.
|
|
45
|
+
//# sourceMappingURL=compiler.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compiler.d.mts","names":[],"sources":["../src/compiler.ts"],"mappings":";;;;;;;;;AAkBA;;KAAY,SAAA,YAAqB,UAAA;;;AAKjC;UAAiB,iBAAA;EACf,eAAA;EACA,UAAA,GAAa,cAAA;AAAA;;;;;UAOE,WAAA,gBAA2B,MAAA;EAC1C,QAAA;EACA,WAAA,EAAa,YAAA;AAAA;;;;cAMF,WAAA;EAAA,iBACM,UAAA;EAAA,iBACA,aAAA;EAAA,iBACA,aAAA;cAEE,UAAA,GAAY,cAAA;EAMxB,gBAAA,UAAA,CACL,MAAA,EAAQ,MAAA,EAAQ,QAAA,IAAY,IAAA,EAAU,IAAA,GACtC,OAAA,GAAU,QAAA,GACT,WAAA;EASI,gBAAA,UAAA,CACL,MAAA,EAAQ,MAAA,EAAQ,QAAA,IAAY,IAAA,EAAU,IAAA,GACtC,OAAA,GAAU,QAAA,GACT,WAAA;EASU,OAAA,4BAAmC,CAAA,CAAE,SAAA,CAAA,CAChD,MAAA,EAAQ,SAAA;IACN,eAAA;IAAyB;EAAA,IAAc,iBAAA,EACzC,iBAAA,GAAoB,kBAAA,GACnB,OAAA,CAAQ,WAAA,CAAY,CAAA,CAAE,MAAA,CAAO,kBAAA;AAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compiler.mjs","names":["compileMdxJs"],"sources":["../src/compiler.ts"],"sourcesContent":["import {\n compile as compileMdxJs,\n type CompileOptions as MdxJsCompileOptions,\n} from \"@mdx-js/mdx\";\nimport remarkFrontmatterPlugin from \"remark-frontmatter\";\nimport remarkGfmPlugin from \"remark-gfm\";\nimport type { Pluggable, PluggableList, Plugin } from \"unified\";\nimport { VFile } from \"vfile\";\nimport { matter } from \"vfile-matter\";\nimport type { z } from \"zod\";\n\nimport type { HastNode } from \"./types\";\n\nexport { remarkFrontmatterPlugin, remarkGfmPlugin };\n\n/**\n * Represents the source content for an MDX document.\n */\nexport type MdxSource = string | Uint8Array;\n\n/**\n * Options for compiling an MDX document.\n */\nexport interface MdxCompileOptions {\n frontmatterOnly?: boolean | undefined;\n mdxOptions?: MdxJsCompileOptions;\n}\n\n/**\n * The result of compiling an MDX document.\n * Compiled content is optional, because sometimes we only need the frontmatter.\n */\nexport interface MdxArtifact<TFrontmatter = Record<string, unknown>> {\n compiled?: string | undefined;\n frontmatter: TFrontmatter;\n}\n\n/**\n * A reusable compiler for compiling MDX.\n */\nexport class MdxCompiler {\n private readonly mdxOptions: MdxJsCompileOptions;\n private readonly remarkPlugins: PluggableList;\n private readonly rehypePlugins: PluggableList;\n\n public constructor(mdxOptions: MdxJsCompileOptions = {}) {\n this.mdxOptions = mdxOptions;\n this.remarkPlugins = [remarkFrontmatterPlugin, remarkGfmPlugin];\n this.rehypePlugins = [];\n }\n\n public withRemarkPlugin<TOptions>(\n plugin: Plugin<[TOptions?], HastNode, HastNode>,\n options?: TOptions,\n ): MdxCompiler {\n if (options === undefined) {\n this.remarkPlugins.push(plugin as Pluggable);\n } else {\n this.remarkPlugins.push([plugin, options] as Pluggable);\n }\n return this;\n }\n\n public withRehypePlugin<TOptions>(\n plugin: Plugin<[TOptions?], HastNode, HastNode>,\n options?: TOptions,\n ): MdxCompiler {\n if (options === undefined) {\n this.rehypePlugins.push(plugin as Pluggable);\n } else {\n this.rehypePlugins.push([plugin, options] as Pluggable);\n }\n return this;\n }\n\n public async compile<TFrontmatterSchema extends z.ZodSchema>(\n source: MdxSource,\n { frontmatterOnly = false, mdxOptions }: MdxCompileOptions = {},\n frontmatterSchema?: TFrontmatterSchema,\n ): Promise<MdxArtifact<z.output<TFrontmatterSchema>>> {\n const vfile = new VFile({\n value: source,\n // Needed so vfile doesn't need to access fs.\n cwd: \"/\",\n path: \"/\",\n message: {},\n messages: {},\n });\n matter(vfile, { strip: true });\n\n const remarkPlugins = [\n ...this.remarkPlugins,\n ...(this.mdxOptions?.remarkPlugins ?? []),\n ...(mdxOptions?.remarkPlugins ?? []),\n ];\n const rehypePlugins = [\n ...this.rehypePlugins,\n ...(this.mdxOptions?.rehypePlugins ?? []),\n ...(mdxOptions?.rehypePlugins ?? []),\n ];\n\n let compiled: string | undefined;\n if (!frontmatterOnly) {\n const compiledFile = await compileMdxJs(vfile, {\n outputFormat: \"function-body\",\n ...(this.mdxOptions ?? {}),\n ...(mdxOptions ?? {}),\n remarkPlugins,\n rehypePlugins,\n });\n compiled = compiledFile.value.toString();\n }\n\n const frontmatter =\n frontmatterSchema === undefined\n ? (vfile.data.matter ?? {})\n : frontmatterSchema.parse(vfile.data.matter ?? {});\n\n return {\n compiled,\n frontmatter: frontmatter as z.output<TFrontmatterSchema>,\n };\n }\n}\n"],"mappings":";;;;;;;;;;AAwCA,IAAa,cAAb,MAAyB;CACvB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,AAAO,YAAY,aAAkC,EAAE,EAAE;AACvD,OAAK,aAAa;AAClB,OAAK,gBAAgB,CAAC,yBAAyB,gBAAgB;AAC/D,OAAK,gBAAgB,EAAE;;CAGzB,AAAO,iBACL,QACA,SACa;AACb,MAAI,YAAY,OACd,MAAK,cAAc,KAAK,OAAoB;MAE5C,MAAK,cAAc,KAAK,CAAC,QAAQ,QAAQ,CAAc;AAEzD,SAAO;;CAGT,AAAO,iBACL,QACA,SACa;AACb,MAAI,YAAY,OACd,MAAK,cAAc,KAAK,OAAoB;MAE5C,MAAK,cAAc,KAAK,CAAC,QAAQ,QAAQ,CAAc;AAEzD,SAAO;;CAGT,MAAa,QACX,QACA,EAAE,kBAAkB,OAAO,eAAkC,EAAE,EAC/D,mBACoD;EACpD,MAAM,QAAQ,IAAI,MAAM;GACtB,OAAO;GAEP,KAAK;GACL,MAAM;GACN,SAAS,EAAE;GACX,UAAU,EAAE;GACb,CAAC;AACF,SAAO,OAAO,EAAE,OAAO,MAAM,CAAC;EAE9B,MAAM,gBAAgB;GACpB,GAAG,KAAK;GACR,GAAI,KAAK,YAAY,iBAAiB,EAAE;GACxC,GAAI,YAAY,iBAAiB,EAAE;GACpC;EACD,MAAM,gBAAgB;GACpB,GAAG,KAAK;GACR,GAAI,KAAK,YAAY,iBAAiB,EAAE;GACxC,GAAI,YAAY,iBAAiB,EAAE;GACpC;EAED,IAAI;AACJ,MAAI,CAAC,gBAQH,aAPqB,MAAMA,QAAa,OAAO;GAC7C,cAAc;GACd,GAAI,KAAK,cAAc,EAAE;GACzB,GAAI,cAAc,EAAE;GACpB;GACA;GACD,CAAC,EACsB,MAAM,UAAU;EAG1C,MAAM,cACJ,sBAAsB,SACjB,MAAM,KAAK,UAAU,EAAE,GACxB,kBAAkB,MAAM,MAAM,KAAK,UAAU,EAAE,CAAC;AAEtD,SAAO;GACL;GACa;GACd"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { HastElement as Element, HastNode as Node } from "./types.
|
|
2
|
-
import { MdxArtifact, MdxCompileOptions, MdxCompiler, MdxSource, remarkFrontmatterPlugin, remarkGfmPlugin } from "./compiler.
|
|
3
|
-
import { HeadingIdPluginOptions, headingIdPlugin } from "./plugins/heading-id/plugin.
|
|
4
|
-
import { removeImportsExportsPlugin } from "./plugins/remove-imports-exports.
|
|
5
|
-
import { SyntaxHighlightPluginOptions, syntaxHighlightPlugin } from "./plugins/syntax-highlight/plugin.
|
|
6
|
-
import { MdxTreeProcessor, treeProcessorPlugin } from "./plugins/tree-processor/plugin.
|
|
7
|
-
import "./plugins/mod.
|
|
1
|
+
import { HastElement as Element, HastNode as Node } from "./types.mjs";
|
|
2
|
+
import { MdxArtifact, MdxCompileOptions, MdxCompiler, MdxSource, remarkFrontmatterPlugin, remarkGfmPlugin } from "./compiler.mjs";
|
|
3
|
+
import { HeadingIdPluginOptions, headingIdPlugin } from "./plugins/heading-id/plugin.mjs";
|
|
4
|
+
import { removeImportsExportsPlugin } from "./plugins/remove-imports-exports.mjs";
|
|
5
|
+
import { SyntaxHighlightPluginOptions, syntaxHighlightPlugin } from "./plugins/syntax-highlight/plugin.mjs";
|
|
6
|
+
import { MdxTreeProcessor, treeProcessorPlugin } from "./plugins/tree-processor/plugin.mjs";
|
|
7
|
+
import "./plugins/mod.mjs";
|
|
8
8
|
export { Element as HastElement, Node as HastNode, HeadingIdPluginOptions, MdxArtifact, MdxCompileOptions, MdxCompiler, MdxSource, MdxTreeProcessor, SyntaxHighlightPluginOptions, headingIdPlugin, remarkFrontmatterPlugin, remarkGfmPlugin, removeImportsExportsPlugin, syntaxHighlightPlugin, treeProcessorPlugin };
|
package/dist/{mod.js → mod.mjs}
RENAMED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { MdxCompiler, remarkFrontmatterPlugin, remarkGfmPlugin } from "./compiler.
|
|
2
|
-
import { headingIdPlugin } from "./plugins/heading-id/plugin.
|
|
3
|
-
import { removeImportsExportsPlugin } from "./plugins/remove-imports-exports.
|
|
4
|
-
import { syntaxHighlightPlugin } from "./plugins/syntax-highlight/plugin.
|
|
5
|
-
import { treeProcessorPlugin } from "./plugins/tree-processor/plugin.
|
|
6
|
-
import "./plugins/mod.
|
|
1
|
+
import { MdxCompiler, remarkFrontmatterPlugin, remarkGfmPlugin } from "./compiler.mjs";
|
|
2
|
+
import { headingIdPlugin } from "./plugins/heading-id/plugin.mjs";
|
|
3
|
+
import { removeImportsExportsPlugin } from "./plugins/remove-imports-exports.mjs";
|
|
4
|
+
import { syntaxHighlightPlugin } from "./plugins/syntax-highlight/plugin.mjs";
|
|
5
|
+
import { treeProcessorPlugin } from "./plugins/tree-processor/plugin.mjs";
|
|
6
|
+
import "./plugins/mod.mjs";
|
|
7
7
|
|
|
8
8
|
export { MdxCompiler, headingIdPlugin, remarkFrontmatterPlugin, remarkGfmPlugin, removeImportsExportsPlugin, syntaxHighlightPlugin, treeProcessorPlugin };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { HastNode as Node } from "../../types.
|
|
1
|
+
import { HastNode as Node } from "../../types.mjs";
|
|
2
2
|
import { Plugin } from "unified";
|
|
3
3
|
|
|
4
4
|
//#region src/plugins/heading-id/plugin.d.ts
|
|
@@ -15,4 +15,4 @@ interface HeadingIdPluginOptions {
|
|
|
15
15
|
declare const headingIdPlugin: Plugin<[HeadingIdPluginOptions?], Node, Node>;
|
|
16
16
|
//#endregion
|
|
17
17
|
export { HeadingIdPluginOptions, headingIdPlugin };
|
|
18
|
-
//# sourceMappingURL=plugin.d.
|
|
18
|
+
//# sourceMappingURL=plugin.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.mts","names":[],"sources":["../../../src/plugins/heading-id/plugin.ts"],"mappings":";;;;;;AAUA;UAAiB,sBAAA;;EAEf,MAAA;AAAA;AAMF;;;AAAA,cAAa,eAAA,EAAiB,MAAA,EAC3B,sBAAA,IACD,IAAA,EACA,IAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.mjs","names":["hastToString"],"sources":["../../../src/plugins/heading-id/plugin.ts"],"sourcesContent":["import GithubSlugger from \"github-slugger\";\nimport { toString as hastToString } from \"hast-util-to-string\";\nimport type { Plugin } from \"unified\";\nimport { visit } from \"unist-util-visit\";\n\nimport type { HastElement, HastNode } from \"../../types\";\n\n/**\n * Options for {@linkcode headingIdPlugin}.\n */\nexport interface HeadingIdPluginOptions {\n /** Prefix for title slugs. */\n prefix?: string;\n}\n\n/**\n * A rehype plugin that adds IDs to heading tags.\n */\nexport const headingIdPlugin: Plugin<\n [HeadingIdPluginOptions?],\n HastNode,\n HastNode\n> = ({ prefix } = {}) => {\n const slugger = new GithubSlugger();\n return (tree) => {\n slugger.reset();\n visit(\n tree,\n \"element\",\n function visitor(\n node: HastElement,\n index: number,\n parent: HastElement,\n ): void {\n if (!parent || index === null || !node.tagName.startsWith(\"h\")) return;\n const id = (prefix ?? \"\") + slugger.slug(hastToString(node));\n node.properties.id = id;\n },\n );\n return tree;\n };\n};\n"],"mappings":";;;;;;;;AAkBA,MAAa,mBAIR,EAAE,WAAW,EAAE,KAAK;CACvB,MAAM,UAAU,IAAI,eAAe;AACnC,SAAQ,SAAS;AACf,UAAQ,OAAO;AACf,QACE,MACA,WACA,SAAS,QACP,MACA,OACA,QACM;AACN,OAAI,CAAC,UAAU,UAAU,QAAQ,CAAC,KAAK,QAAQ,WAAW,IAAI,CAAE;GAChE,MAAM,MAAM,UAAU,MAAM,QAAQ,KAAKA,SAAa,KAAK,CAAC;AAC5D,QAAK,WAAW,KAAK;IAExB;AACD,SAAO"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { HeadingIdPluginOptions, headingIdPlugin } from "./heading-id/plugin.
|
|
2
|
-
import { removeImportsExportsPlugin } from "./remove-imports-exports.
|
|
3
|
-
import { SyntaxHighlightPluginOptions, syntaxHighlightPlugin } from "./syntax-highlight/plugin.
|
|
4
|
-
import { MdxTreeProcessor, treeProcessorPlugin } from "./tree-processor/plugin.
|
|
1
|
+
import { HeadingIdPluginOptions, headingIdPlugin } from "./heading-id/plugin.mjs";
|
|
2
|
+
import { removeImportsExportsPlugin } from "./remove-imports-exports.mjs";
|
|
3
|
+
import { SyntaxHighlightPluginOptions, syntaxHighlightPlugin } from "./syntax-highlight/plugin.mjs";
|
|
4
|
+
import { MdxTreeProcessor, treeProcessorPlugin } from "./tree-processor/plugin.mjs";
|
|
5
5
|
export { HeadingIdPluginOptions, MdxTreeProcessor, SyntaxHighlightPluginOptions, headingIdPlugin, removeImportsExportsPlugin, syntaxHighlightPlugin, treeProcessorPlugin };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { headingIdPlugin } from "./heading-id/plugin.
|
|
2
|
-
import { removeImportsExportsPlugin } from "./remove-imports-exports.
|
|
3
|
-
import { syntaxHighlightPlugin } from "./syntax-highlight/plugin.
|
|
4
|
-
import { treeProcessorPlugin } from "./tree-processor/plugin.
|
|
1
|
+
import { headingIdPlugin } from "./heading-id/plugin.mjs";
|
|
2
|
+
import { removeImportsExportsPlugin } from "./remove-imports-exports.mjs";
|
|
3
|
+
import { syntaxHighlightPlugin } from "./syntax-highlight/plugin.mjs";
|
|
4
|
+
import { treeProcessorPlugin } from "./tree-processor/plugin.mjs";
|
|
5
5
|
|
|
6
6
|
export { headingIdPlugin, removeImportsExportsPlugin, syntaxHighlightPlugin, treeProcessorPlugin };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { HastNode as Node } from "../types.
|
|
1
|
+
import { HastNode as Node } from "../types.mjs";
|
|
2
2
|
import { Plugin } from "unified";
|
|
3
3
|
|
|
4
4
|
//#region src/plugins/remove-imports-exports.d.ts
|
|
@@ -8,4 +8,4 @@ import { Plugin } from "unified";
|
|
|
8
8
|
declare const removeImportsExportsPlugin: Plugin<[unknown], Node, Node>;
|
|
9
9
|
//#endregion
|
|
10
10
|
export { removeImportsExportsPlugin };
|
|
11
|
-
//# sourceMappingURL=remove-imports-exports.d.
|
|
11
|
+
//# sourceMappingURL=remove-imports-exports.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remove-imports-exports.d.mts","names":[],"sources":["../../src/plugins/remove-imports-exports.ts"],"mappings":";;;;;;AAQA;cAAa,0BAAA,EAA4B,MAAA,YAEvC,IAAA,EACA,IAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remove-imports-exports.mjs","names":[],"sources":["../../src/plugins/remove-imports-exports.ts"],"sourcesContent":["import type { Plugin } from \"unified\";\nimport { remove } from \"unist-util-remove\";\n\nimport type { HastNode } from \"../types\";\n\n/**\n * A remark plugin which removes all import and export statements.\n */\nexport const removeImportsExportsPlugin: Plugin<\n [unknown],\n HastNode,\n HastNode\n> = () => {\n return (tree: HastNode) => {\n remove(tree, \"mdxjsEsm\");\n return tree;\n };\n};\n"],"mappings":";;;;;;AAQA,MAAa,mCAIH;AACR,SAAQ,SAAmB;AACzB,SAAO,MAAM,WAAW;AACxB,SAAO"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { HastElement as Element } from "../../types.
|
|
1
|
+
import { HastElement as Element } from "../../types.mjs";
|
|
2
2
|
import { NumericRange } from "@temelj/iterator";
|
|
3
3
|
|
|
4
4
|
//#region src/plugins/syntax-highlight/code-meta.d.ts
|
|
@@ -20,4 +20,4 @@ type MdxCodeMetaCommandLine = {
|
|
|
20
20
|
declare function extractCodeMeta(node: Element): MdxCodeMeta;
|
|
21
21
|
//#endregion
|
|
22
22
|
export { MdxCodeMeta, MdxCodeMetaCommandLine, MdxCodeMetaHighlightLine, MdxCodeMetaLineNumbers, extractCodeMeta };
|
|
23
|
-
//# sourceMappingURL=code-meta.d.
|
|
23
|
+
//# sourceMappingURL=code-meta.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code-meta.d.mts","names":[],"sources":["../../../src/plugins/syntax-highlight/code-meta.ts"],"mappings":";;;;UAKiB,WAAA;EACf,SAAA,GAAY,wBAAA;EACZ,eAAA,GAAkB,sBAAA;EAClB,WAAA,GAAc,sBAAA;EACd,QAAA;AAAA;AAAA,KAGU,wBAAA;EACV,SAAA,EAAW,YAAA;AAAA;AAAA,KAGD,sBAAA;EACV,SAAA,EAAW,YAAA;AAAA;AAAA,KAGD,sBAAA;EACV,YAAA,EAAc,YAAA;AAAA;AAAA,iBAGA,eAAA,CAAgB,IAAA,EAAM,OAAA,GAAc,WAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code-meta.mjs","names":["z"],"sources":["../../../src/plugins/syntax-highlight/code-meta.ts"],"sourcesContent":["import { type NumericRange, parseNumericRange } from \"@temelj/iterator\";\nimport * as z from \"zod\";\n\nimport type { HastElement } from \"../../types\";\n\nexport interface MdxCodeMeta {\n highlight?: MdxCodeMetaHighlightLine;\n showLineNumbers?: MdxCodeMetaLineNumbers;\n commandLine?: MdxCodeMetaCommandLine;\n fileName?: string;\n}\n\nexport type MdxCodeMetaHighlightLine = {\n lineRange: NumericRange;\n};\n\nexport type MdxCodeMetaLineNumbers = {\n lineRange: NumericRange;\n};\n\nexport type MdxCodeMetaCommandLine = {\n commandRange: NumericRange;\n};\n\nexport function extractCodeMeta(node: HastElement): MdxCodeMeta {\n const meta = node?.data?.meta ?? node?.properties?.metastring ?? \"\";\n return parseMetaString(meta as string);\n}\n\nconst metaSchema = z.object({\n highlight: z.optional(z.string()),\n showLineNumbers: z.optional(z.union([z.boolean(), z.string()])),\n commandLine: z.optional(z.union([z.boolean(), z.string()])),\n fileName: z.optional(z.string()),\n});\n\nfunction parseMetaString(meta: string): MdxCodeMeta {\n let raw: unknown;\n try {\n raw = JSON.parse(meta.trim());\n } catch {\n return {};\n }\n const parsed = metaSchema.parse(raw);\n const options: MdxCodeMeta = {};\n\n if (parsed.highlight !== undefined) {\n options.highlight = { lineRange: parseNumericRange(parsed.highlight) };\n }\n\n if (parsed.showLineNumbers !== undefined) {\n if (typeof parsed.showLineNumbers === \"string\") {\n options.showLineNumbers = {\n lineRange: parseNumericRange(parsed.showLineNumbers),\n };\n } else if (parsed.showLineNumbers) {\n options.showLineNumbers = { lineRange: [] };\n }\n }\n\n if (parsed.commandLine !== undefined) {\n if (typeof parsed.commandLine === \"string\") {\n options.commandLine = {\n commandRange: parseNumericRange(parsed.commandLine),\n };\n } else if (parsed.commandLine) {\n options.commandLine = { commandRange: [] };\n }\n }\n\n if (parsed.fileName !== undefined) {\n options.fileName = parsed.fileName.trim();\n }\n\n return options;\n}\n"],"mappings":";;;;AAwBA,SAAgB,gBAAgB,MAAgC;AAE9D,QAAO,gBADM,MAAM,MAAM,QAAQ,MAAM,YAAY,cAAc,GAC3B;;AAGxC,MAAM,aAAaA,IAAE,OAAO;CAC1B,WAAWA,IAAE,SAASA,IAAE,QAAQ,CAAC;CACjC,iBAAiBA,IAAE,SAASA,IAAE,MAAM,CAACA,IAAE,SAAS,EAAEA,IAAE,QAAQ,CAAC,CAAC,CAAC;CAC/D,aAAaA,IAAE,SAASA,IAAE,MAAM,CAACA,IAAE,SAAS,EAAEA,IAAE,QAAQ,CAAC,CAAC,CAAC;CAC3D,UAAUA,IAAE,SAASA,IAAE,QAAQ,CAAC;CACjC,CAAC;AAEF,SAAS,gBAAgB,MAA2B;CAClD,IAAI;AACJ,KAAI;AACF,QAAM,KAAK,MAAM,KAAK,MAAM,CAAC;SACvB;AACN,SAAO,EAAE;;CAEX,MAAM,SAAS,WAAW,MAAM,IAAI;CACpC,MAAM,UAAuB,EAAE;AAE/B,KAAI,OAAO,cAAc,OACvB,SAAQ,YAAY,EAAE,WAAW,kBAAkB,OAAO,UAAU,EAAE;AAGxE,KAAI,OAAO,oBAAoB,QAC7B;MAAI,OAAO,OAAO,oBAAoB,SACpC,SAAQ,kBAAkB,EACxB,WAAW,kBAAkB,OAAO,gBAAgB,EACrD;WACQ,OAAO,gBAChB,SAAQ,kBAAkB,EAAE,WAAW,EAAE,EAAE;;AAI/C,KAAI,OAAO,gBAAgB,QACzB;MAAI,OAAO,OAAO,gBAAgB,SAChC,SAAQ,cAAc,EACpB,cAAc,kBAAkB,OAAO,YAAY,EACpD;WACQ,OAAO,YAChB,SAAQ,cAAc,EAAE,cAAc,EAAE,EAAE;;AAI9C,KAAI,OAAO,aAAa,OACtB,SAAQ,WAAW,OAAO,SAAS,MAAM;AAG3C,QAAO"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { HastNode as Node } from "../../types.
|
|
1
|
+
import { HastNode as Node } from "../../types.mjs";
|
|
2
2
|
import { TransformerNotationHighlightOptions } from "@shikijs/transformers";
|
|
3
3
|
import { BundledLanguage, BundledTheme, CodeToHastOptions } from "shiki";
|
|
4
4
|
import { Plugin } from "unified";
|
|
@@ -55,4 +55,4 @@ interface SyntaxHighlightPluginOptions {
|
|
|
55
55
|
declare const syntaxHighlightPlugin: Plugin<[SyntaxHighlightPluginOptions?], Node, Node>;
|
|
56
56
|
//#endregion
|
|
57
57
|
export { SyntaxHighlightPluginOptions, syntaxHighlightPlugin };
|
|
58
|
-
//# sourceMappingURL=plugin.d.
|
|
58
|
+
//# sourceMappingURL=plugin.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.mts","names":[],"sources":["../../../src/plugins/syntax-highlight/plugin.ts"],"mappings":";;;;;;KAoBK,gBAAA,GAAmB,OAAA,CACtB,iBAAA,CAAkB,eAAA,EAAiB,YAAA;AAAA,KAGhC,mBAAA;AAPoD;;;AAAA,UAYxC,4BAAA;EARoB;;;;EAanC,qBAAA,GAAwB,mBAAA;EAdF;;;EAmBtB,SAAA;IACE,WAAA,GAAc,mCAAA;EAAA;EAhBb;;;EAsBH,WAAA;IAtBsB;AAKxB;;;IAsBI,iBAAA;IAXc;;;IAed,SAAA;EAAA;EArBsB;;;EA2BxB,WAAA;IAfA;;;IAmBE,SAAA;EAAA;EAMF;;;EAAA,gBAAA,GAAmB,gBAAA;AAAA;;;;cAMR,qBAAA,EAAuB,MAAA,EACjC,4BAAA,IACD,IAAA,EACA,IAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { extractCodeMeta } from "./code-meta.
|
|
1
|
+
import { extractCodeMeta } from "./code-meta.mjs";
|
|
2
2
|
import { toString } from "hast-util-to-string";
|
|
3
3
|
import { visit } from "unist-util-visit";
|
|
4
4
|
import { transformerNotationHighlight } from "@shikijs/transformers";
|
|
@@ -91,4 +91,4 @@ function getNodeLanguage(node) {
|
|
|
91
91
|
|
|
92
92
|
//#endregion
|
|
93
93
|
export { syntaxHighlightPlugin };
|
|
94
|
-
//# sourceMappingURL=plugin.
|
|
94
|
+
//# sourceMappingURL=plugin.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.mjs","names":["hastToString"],"sources":["../../../src/plugins/syntax-highlight/plugin.ts"],"sourcesContent":["import {\n type TransformerNotationHighlightOptions,\n transformerNotationHighlight,\n} from \"@shikijs/transformers\";\nimport { numericRangeContains } from \"@temelj/iterator\";\nimport type { Properties } from \"hast\";\nimport { toString as hastToString } from \"hast-util-to-string\";\nimport {\n type BundledLanguage,\n type BundledTheme,\n type CodeToHastOptions,\n codeToHast,\n ShikiError,\n} from \"shiki\";\nimport type { Plugin } from \"unified\";\nimport { visit } from \"unist-util-visit\";\n\nimport type { HastElement, HastNode } from \"../../types\";\nimport { extractCodeMeta } from \"./code-meta\";\n\ntype ShikiHastOptions = Partial<\n CodeToHastOptions<BundledLanguage, BundledTheme>\n>;\n\ntype SyntaxDataAttribute = \"source-code\" | \"language\" | \"line-count\";\n\n/**\n * Options for {@linkcode syntaxHighlightPlugin}.\n */\nexport interface SyntaxHighlightPluginOptions {\n /**\n * Include these data attributes on the `<pre>` element.\n * These might be useful to have when building custom code components.\n */\n includeDataAttributes?: SyntaxDataAttribute[];\n\n /**\n * Options for the highlight transformer.\n */\n highlight?: {\n transformer?: TransformerNotationHighlightOptions;\n };\n\n /**\n * Options for line numbers.\n */\n lineNumbers?: {\n /**\n * The name of the variable that contains the line number width.\n * This is used for padding the line numbers so that they align.\n */\n widthVariableName?: string;\n /**\n * A class name to be added to the line numbers.\n */\n className?: string;\n };\n\n /**\n * Options for command line.\n */\n commandLine?: {\n /**\n * A class name to be added to the prompt line.\n */\n className?: string;\n };\n\n /**\n * Extra options for the Shiki hast transformer.\n */\n shikiHastOptions?: ShikiHastOptions;\n}\n\n/**\n * A plugin that highlights code blocks using Shiki.\n */\nexport const syntaxHighlightPlugin: Plugin<\n [SyntaxHighlightPluginOptions?],\n HastNode,\n HastNode\n> = (options: SyntaxHighlightPluginOptions = {}) => {\n return async (tree) => {\n const promises: Promise<void>[] = [];\n\n async function visitor(\n node: HastElement,\n index: number,\n parent: HastElement,\n ): Promise<void> {\n if (!parent || index === null || node.tagName !== \"pre\") {\n return;\n }\n\n const codeNode = node.children[0];\n if (\n !codeNode ||\n codeNode.type !== \"element\" ||\n codeNode.tagName !== \"code\" ||\n !codeNode.properties\n ) {\n return;\n }\n\n const lang = getNodeLanguage(codeNode);\n if (typeof lang !== \"string\") {\n return;\n }\n\n const meta = extractCodeMeta(codeNode);\n\n const sourceCode = hastToString(codeNode).trim();\n\n const hastOptions: CodeToHastOptions<BundledLanguage, BundledTheme> = {\n tokenizeTimeLimit: 10_000,\n lang,\n themes: {\n light: \"github-light-default\",\n dark: \"github-dark-default\",\n },\n ...(options.shikiHastOptions ?? {}),\n };\n if (hastOptions.transformers === undefined) {\n hastOptions.transformers = [];\n }\n\n if (options.highlight) {\n hastOptions.transformers.push(\n transformerNotationHighlight({\n ...(options.highlight.transformer ?? {}),\n matchAlgorithm: \"v3\",\n }),\n );\n }\n\n let lineCount = 0;\n hastOptions.transformers.push({\n code(node): void {\n let lineIndex = 0;\n for (const line of node.children.filter(\n (node: HastNode) => node.type === \"element\",\n ) as HastElement[]) {\n line.properties[\"data-line\"] = lineIndex + 1;\n\n if (options.highlight) {\n if (\n meta.highlight &&\n numericRangeContains(meta.highlight.lineRange, lineIndex)\n ) {\n this.addClassToHast(\n line,\n options.highlight.transformer?.classActiveLine ??\n \"highlighted\",\n );\n }\n }\n\n if (\n options.lineNumbers &&\n meta.showLineNumbers &&\n (meta.showLineNumbers.lineRange.length === 0 ||\n numericRangeContains(meta.showLineNumbers.lineRange, lineIndex))\n ) {\n this.addClassToHast(\n line,\n options.lineNumbers.className ?? \"line-number\",\n );\n }\n\n if (\n options.commandLine &&\n meta.commandLine &&\n (meta.commandLine.commandRange.length === 0 ||\n numericRangeContains(meta.commandLine.commandRange, lineIndex))\n ) {\n this.addClassToHast(\n line,\n options.commandLine.className ?? \"line-command\",\n );\n }\n\n lineIndex++;\n }\n\n lineCount = lineIndex;\n\n if (meta.showLineNumbers) {\n if (typeof node.properties.style !== \"string\") {\n node.properties.style = \"\";\n }\n node.properties.style += `--${\n options.lineNumbers?.widthVariableName ?? \"line-number-width\"\n }:${1 + Math.floor(Math.log10(this.lines.length))}ch;`;\n }\n },\n });\n\n try {\n const hast = await codeToHast(sourceCode, hastOptions);\n\n // Insert highlighted code into parent\n const hastPre = hast.children[0] as HastElement;\n\n const properties: Properties = {\n ...hastPre.properties,\n dataFileName: meta.fileName,\n };\n if (options.includeDataAttributes) {\n if (options.includeDataAttributes.includes(\"language\")) {\n properties.dataLanguage = lang;\n }\n if (options.includeDataAttributes.includes(\"source-code\")) {\n properties.dataSourceCode = sourceCode;\n }\n if (options.includeDataAttributes.includes(\"line-count\")) {\n properties.dataLineCount = lineCount;\n }\n }\n\n parent.children.splice(index, 1, {\n ...hastPre,\n properties,\n });\n } catch (error) {\n if (error instanceof ShikiError) {\n if (\n error.message.includes(\n \"Language `math` is not included in this bundle\",\n )\n ) {\n // Skip if the block is math block.\n return;\n }\n }\n throw error;\n }\n }\n\n visit(\n tree,\n \"element\",\n (node: HastElement, index: number, parent: HastElement) => {\n promises.push(visitor(node, index, parent));\n },\n );\n\n await Promise.all(promises);\n\n return tree;\n };\n};\n\nfunction getNodeLanguage(node: HastElement): string | undefined {\n if (!Array.isArray(node.properties.className)) {\n return undefined;\n }\n const prefix = \"language-\";\n const language = node.properties.className.find(\n (name) => typeof name === \"string\" && name.startsWith(prefix),\n ) as string | undefined;\n if (typeof language === \"string\") {\n return language.slice(prefix.length);\n }\n return language;\n}\n"],"mappings":"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"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { HastElement as Element, HastNode as Node } from "../../types.
|
|
1
|
+
import { HastElement as Element, HastNode as Node } from "../../types.mjs";
|
|
2
2
|
import { Plugin } from "unified";
|
|
3
3
|
|
|
4
4
|
//#region src/plugins/tree-processor/plugin.d.ts
|
|
@@ -16,4 +16,4 @@ type TreeProcessorPluginOptions = {
|
|
|
16
16
|
declare const treeProcessorPlugin: Plugin<[TreeProcessorPluginOptions?], Node, Node>;
|
|
17
17
|
//#endregion
|
|
18
18
|
export { MdxTreeProcessor, treeProcessorPlugin };
|
|
19
|
-
//# sourceMappingURL=plugin.d.
|
|
19
|
+
//# sourceMappingURL=plugin.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.mts","names":[],"sources":["../../../src/plugins/tree-processor/plugin.ts"],"mappings":";;;;KAKY,gBAAA,IACV,IAAA,EAAM,OAAA,EACN,KAAA,UACA,MAAA,EAAQ,OAAA,YACE,OAAA;;AAJZ;;KASK,0BAAA;EARG,qBAUN,OAAA,EAAS,gBAAA;AAAA;;;;;cAOE,mBAAA,EAAqB,MAAA,EAC/B,0BAAA,IACD,IAAA,EACA,IAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.mjs","names":[],"sources":["../../../src/plugins/tree-processor/plugin.ts"],"sourcesContent":["import type { Plugin } from \"unified\";\nimport { visit } from \"unist-util-visit\";\n\nimport type { HastElement, HastNode } from \"../../types\";\n\nexport type MdxTreeProcessor = (\n node: HastElement,\n index: number,\n parent: HastElement,\n) => void | Promise<void>;\n\n/**\n * Options for {@linkcode treeProcessorPlugin}.\n */\ntype TreeProcessorPluginOptions = {\n /** The processor. */\n process: MdxTreeProcessor;\n};\n\n/**\n * A plugin that allows to process MDX tree nodes.\n * It can be async or sync.\n */\nexport const treeProcessorPlugin: Plugin<\n [TreeProcessorPluginOptions?],\n HastNode,\n HastNode\n> = ({ process } = { process: () => {} }) => {\n return async (tree) => {\n const promises: Promise<void>[] = [];\n\n visit(\n tree,\n \"element\",\n function visitor(\n node: HastElement,\n index: number,\n parent: HastElement,\n ): void {\n const p = process(node, index, parent);\n if (\n p instanceof Promise ||\n Object.prototype.toString.call(p) === \"[object Promise]\"\n ) {\n promises.push(p as Promise<void>);\n }\n },\n );\n\n await Promise.allSettled(promises);\n\n return tree;\n };\n};\n"],"mappings":";;;;;;;AAuBA,MAAa,uBAIR,EAAE,YAAY,EAAE,eAAe,IAAI,KAAK;AAC3C,QAAO,OAAO,SAAS;EACrB,MAAM,WAA4B,EAAE;AAEpC,QACE,MACA,WACA,SAAS,QACP,MACA,OACA,QACM;GACN,MAAM,IAAI,QAAQ,MAAM,OAAO,OAAO;AACtC,OACE,aAAa,WACb,OAAO,UAAU,SAAS,KAAK,EAAE,KAAK,mBAEtC,UAAS,KAAK,EAAmB;IAGtC;AAED,QAAM,QAAQ,WAAW,SAAS;AAElC,SAAO"}
|
package/package.json
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@temelj/mdx",
|
|
3
|
+
"version": "0.10.1",
|
|
3
4
|
"description": "MDX processing utilities.",
|
|
4
5
|
"license": "MIT",
|
|
5
6
|
"repository": {
|
|
6
7
|
"url": "https://github.com/tinrab/temelj"
|
|
7
8
|
},
|
|
8
|
-
"
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
9
12
|
"type": "module",
|
|
10
|
-
"main": "./dist/mod.
|
|
11
|
-
"types": "./dist/mod.d.
|
|
13
|
+
"main": "./dist/mod.mjs",
|
|
14
|
+
"types": "./dist/mod.d.mts",
|
|
12
15
|
"exports": {
|
|
13
16
|
".": {
|
|
14
|
-
"types": "./dist/mod.d.
|
|
15
|
-
"import": "./dist/mod.
|
|
17
|
+
"types": "./dist/mod.d.mts",
|
|
18
|
+
"import": "./dist/mod.mjs"
|
|
16
19
|
}
|
|
17
20
|
},
|
|
18
|
-
"files": [
|
|
19
|
-
"dist"
|
|
20
|
-
],
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"@mdx-js/mdx": "^3.1.1",
|
|
23
|
-
"@shikijs/transformers": "^3.
|
|
23
|
+
"@shikijs/transformers": "^3.22.0",
|
|
24
24
|
"@types/hast": "^3.0.4",
|
|
25
25
|
"github-slugger": "^2.0.0",
|
|
26
26
|
"hast": "^1.0.0",
|
|
@@ -28,20 +28,20 @@
|
|
|
28
28
|
"remark-frontmatter": "^5.0.0",
|
|
29
29
|
"remark-gfm": "^4.0.1",
|
|
30
30
|
"remark-parse": "^11.0.0",
|
|
31
|
-
"shiki": "^3.
|
|
31
|
+
"shiki": "^3.22.0",
|
|
32
32
|
"unified": "^11.0.5",
|
|
33
33
|
"unist-util-remove": "^4.0.0",
|
|
34
34
|
"unist-util-visit": "^5.1.0",
|
|
35
35
|
"vfile": "^6.0.3",
|
|
36
36
|
"vfile-matter": "^5.0.1",
|
|
37
37
|
"zod": "^4.3.6",
|
|
38
|
-
"@temelj/iterator": "0.
|
|
38
|
+
"@temelj/iterator": "0.10.1"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/mdast": "^4.0.4",
|
|
42
|
-
"@types/node": "25.
|
|
43
|
-
"tsdown": "^0.20.1",
|
|
42
|
+
"@types/node": "25.2.1",
|
|
44
43
|
"shx": "^0.4.0",
|
|
44
|
+
"tsdown": "^0.20.3",
|
|
45
45
|
"typescript": "^5.9.3",
|
|
46
46
|
"vitest": "^4.0.18"
|
|
47
47
|
},
|
package/dist/compiler.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"compiler.d.ts","names":[],"sources":["../src/compiler.ts"],"mappings":";;;;;;;;;AAkBA;;KAAY,SAAA,YAAqB,UAAA;;;AAKjC;UAAiB,iBAAA;EACf,eAAA;EACA,UAAA,GAAa,cAAA;AAAA;;;;;UAOE,WAAA,gBAA2B,MAAA;EAC1C,QAAA;EACA,WAAA,EAAa,YAAA;AAAA;;;;cAMF,WAAA;EAAA,iBACM,UAAA;EAAA,iBACA,aAAA;EAAA,iBACA,aAAA;cAEE,UAAA,GAAY,cAAA;EAMxB,gBAAA,UAAA,CACL,MAAA,EAAQ,MAAA,EAAQ,QAAA,IAAY,IAAA,EAAU,IAAA,GACtC,OAAA,GAAU,QAAA,GACT,WAAA;EASI,gBAAA,UAAA,CACL,MAAA,EAAQ,MAAA,EAAQ,QAAA,IAAY,IAAA,EAAU,IAAA,GACtC,OAAA,GAAU,QAAA,GACT,WAAA;EASU,OAAA,4BAAmC,CAAA,CAAE,SAAA,CAAA,CAChD,MAAA,EAAQ,SAAA;IACN,eAAA;IAAyB;EAAA,IAAc,iBAAA,EACzC,iBAAA,GAAoB,kBAAA,GACnB,OAAA,CAAQ,WAAA,CAAY,CAAA,CAAE,MAAA,CAAO,kBAAA;AAAA"}
|
package/dist/compiler.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"compiler.js","names":["compileMdxJs"],"sources":["../src/compiler.ts"],"sourcesContent":["import {\n compile as compileMdxJs,\n type CompileOptions as MdxJsCompileOptions,\n} from \"@mdx-js/mdx\";\nimport remarkFrontmatterPlugin from \"remark-frontmatter\";\nimport remarkGfmPlugin from \"remark-gfm\";\nimport type { Pluggable, PluggableList, Plugin } from \"unified\";\nimport { VFile } from \"vfile\";\nimport { matter } from \"vfile-matter\";\nimport type { z } from \"zod\";\n\nimport type { HastNode } from \"./types\";\n\nexport { remarkFrontmatterPlugin, remarkGfmPlugin };\n\n/**\n * Represents the source content for an MDX document.\n */\nexport type MdxSource = string | Uint8Array;\n\n/**\n * Options for compiling an MDX document.\n */\nexport interface MdxCompileOptions {\n frontmatterOnly?: boolean | undefined;\n mdxOptions?: MdxJsCompileOptions;\n}\n\n/**\n * The result of compiling an MDX document.\n * Compiled content is optional, because sometimes we only need the frontmatter.\n */\nexport interface MdxArtifact<TFrontmatter = Record<string, unknown>> {\n compiled?: string | undefined;\n frontmatter: TFrontmatter;\n}\n\n/**\n * A reusable compiler for compiling MDX.\n */\nexport class MdxCompiler {\n private readonly mdxOptions: MdxJsCompileOptions;\n private readonly remarkPlugins: PluggableList;\n private readonly rehypePlugins: PluggableList;\n\n public constructor(mdxOptions: MdxJsCompileOptions = {}) {\n this.mdxOptions = mdxOptions;\n this.remarkPlugins = [remarkFrontmatterPlugin, remarkGfmPlugin];\n this.rehypePlugins = [];\n }\n\n public withRemarkPlugin<TOptions>(\n plugin: Plugin<[TOptions?], HastNode, HastNode>,\n options?: TOptions,\n ): MdxCompiler {\n if (options === undefined) {\n this.remarkPlugins.push(plugin as Pluggable);\n } else {\n this.remarkPlugins.push([plugin, options] as Pluggable);\n }\n return this;\n }\n\n public withRehypePlugin<TOptions>(\n plugin: Plugin<[TOptions?], HastNode, HastNode>,\n options?: TOptions,\n ): MdxCompiler {\n if (options === undefined) {\n this.rehypePlugins.push(plugin as Pluggable);\n } else {\n this.rehypePlugins.push([plugin, options] as Pluggable);\n }\n return this;\n }\n\n public async compile<TFrontmatterSchema extends z.ZodSchema>(\n source: MdxSource,\n { frontmatterOnly = false, mdxOptions }: MdxCompileOptions = {},\n frontmatterSchema?: TFrontmatterSchema,\n ): Promise<MdxArtifact<z.output<TFrontmatterSchema>>> {\n const vfile = new VFile({\n value: source,\n // Needed so vfile doesn't need to access fs.\n cwd: \"/\",\n path: \"/\",\n message: {},\n messages: {},\n });\n matter(vfile, { strip: true });\n\n const remarkPlugins = [\n ...this.remarkPlugins,\n ...(this.mdxOptions?.remarkPlugins ?? []),\n ...(mdxOptions?.remarkPlugins ?? []),\n ];\n const rehypePlugins = [\n ...this.rehypePlugins,\n ...(this.mdxOptions?.rehypePlugins ?? []),\n ...(mdxOptions?.rehypePlugins ?? []),\n ];\n\n let compiled: string | undefined;\n if (!frontmatterOnly) {\n const compiledFile = await compileMdxJs(vfile, {\n outputFormat: \"function-body\",\n ...(this.mdxOptions ?? {}),\n ...(mdxOptions ?? {}),\n remarkPlugins,\n rehypePlugins,\n });\n compiled = compiledFile.value.toString();\n }\n\n const frontmatter =\n frontmatterSchema === undefined\n ? (vfile.data.matter ?? {})\n : frontmatterSchema.parse(vfile.data.matter ?? {});\n\n return {\n compiled,\n frontmatter: frontmatter as z.output<TFrontmatterSchema>,\n };\n }\n}\n"],"mappings":";;;;;;;;;;AAwCA,IAAa,cAAb,MAAyB;CACvB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,AAAO,YAAY,aAAkC,EAAE,EAAE;AACvD,OAAK,aAAa;AAClB,OAAK,gBAAgB,CAAC,yBAAyB,gBAAgB;AAC/D,OAAK,gBAAgB,EAAE;;CAGzB,AAAO,iBACL,QACA,SACa;AACb,MAAI,YAAY,OACd,MAAK,cAAc,KAAK,OAAoB;MAE5C,MAAK,cAAc,KAAK,CAAC,QAAQ,QAAQ,CAAc;AAEzD,SAAO;;CAGT,AAAO,iBACL,QACA,SACa;AACb,MAAI,YAAY,OACd,MAAK,cAAc,KAAK,OAAoB;MAE5C,MAAK,cAAc,KAAK,CAAC,QAAQ,QAAQ,CAAc;AAEzD,SAAO;;CAGT,MAAa,QACX,QACA,EAAE,kBAAkB,OAAO,eAAkC,EAAE,EAC/D,mBACoD;EACpD,MAAM,QAAQ,IAAI,MAAM;GACtB,OAAO;GAEP,KAAK;GACL,MAAM;GACN,SAAS,EAAE;GACX,UAAU,EAAE;GACb,CAAC;AACF,SAAO,OAAO,EAAE,OAAO,MAAM,CAAC;EAE9B,MAAM,gBAAgB;GACpB,GAAG,KAAK;GACR,GAAI,KAAK,YAAY,iBAAiB,EAAE;GACxC,GAAI,YAAY,iBAAiB,EAAE;GACpC;EACD,MAAM,gBAAgB;GACpB,GAAG,KAAK;GACR,GAAI,KAAK,YAAY,iBAAiB,EAAE;GACxC,GAAI,YAAY,iBAAiB,EAAE;GACpC;EAED,IAAI;AACJ,MAAI,CAAC,gBAQH,aAPqB,MAAMA,QAAa,OAAO;GAC7C,cAAc;GACd,GAAI,KAAK,cAAc,EAAE;GACzB,GAAI,cAAc,EAAE;GACpB;GACA;GACD,CAAC,EACsB,MAAM,UAAU;EAG1C,MAAM,cACJ,sBAAsB,SACjB,MAAM,KAAK,UAAU,EAAE,GACxB,kBAAkB,MAAM,MAAM,KAAK,UAAU,EAAE,CAAC;AAEtD,SAAO;GACL;GACa;GACd"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","names":[],"sources":["../../../src/plugins/heading-id/plugin.ts"],"mappings":";;;;;;AAUA;UAAiB,sBAAA;;EAEf,MAAA;AAAA;AAMF;;;AAAA,cAAa,eAAA,EAAiB,MAAA,EAC3B,sBAAA,IACD,IAAA,EACA,IAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","names":["hastToString"],"sources":["../../../src/plugins/heading-id/plugin.ts"],"sourcesContent":["import GithubSlugger from \"github-slugger\";\nimport { toString as hastToString } from \"hast-util-to-string\";\nimport type { Plugin } from \"unified\";\nimport { visit } from \"unist-util-visit\";\n\nimport type { HastElement, HastNode } from \"../../types\";\n\n/**\n * Options for {@linkcode headingIdPlugin}.\n */\nexport interface HeadingIdPluginOptions {\n /** Prefix for title slugs. */\n prefix?: string;\n}\n\n/**\n * A rehype plugin that adds IDs to heading tags.\n */\nexport const headingIdPlugin: Plugin<\n [HeadingIdPluginOptions?],\n HastNode,\n HastNode\n> = ({ prefix } = {}) => {\n const slugger = new GithubSlugger();\n return (tree) => {\n slugger.reset();\n visit(\n tree,\n \"element\",\n function visitor(\n node: HastElement,\n index: number,\n parent: HastElement,\n ): void {\n if (!parent || index === null || !node.tagName.startsWith(\"h\")) return;\n const id = (prefix ?? \"\") + slugger.slug(hastToString(node));\n node.properties.id = id;\n },\n );\n return tree;\n };\n};\n"],"mappings":";;;;;;;;AAkBA,MAAa,mBAIR,EAAE,WAAW,EAAE,KAAK;CACvB,MAAM,UAAU,IAAI,eAAe;AACnC,SAAQ,SAAS;AACf,UAAQ,OAAO;AACf,QACE,MACA,WACA,SAAS,QACP,MACA,OACA,QACM;AACN,OAAI,CAAC,UAAU,UAAU,QAAQ,CAAC,KAAK,QAAQ,WAAW,IAAI,CAAE;GAChE,MAAM,MAAM,UAAU,MAAM,QAAQ,KAAKA,SAAa,KAAK,CAAC;AAC5D,QAAK,WAAW,KAAK;IAExB;AACD,SAAO"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"remove-imports-exports.d.ts","names":[],"sources":["../../src/plugins/remove-imports-exports.ts"],"mappings":";;;;;;AAQA;cAAa,0BAAA,EAA4B,MAAA,YAEvC,IAAA,EACA,IAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"remove-imports-exports.js","names":[],"sources":["../../src/plugins/remove-imports-exports.ts"],"sourcesContent":["import type { Plugin } from \"unified\";\nimport { remove } from \"unist-util-remove\";\n\nimport type { HastNode } from \"../types\";\n\n/**\n * A remark plugin which removes all import and export statements.\n */\nexport const removeImportsExportsPlugin: Plugin<\n [unknown],\n HastNode,\n HastNode\n> = () => {\n return (tree: HastNode) => {\n remove(tree, \"mdxjsEsm\");\n return tree;\n };\n};\n"],"mappings":";;;;;;AAQA,MAAa,mCAIH;AACR,SAAQ,SAAmB;AACzB,SAAO,MAAM,WAAW;AACxB,SAAO"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"code-meta.d.ts","names":[],"sources":["../../../src/plugins/syntax-highlight/code-meta.ts"],"mappings":";;;;UAKiB,WAAA;EACf,SAAA,GAAY,wBAAA;EACZ,eAAA,GAAkB,sBAAA;EAClB,WAAA,GAAc,sBAAA;EACd,QAAA;AAAA;AAAA,KAGU,wBAAA;EACV,SAAA,EAAW,YAAA;AAAA;AAAA,KAGD,sBAAA;EACV,SAAA,EAAW,YAAA;AAAA;AAAA,KAGD,sBAAA;EACV,YAAA,EAAc,YAAA;AAAA;AAAA,iBAGA,eAAA,CAAgB,IAAA,EAAM,OAAA,GAAc,WAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"code-meta.js","names":["z"],"sources":["../../../src/plugins/syntax-highlight/code-meta.ts"],"sourcesContent":["import { type NumericRange, parseNumericRange } from \"@temelj/iterator\";\nimport * as z from \"zod\";\n\nimport type { HastElement } from \"../../types\";\n\nexport interface MdxCodeMeta {\n highlight?: MdxCodeMetaHighlightLine;\n showLineNumbers?: MdxCodeMetaLineNumbers;\n commandLine?: MdxCodeMetaCommandLine;\n fileName?: string;\n}\n\nexport type MdxCodeMetaHighlightLine = {\n lineRange: NumericRange;\n};\n\nexport type MdxCodeMetaLineNumbers = {\n lineRange: NumericRange;\n};\n\nexport type MdxCodeMetaCommandLine = {\n commandRange: NumericRange;\n};\n\nexport function extractCodeMeta(node: HastElement): MdxCodeMeta {\n const meta = node?.data?.meta ?? node?.properties?.metastring ?? \"\";\n return parseMetaString(meta as string);\n}\n\nconst metaSchema = z.object({\n highlight: z.optional(z.string()),\n showLineNumbers: z.optional(z.union([z.boolean(), z.string()])),\n commandLine: z.optional(z.union([z.boolean(), z.string()])),\n fileName: z.optional(z.string()),\n});\n\nfunction parseMetaString(meta: string): MdxCodeMeta {\n let raw: unknown;\n try {\n raw = JSON.parse(meta.trim());\n } catch {\n return {};\n }\n const parsed = metaSchema.parse(raw);\n const options: MdxCodeMeta = {};\n\n if (parsed.highlight !== undefined) {\n options.highlight = { lineRange: parseNumericRange(parsed.highlight) };\n }\n\n if (parsed.showLineNumbers !== undefined) {\n if (typeof parsed.showLineNumbers === \"string\") {\n options.showLineNumbers = {\n lineRange: parseNumericRange(parsed.showLineNumbers),\n };\n } else if (parsed.showLineNumbers) {\n options.showLineNumbers = { lineRange: [] };\n }\n }\n\n if (parsed.commandLine !== undefined) {\n if (typeof parsed.commandLine === \"string\") {\n options.commandLine = {\n commandRange: parseNumericRange(parsed.commandLine),\n };\n } else if (parsed.commandLine) {\n options.commandLine = { commandRange: [] };\n }\n }\n\n if (parsed.fileName !== undefined) {\n options.fileName = parsed.fileName.trim();\n }\n\n return options;\n}\n"],"mappings":";;;;AAwBA,SAAgB,gBAAgB,MAAgC;AAE9D,QAAO,gBADM,MAAM,MAAM,QAAQ,MAAM,YAAY,cAAc,GAC3B;;AAGxC,MAAM,aAAaA,IAAE,OAAO;CAC1B,WAAWA,IAAE,SAASA,IAAE,QAAQ,CAAC;CACjC,iBAAiBA,IAAE,SAASA,IAAE,MAAM,CAACA,IAAE,SAAS,EAAEA,IAAE,QAAQ,CAAC,CAAC,CAAC;CAC/D,aAAaA,IAAE,SAASA,IAAE,MAAM,CAACA,IAAE,SAAS,EAAEA,IAAE,QAAQ,CAAC,CAAC,CAAC;CAC3D,UAAUA,IAAE,SAASA,IAAE,QAAQ,CAAC;CACjC,CAAC;AAEF,SAAS,gBAAgB,MAA2B;CAClD,IAAI;AACJ,KAAI;AACF,QAAM,KAAK,MAAM,KAAK,MAAM,CAAC;SACvB;AACN,SAAO,EAAE;;CAEX,MAAM,SAAS,WAAW,MAAM,IAAI;CACpC,MAAM,UAAuB,EAAE;AAE/B,KAAI,OAAO,cAAc,OACvB,SAAQ,YAAY,EAAE,WAAW,kBAAkB,OAAO,UAAU,EAAE;AAGxE,KAAI,OAAO,oBAAoB,QAC7B;MAAI,OAAO,OAAO,oBAAoB,SACpC,SAAQ,kBAAkB,EACxB,WAAW,kBAAkB,OAAO,gBAAgB,EACrD;WACQ,OAAO,gBAChB,SAAQ,kBAAkB,EAAE,WAAW,EAAE,EAAE;;AAI/C,KAAI,OAAO,gBAAgB,QACzB;MAAI,OAAO,OAAO,gBAAgB,SAChC,SAAQ,cAAc,EACpB,cAAc,kBAAkB,OAAO,YAAY,EACpD;WACQ,OAAO,YAChB,SAAQ,cAAc,EAAE,cAAc,EAAE,EAAE;;AAI9C,KAAI,OAAO,aAAa,OACtB,SAAQ,WAAW,OAAO,SAAS,MAAM;AAG3C,QAAO"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","names":[],"sources":["../../../src/plugins/syntax-highlight/plugin.ts"],"mappings":";;;;;;KAoBK,gBAAA,GAAmB,OAAA,CACtB,iBAAA,CAAkB,eAAA,EAAiB,YAAA;AAAA,KAGhC,mBAAA;AAPoD;;;AAAA,UAYxC,4BAAA;EARoB;;;;EAanC,qBAAA,GAAwB,mBAAA;EAdF;;;EAmBtB,SAAA;IACE,WAAA,GAAc,mCAAA;EAAA;EAhBb;;;EAsBH,WAAA;IAtBsB;AAKxB;;;IAsBI,iBAAA;IAXc;;;IAed,SAAA;EAAA;EArBsB;;;EA2BxB,WAAA;IAfA;;;IAmBE,SAAA;EAAA;EAMF;;;EAAA,gBAAA,GAAmB,gBAAA;AAAA;;;;cAMR,qBAAA,EAAuB,MAAA,EACjC,4BAAA,IACD,IAAA,EACA,IAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","names":["hastToString"],"sources":["../../../src/plugins/syntax-highlight/plugin.ts"],"sourcesContent":["import {\n type TransformerNotationHighlightOptions,\n transformerNotationHighlight,\n} from \"@shikijs/transformers\";\nimport { numericRangeContains } from \"@temelj/iterator\";\nimport type { Properties } from \"hast\";\nimport { toString as hastToString } from \"hast-util-to-string\";\nimport {\n type BundledLanguage,\n type BundledTheme,\n type CodeToHastOptions,\n codeToHast,\n ShikiError,\n} from \"shiki\";\nimport type { Plugin } from \"unified\";\nimport { visit } from \"unist-util-visit\";\n\nimport type { HastElement, HastNode } from \"../../types\";\nimport { extractCodeMeta } from \"./code-meta\";\n\ntype ShikiHastOptions = Partial<\n CodeToHastOptions<BundledLanguage, BundledTheme>\n>;\n\ntype SyntaxDataAttribute = \"source-code\" | \"language\" | \"line-count\";\n\n/**\n * Options for {@linkcode syntaxHighlightPlugin}.\n */\nexport interface SyntaxHighlightPluginOptions {\n /**\n * Include these data attributes on the `<pre>` element.\n * These might be useful to have when building custom code components.\n */\n includeDataAttributes?: SyntaxDataAttribute[];\n\n /**\n * Options for the highlight transformer.\n */\n highlight?: {\n transformer?: TransformerNotationHighlightOptions;\n };\n\n /**\n * Options for line numbers.\n */\n lineNumbers?: {\n /**\n * The name of the variable that contains the line number width.\n * This is used for padding the line numbers so that they align.\n */\n widthVariableName?: string;\n /**\n * A class name to be added to the line numbers.\n */\n className?: string;\n };\n\n /**\n * Options for command line.\n */\n commandLine?: {\n /**\n * A class name to be added to the prompt line.\n */\n className?: string;\n };\n\n /**\n * Extra options for the Shiki hast transformer.\n */\n shikiHastOptions?: ShikiHastOptions;\n}\n\n/**\n * A plugin that highlights code blocks using Shiki.\n */\nexport const syntaxHighlightPlugin: Plugin<\n [SyntaxHighlightPluginOptions?],\n HastNode,\n HastNode\n> = (options: SyntaxHighlightPluginOptions = {}) => {\n return async (tree) => {\n const promises: Promise<void>[] = [];\n\n async function visitor(\n node: HastElement,\n index: number,\n parent: HastElement,\n ): Promise<void> {\n if (!parent || index === null || node.tagName !== \"pre\") {\n return;\n }\n\n const codeNode = node.children[0];\n if (\n !codeNode ||\n codeNode.type !== \"element\" ||\n codeNode.tagName !== \"code\" ||\n !codeNode.properties\n ) {\n return;\n }\n\n const lang = getNodeLanguage(codeNode);\n if (typeof lang !== \"string\") {\n return;\n }\n\n const meta = extractCodeMeta(codeNode);\n\n const sourceCode = hastToString(codeNode).trim();\n\n const hastOptions: CodeToHastOptions<BundledLanguage, BundledTheme> = {\n tokenizeTimeLimit: 10_000,\n lang,\n themes: {\n light: \"github-light-default\",\n dark: \"github-dark-default\",\n },\n ...(options.shikiHastOptions ?? {}),\n };\n if (hastOptions.transformers === undefined) {\n hastOptions.transformers = [];\n }\n\n if (options.highlight) {\n hastOptions.transformers.push(\n transformerNotationHighlight({\n ...(options.highlight.transformer ?? {}),\n matchAlgorithm: \"v3\",\n }),\n );\n }\n\n let lineCount = 0;\n hastOptions.transformers.push({\n code(node): void {\n let lineIndex = 0;\n for (const line of node.children.filter(\n (node: HastNode) => node.type === \"element\",\n ) as HastElement[]) {\n line.properties[\"data-line\"] = lineIndex + 1;\n\n if (options.highlight) {\n if (\n meta.highlight &&\n numericRangeContains(meta.highlight.lineRange, lineIndex)\n ) {\n this.addClassToHast(\n line,\n options.highlight.transformer?.classActiveLine ??\n \"highlighted\",\n );\n }\n }\n\n if (\n options.lineNumbers &&\n meta.showLineNumbers &&\n (meta.showLineNumbers.lineRange.length === 0 ||\n numericRangeContains(meta.showLineNumbers.lineRange, lineIndex))\n ) {\n this.addClassToHast(\n line,\n options.lineNumbers.className ?? \"line-number\",\n );\n }\n\n if (\n options.commandLine &&\n meta.commandLine &&\n (meta.commandLine.commandRange.length === 0 ||\n numericRangeContains(meta.commandLine.commandRange, lineIndex))\n ) {\n this.addClassToHast(\n line,\n options.commandLine.className ?? \"line-command\",\n );\n }\n\n lineIndex++;\n }\n\n lineCount = lineIndex;\n\n if (meta.showLineNumbers) {\n if (typeof node.properties.style !== \"string\") {\n node.properties.style = \"\";\n }\n node.properties.style += `--${\n options.lineNumbers?.widthVariableName ?? \"line-number-width\"\n }:${1 + Math.floor(Math.log10(this.lines.length))}ch;`;\n }\n },\n });\n\n try {\n const hast = await codeToHast(sourceCode, hastOptions);\n\n // Insert highlighted code into parent\n const hastPre = hast.children[0] as HastElement;\n\n const properties: Properties = {\n ...hastPre.properties,\n dataFileName: meta.fileName,\n };\n if (options.includeDataAttributes) {\n if (options.includeDataAttributes.includes(\"language\")) {\n properties.dataLanguage = lang;\n }\n if (options.includeDataAttributes.includes(\"source-code\")) {\n properties.dataSourceCode = sourceCode;\n }\n if (options.includeDataAttributes.includes(\"line-count\")) {\n properties.dataLineCount = lineCount;\n }\n }\n\n parent.children.splice(index, 1, {\n ...hastPre,\n properties,\n });\n } catch (error) {\n if (error instanceof ShikiError) {\n if (\n error.message.includes(\n \"Language `math` is not included in this bundle\",\n )\n ) {\n // Skip if the block is math block.\n return;\n }\n }\n throw error;\n }\n }\n\n visit(\n tree,\n \"element\",\n (node: HastElement, index: number, parent: HastElement) => {\n promises.push(visitor(node, index, parent));\n },\n );\n\n await Promise.all(promises);\n\n return tree;\n };\n};\n\nfunction getNodeLanguage(node: HastElement): string | undefined {\n if (!Array.isArray(node.properties.className)) {\n return undefined;\n }\n const prefix = \"language-\";\n const language = node.properties.className.find(\n (name) => typeof name === \"string\" && name.startsWith(prefix),\n ) as string | undefined;\n if (typeof language === \"string\") {\n return language.slice(prefix.length);\n }\n return language;\n}\n"],"mappings":"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"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","names":[],"sources":["../../../src/plugins/tree-processor/plugin.ts"],"mappings":";;;;KAKY,gBAAA,IACV,IAAA,EAAM,OAAA,EACN,KAAA,UACA,MAAA,EAAQ,OAAA,YACE,OAAA;;AAJZ;;KASK,0BAAA;EARG,qBAUN,OAAA,EAAS,gBAAA;AAAA;;;;;cAOE,mBAAA,EAAqB,MAAA,EAC/B,0BAAA,IACD,IAAA,EACA,IAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","names":[],"sources":["../../../src/plugins/tree-processor/plugin.ts"],"sourcesContent":["import type { Plugin } from \"unified\";\nimport { visit } from \"unist-util-visit\";\n\nimport type { HastElement, HastNode } from \"../../types\";\n\nexport type MdxTreeProcessor = (\n node: HastElement,\n index: number,\n parent: HastElement,\n) => void | Promise<void>;\n\n/**\n * Options for {@linkcode treeProcessorPlugin}.\n */\ntype TreeProcessorPluginOptions = {\n /** The processor. */\n process: MdxTreeProcessor;\n};\n\n/**\n * A plugin that allows to process MDX tree nodes.\n * It can be async or sync.\n */\nexport const treeProcessorPlugin: Plugin<\n [TreeProcessorPluginOptions?],\n HastNode,\n HastNode\n> = ({ process } = { process: () => {} }) => {\n return async (tree) => {\n const promises: Promise<void>[] = [];\n\n visit(\n tree,\n \"element\",\n function visitor(\n node: HastElement,\n index: number,\n parent: HastElement,\n ): void {\n const p = process(node, index, parent);\n if (\n p instanceof Promise ||\n Object.prototype.toString.call(p) === \"[object Promise]\"\n ) {\n promises.push(p as Promise<void>);\n }\n },\n );\n\n await Promise.allSettled(promises);\n\n return tree;\n };\n};\n"],"mappings":";;;;;;;AAuBA,MAAa,uBAIR,EAAE,YAAY,EAAE,eAAe,IAAI,KAAK;AAC3C,QAAO,OAAO,SAAS;EACrB,MAAM,WAA4B,EAAE;AAEpC,QACE,MACA,WACA,SAAS,QACP,MACA,OACA,QACM;GACN,MAAM,IAAI,QAAQ,MAAM,OAAO,OAAO;AACtC,OACE,aAAa,WACb,OAAO,UAAU,SAAS,KAAK,EAAE,KAAK,mBAEtC,UAAS,KAAK,EAAmB;IAGtC;AAED,QAAM,QAAQ,WAAW,SAAS;AAElC,SAAO"}
|
|
File without changes
|
|
File without changes
|