@temelj/mdx 0.8.1 → 0.8.2
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/compiler.d.ts +25 -19
- package/dist/compiler.d.ts.map +1 -0
- package/dist/compiler.js +62 -70
- package/dist/compiler.js.map +1 -0
- package/dist/mod.d.ts +8 -13
- package/dist/mod.js +8 -3
- package/dist/plugins/heading-id/plugin.d.ts +10 -9
- package/dist/plugins/heading-id/plugin.d.ts.map +1 -0
- package/dist/plugins/heading-id/plugin.js +20 -18
- package/dist/plugins/heading-id/plugin.js.map +1 -0
- package/dist/plugins/mod.d.ts +5 -8
- package/dist/plugins/mod.js +6 -4
- package/dist/plugins/remove-imports-exports.d.ts +7 -6
- package/dist/plugins/remove-imports-exports.d.ts.map +1 -0
- package/dist/plugins/remove-imports-exports.js +13 -7
- package/dist/plugins/remove-imports-exports.js.map +1 -0
- package/dist/plugins/syntax-highlight/code-meta.d.ts +13 -11
- package/dist/plugins/syntax-highlight/code-meta.d.ts.map +1 -0
- package/dist/plugins/syntax-highlight/code-meta.js +30 -42
- package/dist/plugins/syntax-highlight/code-meta.js.map +1 -0
- package/dist/plugins/syntax-highlight/plugin.d.ts +41 -41
- package/dist/plugins/syntax-highlight/plugin.d.ts.map +1 -0
- package/dist/plugins/syntax-highlight/plugin.js +88 -141
- package/dist/plugins/syntax-highlight/plugin.js.map +1 -0
- package/dist/plugins/tree-processor/plugin.d.ts +9 -9
- package/dist/plugins/tree-processor/plugin.d.ts.map +1 -0
- package/dist/plugins/tree-processor/plugin.js +20 -20
- package/dist/plugins/tree-processor/plugin.js.map +1 -0
- package/dist/types.d.ts +2 -1
- package/dist/types.js +1 -0
- package/package.json +4 -4
package/dist/compiler.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import
|
|
5
|
-
import { z } from
|
|
6
|
-
import {
|
|
1
|
+
import { HastNode as Node } from "./types.js";
|
|
2
|
+
import { CompileOptions } from "@mdx-js/mdx";
|
|
3
|
+
import remarkFrontmatterPlugin from "remark-frontmatter";
|
|
4
|
+
import remarkGfmPlugin from "remark-gfm";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { Plugin } from "unified";
|
|
7
|
+
|
|
8
|
+
//#region src/compiler.d.ts
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* Represents the source content for an MDX document.
|
|
@@ -13,28 +15,32 @@ type MdxSource = string | Uint8Array;
|
|
|
13
15
|
* Options for compiling an MDX document.
|
|
14
16
|
*/
|
|
15
17
|
interface MdxCompileOptions {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
frontmatterOnly?: boolean | undefined;
|
|
19
|
+
mdxOptions?: CompileOptions;
|
|
18
20
|
}
|
|
19
21
|
/**
|
|
20
22
|
* The result of compiling an MDX document.
|
|
21
23
|
* Compiled content is optional, because sometimes we only need the frontmatter.
|
|
22
24
|
*/
|
|
23
25
|
interface MdxArtifact<TFrontmatter = Record<string, unknown>> {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
+
compiled?: string | undefined;
|
|
27
|
+
frontmatter: TFrontmatter;
|
|
26
28
|
}
|
|
27
29
|
/**
|
|
28
30
|
* A reusable compiler for compiling MDX.
|
|
29
31
|
*/
|
|
30
32
|
declare class MdxCompiler {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
private readonly mdxOptions;
|
|
34
|
+
private readonly remarkPlugins;
|
|
35
|
+
private readonly rehypePlugins;
|
|
36
|
+
constructor(mdxOptions?: CompileOptions);
|
|
37
|
+
withRemarkPlugin<TOptions>(plugin: Plugin<[TOptions?], Node, Node>, options?: TOptions): MdxCompiler;
|
|
38
|
+
withRehypePlugin<TOptions>(plugin: Plugin<[TOptions?], Node, Node>, options?: TOptions): MdxCompiler;
|
|
39
|
+
compile<TFrontmatterSchema extends z.ZodSchema>(source: MdxSource, {
|
|
40
|
+
frontmatterOnly,
|
|
41
|
+
mdxOptions
|
|
42
|
+
}?: MdxCompileOptions, frontmatterSchema?: TFrontmatterSchema): Promise<MdxArtifact<z.output<TFrontmatterSchema>>>;
|
|
38
43
|
}
|
|
39
|
-
|
|
40
|
-
export {
|
|
44
|
+
//#endregion
|
|
45
|
+
export { MdxArtifact, MdxCompileOptions, MdxCompiler, MdxSource, remarkFrontmatterPlugin, remarkGfmPlugin };
|
|
46
|
+
//# sourceMappingURL=compiler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compiler.d.ts","names":[],"sources":["../src/compiler.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;AAkBA;AAKA;AASiB,KAdL,SAAA,GAcgB,MAAA,GAdK,UAcW;AAQ5C;;;AAYgC,UA7Bf,iBAAA,CA6Be;EAAU,eAAA,CAAA,EAAA,OAAA,GAAA,SAAA;EAA9B,UAAA,CAAA,EA3BG,cA2BH;;;;;;AAYA,UAhCK,WAgCL,CAAA,eAhCgC,MAgChC,CAAA,MAAA,EAAA,OAAA,CAAA,CAAA,CAAA;EACE,QAAA,CAAA,EAAA,MAAA,GAAA,SAAA;EACT,WAAA,EAhCU,YAgCV;;;;;AAWwC,cArChC,WAAA,CAqCgC;EACrB,iBAAA,UAAA;EACU,iBAAA,aAAA;EAAP,iBAAA,aAAA;EAAd,WAAA,CAAA,UAAA,CAAA,EAlCoB,cAkCpB;EAAR,gBAAA,CAAA,QAAA,CAAA,CAAA,MAAA,EA3BO,MA2BP,CAAA,CA3Be,QA2Bf,CAAA,CAAA,EA3B2B,IA2B3B,EA3BqC,IA2BrC,CAAA,EAAA,OAAA,CAAA,EA1BS,QA0BT,CAAA,EAzBA,WAyBA;EAAO,gBAAA,CAAA,QAAA,CAAA,CAAA,MAAA,EAfA,MAeA,CAAA,CAfQ,QAeR,CAAA,CAAA,EAfoB,IAepB,EAf8B,IAe9B,CAAA,EAAA,OAAA,CAAA,EAdE,QAcF,CAAA,EAbP,WAaO;qCAJsC,CAAA,CAAE,mBACxC;;;MACiC,uCACrB,qBACnB,QAAQ,YAAY,CAAA,CAAE,OAAO"}
|
package/dist/compiler.js
CHANGED
|
@@ -1,75 +1,67 @@
|
|
|
1
|
-
import {
|
|
2
|
-
compile as compileMdxJs
|
|
3
|
-
} from "@mdx-js/mdx";
|
|
1
|
+
import { compile } from "@mdx-js/mdx";
|
|
4
2
|
import remarkFrontmatterPlugin from "remark-frontmatter";
|
|
5
3
|
import remarkGfmPlugin from "remark-gfm";
|
|
6
4
|
import { VFile } from "vfile";
|
|
7
5
|
import { matter } from "vfile-matter";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return {
|
|
66
|
-
compiled,
|
|
67
|
-
frontmatter
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
export {
|
|
72
|
-
MdxCompiler,
|
|
73
|
-
remarkFrontmatterPlugin,
|
|
74
|
-
remarkGfmPlugin
|
|
6
|
+
|
|
7
|
+
//#region src/compiler.ts
|
|
8
|
+
/**
|
|
9
|
+
* A reusable compiler for compiling MDX.
|
|
10
|
+
*/
|
|
11
|
+
var MdxCompiler = class {
|
|
12
|
+
mdxOptions;
|
|
13
|
+
remarkPlugins;
|
|
14
|
+
rehypePlugins;
|
|
15
|
+
constructor(mdxOptions = {}) {
|
|
16
|
+
this.mdxOptions = mdxOptions;
|
|
17
|
+
this.remarkPlugins = [remarkFrontmatterPlugin, remarkGfmPlugin];
|
|
18
|
+
this.rehypePlugins = [];
|
|
19
|
+
}
|
|
20
|
+
withRemarkPlugin(plugin, options) {
|
|
21
|
+
if (options === void 0) this.remarkPlugins.push(plugin);
|
|
22
|
+
else this.remarkPlugins.push([plugin, options]);
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
withRehypePlugin(plugin, options) {
|
|
26
|
+
if (options === void 0) this.rehypePlugins.push(plugin);
|
|
27
|
+
else this.rehypePlugins.push([plugin, options]);
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
async compile(source, { frontmatterOnly = false, mdxOptions } = {}, frontmatterSchema) {
|
|
31
|
+
const vfile = new VFile({
|
|
32
|
+
value: source,
|
|
33
|
+
cwd: "/",
|
|
34
|
+
path: "/",
|
|
35
|
+
message: {},
|
|
36
|
+
messages: {}
|
|
37
|
+
});
|
|
38
|
+
matter(vfile, { strip: true });
|
|
39
|
+
const remarkPlugins = [
|
|
40
|
+
...this.remarkPlugins,
|
|
41
|
+
...this.mdxOptions?.remarkPlugins ?? [],
|
|
42
|
+
...mdxOptions?.remarkPlugins ?? []
|
|
43
|
+
];
|
|
44
|
+
const rehypePlugins = [
|
|
45
|
+
...this.rehypePlugins,
|
|
46
|
+
...this.mdxOptions?.rehypePlugins ?? [],
|
|
47
|
+
...mdxOptions?.rehypePlugins ?? []
|
|
48
|
+
];
|
|
49
|
+
let compiled;
|
|
50
|
+
if (!frontmatterOnly) compiled = (await compile(vfile, {
|
|
51
|
+
outputFormat: "function-body",
|
|
52
|
+
...this.mdxOptions ?? {},
|
|
53
|
+
...mdxOptions ?? {},
|
|
54
|
+
remarkPlugins,
|
|
55
|
+
rehypePlugins
|
|
56
|
+
})).value.toString();
|
|
57
|
+
const frontmatter = frontmatterSchema === void 0 ? vfile.data.matter ?? {} : frontmatterSchema.parse(vfile.data.matter ?? {});
|
|
58
|
+
return {
|
|
59
|
+
compiled,
|
|
60
|
+
frontmatter
|
|
61
|
+
};
|
|
62
|
+
}
|
|
75
63
|
};
|
|
64
|
+
|
|
65
|
+
//#endregion
|
|
66
|
+
export { MdxCompiler, remarkFrontmatterPlugin, remarkGfmPlugin };
|
|
67
|
+
//# sourceMappingURL=compiler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compiler.js","names":["compiled: string | undefined","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,IAAIA;AACJ,MAAI,CAAC,gBAQH,aAPqB,MAAMC,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"}
|
package/dist/mod.d.ts
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export {
|
|
9
|
-
import '@mdx-js/mdx';
|
|
10
|
-
import 'unified';
|
|
11
|
-
import 'zod';
|
|
12
|
-
import 'shiki';
|
|
13
|
-
import '@shikijs/transformers';
|
|
1
|
+
import { HastElement as Element, HastNode as Node } from "./types.js";
|
|
2
|
+
import { MdxArtifact, MdxCompileOptions, MdxCompiler, MdxSource, remarkFrontmatterPlugin, remarkGfmPlugin } from "./compiler.js";
|
|
3
|
+
import { HeadingIdPluginOptions, headingIdPlugin } from "./plugins/heading-id/plugin.js";
|
|
4
|
+
import { SyntaxHighlightPluginOptions, syntaxHighlightPlugin } from "./plugins/syntax-highlight/plugin.js";
|
|
5
|
+
import { MdxTreeProcessor, treeProcessorPlugin } from "./plugins/tree-processor/plugin.js";
|
|
6
|
+
import { removeImportsExportsPlugin } from "./plugins/remove-imports-exports.js";
|
|
7
|
+
import "./plugins/mod.js";
|
|
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
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { MdxCompiler, remarkFrontmatterPlugin, remarkGfmPlugin } from "./compiler.js";
|
|
2
|
+
import { headingIdPlugin } from "./plugins/heading-id/plugin.js";
|
|
3
|
+
import { syntaxHighlightPlugin } from "./plugins/syntax-highlight/plugin.js";
|
|
4
|
+
import { treeProcessorPlugin } from "./plugins/tree-processor/plugin.js";
|
|
5
|
+
import { removeImportsExportsPlugin } from "./plugins/remove-imports-exports.js";
|
|
6
|
+
import "./plugins/mod.js";
|
|
7
|
+
|
|
8
|
+
export { MdxCompiler, headingIdPlugin, remarkFrontmatterPlugin, remarkGfmPlugin, removeImportsExportsPlugin, syntaxHighlightPlugin, treeProcessorPlugin };
|
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { HastNode as Node } from "../../types.js";
|
|
2
|
+
import { Plugin } from "unified";
|
|
3
|
+
|
|
4
|
+
//#region src/plugins/heading-id/plugin.d.ts
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* Options for {@linkcode headingIdPlugin}.
|
|
6
8
|
*/
|
|
7
9
|
interface HeadingIdPluginOptions {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
/** Prefix for title slugs. */
|
|
11
|
+
prefix?: string;
|
|
10
12
|
}
|
|
11
13
|
/**
|
|
12
14
|
* A rehype plugin that adds IDs to heading tags.
|
|
13
15
|
*/
|
|
14
|
-
declare const headingIdPlugin: Plugin<[
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
export { type HeadingIdPluginOptions, headingIdPlugin };
|
|
16
|
+
declare const headingIdPlugin: Plugin<[HeadingIdPluginOptions?], Node, Node>;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { HeadingIdPluginOptions, headingIdPlugin };
|
|
19
|
+
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","names":[],"sources":["../../../src/plugins/heading-id/plugin.ts"],"sourcesContent":[],"mappings":";;;;;;;AAUA;AAQa,UARI,sBAAA,CA+BhB;EAtBE;EACD,MAAA,CAAA,EAAA,MAAA;;;;;cAFW,iBAAiB,QAC3B,0BACD,MACA"}
|
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
import GithubSlugger from "github-slugger";
|
|
2
|
-
import { toString
|
|
2
|
+
import { toString } from "hast-util-to-string";
|
|
3
3
|
import { visit } from "unist-util-visit";
|
|
4
|
+
|
|
5
|
+
//#region src/plugins/heading-id/plugin.ts
|
|
6
|
+
/**
|
|
7
|
+
* A rehype plugin that adds IDs to heading tags.
|
|
8
|
+
*/
|
|
4
9
|
const headingIdPlugin = ({ prefix } = {}) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
);
|
|
17
|
-
return tree;
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
export {
|
|
21
|
-
headingIdPlugin
|
|
10
|
+
const slugger = new GithubSlugger();
|
|
11
|
+
return (tree) => {
|
|
12
|
+
slugger.reset();
|
|
13
|
+
visit(tree, "element", function visitor(node, index, parent) {
|
|
14
|
+
if (!parent || index === null || !node.tagName.startsWith("h")) return;
|
|
15
|
+
const id = (prefix ?? "") + slugger.slug(toString(node));
|
|
16
|
+
node.properties.id = id;
|
|
17
|
+
});
|
|
18
|
+
return tree;
|
|
19
|
+
};
|
|
22
20
|
};
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
export { headingIdPlugin };
|
|
24
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","names":["headingIdPlugin: Plugin<\n [HeadingIdPluginOptions?],\n HastNode,\n HastNode\n>","hastToString"],"sources":["../../../src/plugins/heading-id/plugin.ts"],"sourcesContent":["import GithubSlugger from \"github-slugger\";\nimport { toString as hastToString } from \"hast-util-to-string\";\nimport { visit } from \"unist-util-visit\";\nimport type { Plugin } from \"unified\";\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,MAAaA,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,KAAKC,SAAa,KAAK,CAAC;AAC5D,QAAK,WAAW,KAAK;IAExB;AACD,SAAO"}
|
package/dist/plugins/mod.d.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import 'hast';
|
|
7
|
-
import 'shiki';
|
|
8
|
-
import '@shikijs/transformers';
|
|
1
|
+
import { HeadingIdPluginOptions, headingIdPlugin } from "./heading-id/plugin.js";
|
|
2
|
+
import { SyntaxHighlightPluginOptions, syntaxHighlightPlugin } from "./syntax-highlight/plugin.js";
|
|
3
|
+
import { MdxTreeProcessor, treeProcessorPlugin } from "./tree-processor/plugin.js";
|
|
4
|
+
import { removeImportsExportsPlugin } from "./remove-imports-exports.js";
|
|
5
|
+
export { HeadingIdPluginOptions, MdxTreeProcessor, SyntaxHighlightPluginOptions, headingIdPlugin, removeImportsExportsPlugin, syntaxHighlightPlugin, treeProcessorPlugin };
|
package/dist/plugins/mod.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { headingIdPlugin } from "./heading-id/plugin.js";
|
|
2
|
+
import { syntaxHighlightPlugin } from "./syntax-highlight/plugin.js";
|
|
3
|
+
import { treeProcessorPlugin } from "./tree-processor/plugin.js";
|
|
4
|
+
import { removeImportsExportsPlugin } from "./remove-imports-exports.js";
|
|
5
|
+
|
|
6
|
+
export { headingIdPlugin, removeImportsExportsPlugin, syntaxHighlightPlugin, treeProcessorPlugin };
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { HastNode as Node } from "../types.js";
|
|
2
|
+
import { Plugin } from "unified";
|
|
3
|
+
|
|
4
|
+
//#region src/plugins/remove-imports-exports.d.ts
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* A remark plugin which removes all import and export statements.
|
|
6
8
|
*/
|
|
7
|
-
declare const removeImportsExportsPlugin: Plugin<[
|
|
8
|
-
|
|
9
|
-
], Node, Node>;
|
|
10
|
-
|
|
9
|
+
declare const removeImportsExportsPlugin: Plugin<[unknown], Node, Node>;
|
|
10
|
+
//#endregion
|
|
11
11
|
export { removeImportsExportsPlugin };
|
|
12
|
+
//# sourceMappingURL=remove-imports-exports.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remove-imports-exports.d.ts","names":[],"sources":["../../src/plugins/remove-imports-exports.ts"],"sourcesContent":[],"mappings":";;;;;;;AAQA;AAEE,cAFW,0BAEX,EAFuC,MAEvC,CAAA,CACA,OAAA,CAHuC,EAEvC,IAFuC,EAGvC,IAHuC,CAAA"}
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import { remove } from "unist-util-remove";
|
|
2
|
+
|
|
3
|
+
//#region src/plugins/remove-imports-exports.ts
|
|
4
|
+
/**
|
|
5
|
+
* A remark plugin which removes all import and export statements.
|
|
6
|
+
*/
|
|
2
7
|
const removeImportsExportsPlugin = () => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
};
|
|
8
|
-
export {
|
|
9
|
-
removeImportsExportsPlugin
|
|
8
|
+
return (tree) => {
|
|
9
|
+
remove(tree, "mdxjsEsm");
|
|
10
|
+
return tree;
|
|
11
|
+
};
|
|
10
12
|
};
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
export { removeImportsExportsPlugin };
|
|
16
|
+
//# sourceMappingURL=remove-imports-exports.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remove-imports-exports.js","names":["removeImportsExportsPlugin: Plugin<\n [unknown],\n HastNode,\n HastNode\n>"],"sources":["../../src/plugins/remove-imports-exports.ts"],"sourcesContent":["import { remove } from \"unist-util-remove\";\nimport type { Plugin } from \"unified\";\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,MAAaA,mCAIH;AACR,SAAQ,SAAmB;AACzB,SAAO,MAAM,WAAW;AACxB,SAAO"}
|
|
@@ -1,21 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { HastElement as Element } from "../../types.js";
|
|
2
|
+
import { NumericRange } from "@temelj/iterator";
|
|
3
3
|
|
|
4
|
+
//#region src/plugins/syntax-highlight/code-meta.d.ts
|
|
4
5
|
interface MdxCodeMeta {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
highlight?: MdxCodeMetaHighlightLine;
|
|
7
|
+
showLineNumbers?: MdxCodeMetaLineNumbers;
|
|
8
|
+
commandLine?: MdxCodeMetaCommandLine;
|
|
9
|
+
fileName?: string;
|
|
9
10
|
}
|
|
10
11
|
type MdxCodeMetaHighlightLine = {
|
|
11
|
-
|
|
12
|
+
lineRange: NumericRange;
|
|
12
13
|
};
|
|
13
14
|
type MdxCodeMetaLineNumbers = {
|
|
14
|
-
|
|
15
|
+
lineRange: NumericRange;
|
|
15
16
|
};
|
|
16
17
|
type MdxCodeMetaCommandLine = {
|
|
17
|
-
|
|
18
|
+
commandRange: NumericRange;
|
|
18
19
|
};
|
|
19
20
|
declare function extractCodeMeta(node: Element): MdxCodeMeta;
|
|
20
|
-
|
|
21
|
-
export {
|
|
21
|
+
//#endregion
|
|
22
|
+
export { MdxCodeMeta, MdxCodeMetaCommandLine, MdxCodeMetaHighlightLine, MdxCodeMetaLineNumbers, extractCodeMeta };
|
|
23
|
+
//# sourceMappingURL=code-meta.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code-meta.d.ts","names":[],"sources":["../../../src/plugins/syntax-highlight/code-meta.ts"],"sourcesContent":[],"mappings":";;;;UAKiB,WAAA;cACH;EADG,eAAW,CAAA,EAER,sBAFQ;EACd,WAAA,CAAA,EAEE,sBAFF;EACM,QAAA,CAAA,EAAA,MAAA;;AACkB,KAI1B,wBAAA,GAJ0B;EAI1B,SAAA,EACC,YADD;AAIZ,CAAA;AAIY,KAJA,sBAAA,GAIsB;EAIlB,SAAA,EAPH,YAOkB;;KAJnB,sBAAA;gBACI;;iBAGA,eAAA,OAAsB,UAAc"}
|
|
@@ -1,50 +1,38 @@
|
|
|
1
1
|
import { parseNumericRange } from "@temelj/iterator";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
//#region src/plugins/syntax-highlight/code-meta.ts
|
|
3
5
|
function extractCodeMeta(node) {
|
|
4
|
-
|
|
5
|
-
return parseMetaString(meta);
|
|
6
|
+
return parseMetaString(node?.data?.meta ?? node?.properties?.metastring ?? "");
|
|
6
7
|
}
|
|
7
8
|
const metaSchema = z.object({
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
highlight: z.optional(z.string()),
|
|
10
|
+
showLineNumbers: z.optional(z.union([z.boolean(), z.string()])),
|
|
11
|
+
commandLine: z.optional(z.union([z.boolean(), z.string()])),
|
|
12
|
+
fileName: z.optional(z.string())
|
|
12
13
|
});
|
|
13
14
|
function parseMetaString(meta) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
if (parsed.commandLine !== void 0) {
|
|
35
|
-
if (typeof parsed.commandLine === "string") {
|
|
36
|
-
options.commandLine = {
|
|
37
|
-
commandRange: parseNumericRange(parsed.commandLine)
|
|
38
|
-
};
|
|
39
|
-
} else if (parsed.commandLine) {
|
|
40
|
-
options.commandLine = { commandRange: [] };
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
if (parsed.fileName !== void 0) {
|
|
44
|
-
options.fileName = parsed.fileName.trim();
|
|
45
|
-
}
|
|
46
|
-
return options;
|
|
15
|
+
let raw;
|
|
16
|
+
try {
|
|
17
|
+
raw = JSON.parse(meta.trim());
|
|
18
|
+
} catch {
|
|
19
|
+
return {};
|
|
20
|
+
}
|
|
21
|
+
const parsed = metaSchema.parse(raw);
|
|
22
|
+
const options = {};
|
|
23
|
+
if (parsed.highlight !== void 0) options.highlight = { lineRange: parseNumericRange(parsed.highlight) };
|
|
24
|
+
if (parsed.showLineNumbers !== void 0) {
|
|
25
|
+
if (typeof parsed.showLineNumbers === "string") options.showLineNumbers = { lineRange: parseNumericRange(parsed.showLineNumbers) };
|
|
26
|
+
else if (parsed.showLineNumbers) options.showLineNumbers = { lineRange: [] };
|
|
27
|
+
}
|
|
28
|
+
if (parsed.commandLine !== void 0) {
|
|
29
|
+
if (typeof parsed.commandLine === "string") options.commandLine = { commandRange: parseNumericRange(parsed.commandLine) };
|
|
30
|
+
else if (parsed.commandLine) options.commandLine = { commandRange: [] };
|
|
31
|
+
}
|
|
32
|
+
if (parsed.fileName !== void 0) options.fileName = parsed.fileName.trim();
|
|
33
|
+
return options;
|
|
47
34
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
};
|
|
35
|
+
|
|
36
|
+
//#endregion
|
|
37
|
+
export { extractCodeMeta };
|
|
38
|
+
//# sourceMappingURL=code-meta.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code-meta.js","names":["raw: unknown","options: MdxCodeMeta"],"sources":["../../../src/plugins/syntax-highlight/code-meta.ts"],"sourcesContent":["import { type NumericRange, parseNumericRange } from \"@temelj/iterator\";\nimport { 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,aAAa,EAAE,OAAO;CAC1B,WAAW,EAAE,SAAS,EAAE,QAAQ,CAAC;CACjC,iBAAiB,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,SAAS,EAAE,EAAE,QAAQ,CAAC,CAAC,CAAC;CAC/D,aAAa,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,SAAS,EAAE,EAAE,QAAQ,CAAC,CAAC,CAAC;CAC3D,UAAU,EAAE,SAAS,EAAE,QAAQ,CAAC;CACjC,CAAC;AAEF,SAAS,gBAAgB,MAA2B;CAClD,IAAIA;AACJ,KAAI;AACF,QAAM,KAAK,MAAM,KAAK,MAAM,CAAC;SACvB;AACN,SAAO,EAAE;;CAEX,MAAM,SAAS,WAAW,MAAM,IAAI;CACpC,MAAMC,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,58 +1,58 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { HastNode as Node } from "../../types.js";
|
|
2
|
+
import { BundledLanguage, BundledTheme, CodeToHastOptions } from "shiki";
|
|
3
|
+
import { TransformerNotationHighlightOptions } from "@shikijs/transformers";
|
|
4
|
+
import { Plugin } from "unified";
|
|
5
5
|
|
|
6
|
+
//#region src/plugins/syntax-highlight/plugin.d.ts
|
|
6
7
|
type ShikiHastOptions = Partial<CodeToHastOptions<BundledLanguage, BundledTheme>>;
|
|
7
8
|
type SyntaxDataAttribute = "source-code" | "language" | "line-count";
|
|
8
9
|
/**
|
|
9
10
|
* Options for {@linkcode syntaxHighlightPlugin}.
|
|
10
11
|
*/
|
|
11
12
|
interface SyntaxHighlightPluginOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Include these data attributes on the `<pre>` element.
|
|
15
|
+
* These might be useful to have when building custom code components.
|
|
16
|
+
*/
|
|
17
|
+
includeDataAttributes?: SyntaxDataAttribute[];
|
|
18
|
+
/**
|
|
19
|
+
* Options for the highlight transformer.
|
|
20
|
+
*/
|
|
21
|
+
highlight?: {
|
|
22
|
+
transformer?: TransformerNotationHighlightOptions;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Options for line numbers.
|
|
26
|
+
*/
|
|
27
|
+
lineNumbers?: {
|
|
12
28
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
29
|
+
* The name of the variable that contains the line number width.
|
|
30
|
+
* This is used for padding the line numbers so that they align.
|
|
15
31
|
*/
|
|
16
|
-
|
|
32
|
+
widthVariableName?: string;
|
|
17
33
|
/**
|
|
18
|
-
*
|
|
34
|
+
* A class name to be added to the line numbers.
|
|
19
35
|
*/
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
36
|
+
className?: string;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Options for command line.
|
|
40
|
+
*/
|
|
41
|
+
commandLine?: {
|
|
23
42
|
/**
|
|
24
|
-
*
|
|
43
|
+
* A class name to be added to the prompt line.
|
|
25
44
|
*/
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* A class name to be added to the line numbers.
|
|
34
|
-
*/
|
|
35
|
-
className?: string;
|
|
36
|
-
};
|
|
37
|
-
/**
|
|
38
|
-
* Options for command line.
|
|
39
|
-
*/
|
|
40
|
-
commandLine?: {
|
|
41
|
-
/**
|
|
42
|
-
* A class name to be added to the prompt line.
|
|
43
|
-
*/
|
|
44
|
-
className?: string;
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* Extra options for the Shiki hast transformer.
|
|
48
|
-
*/
|
|
49
|
-
shikiHastOptions?: ShikiHastOptions;
|
|
45
|
+
className?: string;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Extra options for the Shiki hast transformer.
|
|
49
|
+
*/
|
|
50
|
+
shikiHastOptions?: ShikiHastOptions;
|
|
50
51
|
}
|
|
51
52
|
/**
|
|
52
53
|
* A plugin that highlights code blocks using Shiki.
|
|
53
54
|
*/
|
|
54
|
-
declare const syntaxHighlightPlugin: Plugin<[
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
export { type SyntaxHighlightPluginOptions, syntaxHighlightPlugin };
|
|
55
|
+
declare const syntaxHighlightPlugin: Plugin<[SyntaxHighlightPluginOptions?], Node, Node>;
|
|
56
|
+
//#endregion
|
|
57
|
+
export { SyntaxHighlightPluginOptions, syntaxHighlightPlugin };
|
|
58
|
+
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","names":[],"sources":["../../../src/plugins/syntax-highlight/plugin.ts"],"sourcesContent":[],"mappings":";;;;;;KAqBK,gBAAA,GAAmB,QACtB,kBAAkB,iBAAiB;KAGhC,mBAAA;AARoD;;;AAKvD,UAQe,4BAAA,CARf;EADsB;;AAAO;AAS/B;EAK0B,qBAAA,CAAA,EAAA,mBAAA,EAAA;EAMR;;;EAqCL,SAAA,CAAA,EAAA;IACV,WAAA,CAAA,EAtCe,mCAsCf;EACD,CAAA;EACA;;;;;;;;;;;;;;;;;;;;;;;;;;qBATmB;;;;;cAMR,uBAAuB,QACjC,gCACD,MACA"}
|
|
@@ -1,147 +1,94 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
codeToHast,
|
|
4
|
-
ShikiError
|
|
5
|
-
} from "shiki";
|
|
1
|
+
import { extractCodeMeta } from "./code-meta.js";
|
|
2
|
+
import { toString } from "hast-util-to-string";
|
|
6
3
|
import { visit } from "unist-util-visit";
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
} from "@shikijs/transformers";
|
|
4
|
+
import { ShikiError, codeToHast } from "shiki";
|
|
5
|
+
import { transformerNotationHighlight } from "@shikijs/transformers";
|
|
10
6
|
import { numericRangeContains } from "@temelj/iterator";
|
|
11
|
-
|
|
7
|
+
|
|
8
|
+
//#region src/plugins/syntax-highlight/plugin.ts
|
|
9
|
+
/**
|
|
10
|
+
* A plugin that highlights code blocks using Shiki.
|
|
11
|
+
*/
|
|
12
12
|
const syntaxHighlightPlugin = (options = {}) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
node2.properties.style += `--${options.lineNumbers?.widthVariableName ?? "line-number-width"}:${1 + Math.floor(Math.log10(this.lines.length))}ch;`;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
try {
|
|
89
|
-
const hast = await codeToHast(sourceCode, hastOptions);
|
|
90
|
-
const hastPre = hast.children[0];
|
|
91
|
-
const properties = {
|
|
92
|
-
...hastPre.properties,
|
|
93
|
-
dataFileName: meta.fileName
|
|
94
|
-
};
|
|
95
|
-
if (options.includeDataAttributes) {
|
|
96
|
-
if (options.includeDataAttributes.includes("language")) {
|
|
97
|
-
properties.dataLanguage = lang;
|
|
98
|
-
}
|
|
99
|
-
if (options.includeDataAttributes.includes("source-code")) {
|
|
100
|
-
properties.dataSourceCode = sourceCode;
|
|
101
|
-
}
|
|
102
|
-
if (options.includeDataAttributes.includes("line-count")) {
|
|
103
|
-
properties.dataLineCount = lineCount;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
parent.children.splice(index, 1, {
|
|
107
|
-
...hastPre,
|
|
108
|
-
properties
|
|
109
|
-
});
|
|
110
|
-
} catch (error) {
|
|
111
|
-
if (error instanceof ShikiError) {
|
|
112
|
-
if (error.message.includes(
|
|
113
|
-
"Language `math` is not included in this bundle"
|
|
114
|
-
)) {
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
throw error;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
visit(
|
|
122
|
-
tree,
|
|
123
|
-
"element",
|
|
124
|
-
(node, index, parent) => {
|
|
125
|
-
promises.push(visitor(node, index, parent));
|
|
126
|
-
}
|
|
127
|
-
);
|
|
128
|
-
await Promise.all(promises);
|
|
129
|
-
return tree;
|
|
130
|
-
};
|
|
13
|
+
return async (tree) => {
|
|
14
|
+
const promises = [];
|
|
15
|
+
async function visitor(node, index, parent) {
|
|
16
|
+
if (!parent || index === null || node.tagName !== "pre") return;
|
|
17
|
+
const codeNode = node.children[0];
|
|
18
|
+
if (!codeNode || codeNode.type !== "element" || codeNode.tagName !== "code" || !codeNode.properties) return;
|
|
19
|
+
const lang = getNodeLanguage(codeNode);
|
|
20
|
+
if (typeof lang !== "string") return;
|
|
21
|
+
const meta = extractCodeMeta(codeNode);
|
|
22
|
+
const sourceCode = toString(codeNode).trim();
|
|
23
|
+
const hastOptions = {
|
|
24
|
+
tokenizeTimeLimit: 1e4,
|
|
25
|
+
lang,
|
|
26
|
+
themes: {
|
|
27
|
+
light: "github-light-default",
|
|
28
|
+
dark: "github-dark-default"
|
|
29
|
+
},
|
|
30
|
+
...options.shikiHastOptions ?? {}
|
|
31
|
+
};
|
|
32
|
+
if (hastOptions.transformers === void 0) hastOptions.transformers = [];
|
|
33
|
+
if (options.highlight) hastOptions.transformers.push(transformerNotationHighlight({
|
|
34
|
+
...options.highlight.transformer ?? {},
|
|
35
|
+
matchAlgorithm: "v3"
|
|
36
|
+
}));
|
|
37
|
+
let lineCount = 0;
|
|
38
|
+
hastOptions.transformers.push({ code(node$1) {
|
|
39
|
+
let lineIndex = 0;
|
|
40
|
+
for (const line of node$1.children.filter((node$2) => node$2.type === "element")) {
|
|
41
|
+
line.properties["data-line"] = lineIndex + 1;
|
|
42
|
+
if (options.highlight) {
|
|
43
|
+
if (meta.highlight && numericRangeContains(meta.highlight.lineRange, lineIndex)) this.addClassToHast(line, options.highlight.transformer?.classActiveLine ?? "highlighted");
|
|
44
|
+
}
|
|
45
|
+
if (options.lineNumbers && meta.showLineNumbers && (meta.showLineNumbers.lineRange.length === 0 || numericRangeContains(meta.showLineNumbers.lineRange, lineIndex))) this.addClassToHast(line, options.lineNumbers.className ?? "line-number");
|
|
46
|
+
if (options.commandLine && meta.commandLine && (meta.commandLine.commandRange.length === 0 || numericRangeContains(meta.commandLine.commandRange, lineIndex))) this.addClassToHast(line, options.commandLine.className ?? "line-command");
|
|
47
|
+
lineIndex++;
|
|
48
|
+
}
|
|
49
|
+
lineCount = lineIndex;
|
|
50
|
+
if (meta.showLineNumbers) {
|
|
51
|
+
if (typeof node$1.properties.style !== "string") node$1.properties.style = "";
|
|
52
|
+
node$1.properties.style += `--${options.lineNumbers?.widthVariableName ?? "line-number-width"}:${1 + Math.floor(Math.log10(this.lines.length))}ch;`;
|
|
53
|
+
}
|
|
54
|
+
} });
|
|
55
|
+
try {
|
|
56
|
+
const hastPre = (await codeToHast(sourceCode, hastOptions)).children[0];
|
|
57
|
+
const properties = {
|
|
58
|
+
...hastPre.properties,
|
|
59
|
+
dataFileName: meta.fileName
|
|
60
|
+
};
|
|
61
|
+
if (options.includeDataAttributes) {
|
|
62
|
+
if (options.includeDataAttributes.includes("language")) properties.dataLanguage = lang;
|
|
63
|
+
if (options.includeDataAttributes.includes("source-code")) properties.dataSourceCode = sourceCode;
|
|
64
|
+
if (options.includeDataAttributes.includes("line-count")) properties.dataLineCount = lineCount;
|
|
65
|
+
}
|
|
66
|
+
parent.children.splice(index, 1, {
|
|
67
|
+
...hastPre,
|
|
68
|
+
properties
|
|
69
|
+
});
|
|
70
|
+
} catch (error) {
|
|
71
|
+
if (error instanceof ShikiError) {
|
|
72
|
+
if (error.message.includes("Language `math` is not included in this bundle")) return;
|
|
73
|
+
}
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
visit(tree, "element", (node, index, parent) => {
|
|
78
|
+
promises.push(visitor(node, index, parent));
|
|
79
|
+
});
|
|
80
|
+
await Promise.all(promises);
|
|
81
|
+
return tree;
|
|
82
|
+
};
|
|
131
83
|
};
|
|
132
84
|
function getNodeLanguage(node) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
(name) => typeof name === "string" && name.startsWith(prefix)
|
|
139
|
-
);
|
|
140
|
-
if (typeof language === "string") {
|
|
141
|
-
return language.slice(prefix.length);
|
|
142
|
-
}
|
|
143
|
-
return language;
|
|
85
|
+
if (!Array.isArray(node.properties.className)) return;
|
|
86
|
+
const prefix = "language-";
|
|
87
|
+
const language = node.properties.className.find((name) => typeof name === "string" && name.startsWith(prefix));
|
|
88
|
+
if (typeof language === "string") return language.slice(9);
|
|
89
|
+
return language;
|
|
144
90
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
};
|
|
91
|
+
|
|
92
|
+
//#endregion
|
|
93
|
+
export { syntaxHighlightPlugin };
|
|
94
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","names":["syntaxHighlightPlugin: Plugin<\n [SyntaxHighlightPluginOptions?],\n HastNode,\n HastNode\n>","promises: Promise<void>[]","hastToString","hastOptions: CodeToHastOptions<BundledLanguage, BundledTheme>","node","properties: Properties"],"sources":["../../../src/plugins/syntax-highlight/plugin.ts"],"sourcesContent":["import { toString as hastToString } from \"hast-util-to-string\";\nimport {\n type BundledLanguage,\n type BundledTheme,\n codeToHast,\n type CodeToHastOptions,\n ShikiError,\n} from \"shiki\";\nimport { visit } from \"unist-util-visit\";\nimport {\n transformerNotationHighlight,\n type TransformerNotationHighlightOptions,\n} from \"@shikijs/transformers\";\nimport { numericRangeContains } from \"@temelj/iterator\";\nimport type { Plugin } from \"unified\";\nimport type { Properties } from \"hast\";\n\nimport type { HastElement, HastNode } from \"../../types\";\n\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":";;;;;;;;;;;AA8EA,MAAaA,yBAIR,UAAwC,EAAE,KAAK;AAClD,QAAO,OAAO,SAAS;EACrB,MAAMC,WAA4B,EAAE;EAEpC,eAAe,QACb,MACA,OACA,QACe;AACf,OAAI,CAAC,UAAU,UAAU,QAAQ,KAAK,YAAY,MAChD;GAGF,MAAM,WAAW,KAAK,SAAS;AAC/B,OACE,CAAC,YACD,SAAS,SAAS,aAClB,SAAS,YAAY,UACrB,CAAC,SAAS,WAEV;GAGF,MAAM,OAAO,gBAAgB,SAAS;AACtC,OAAI,OAAO,SAAS,SAClB;GAGF,MAAM,OAAO,gBAAgB,SAAS;GAEtC,MAAM,aAAaC,SAAa,SAAS,CAAC,MAAM;GAEhD,MAAMC,cAAgE;IACpE,mBAAmB;IACnB;IACA,QAAQ;KACN,OAAO;KACP,MAAM;KACP;IACD,GAAI,QAAQ,oBAAoB,EAAE;IACnC;AACD,OAAI,YAAY,iBAAiB,OAC/B,aAAY,eAAe,EAAE;AAG/B,OAAI,QAAQ,UACV,aAAY,aAAa,KACvB,6BAA6B;IAC3B,GAAI,QAAQ,UAAU,eAAe,EAAE;IACvC,gBAAgB;IACjB,CAAC,CACH;GAGH,IAAI,YAAY;AAChB,eAAY,aAAa,KAAK,EAC5B,KAAK,QAAY;IACf,IAAI,YAAY;AAChB,SAAK,MAAM,QAAQC,OAAK,SAAS,QAC9B,WAAmBA,OAAK,SAAS,UACnC,EAAmB;AAClB,UAAK,WAAW,eAAe,YAAY;AAE3C,SAAI,QAAQ,WACV;UACE,KAAK,aACL,qBAAqB,KAAK,UAAU,WAAW,UAAU,CAEzD,MAAK,eACH,MACA,QAAQ,UAAU,aAAa,mBAC7B,cACH;;AAIL,SACE,QAAQ,eACR,KAAK,oBACJ,KAAK,gBAAgB,UAAU,WAAW,KACzC,qBAAqB,KAAK,gBAAgB,WAAW,UAAU,EAEjE,MAAK,eACH,MACA,QAAQ,YAAY,aAAa,cAClC;AAGH,SACE,QAAQ,eACR,KAAK,gBACJ,KAAK,YAAY,aAAa,WAAW,KACxC,qBAAqB,KAAK,YAAY,cAAc,UAAU,EAEhE,MAAK,eACH,MACA,QAAQ,YAAY,aAAa,eAClC;AAGH;;AAGF,gBAAY;AAEZ,QAAI,KAAK,iBAAiB;AACxB,SAAI,OAAOA,OAAK,WAAW,UAAU,SACnC,QAAK,WAAW,QAAQ;AAE1B,YAAK,WAAW,SAAS,KACvB,QAAQ,aAAa,qBAAqB,oBAC3C,GAAG,IAAI,KAAK,MAAM,KAAK,MAAM,KAAK,MAAM,OAAO,CAAC,CAAC;;MAGvD,CAAC;AAEF,OAAI;IAIF,MAAM,WAHO,MAAM,WAAW,YAAY,YAAY,EAGjC,SAAS;IAE9B,MAAMC,aAAyB;KAC7B,GAAG,QAAQ;KACX,cAAc,KAAK;KACpB;AACD,QAAI,QAAQ,uBAAuB;AACjC,SAAI,QAAQ,sBAAsB,SAAS,WAAW,CACpD,YAAW,eAAe;AAE5B,SAAI,QAAQ,sBAAsB,SAAS,cAAc,CACvD,YAAW,iBAAiB;AAE9B,SAAI,QAAQ,sBAAsB,SAAS,aAAa,CACtD,YAAW,gBAAgB;;AAI/B,WAAO,SAAS,OAAO,OAAO,GAAG;KAC/B,GAAG;KACH;KACD,CAAC;YACK,OAAO;AACd,QAAI,iBAAiB,YACnB;SACE,MAAM,QAAQ,SACZ,iDACD,CAGD;;AAGJ,UAAM;;;AAIV,QACE,MACA,YACC,MAAmB,OAAe,WAAwB;AACzD,YAAS,KAAK,QAAQ,MAAM,OAAO,OAAO,CAAC;IAE9C;AAED,QAAM,QAAQ,IAAI,SAAS;AAE3B,SAAO;;;AAIX,SAAS,gBAAgB,MAAuC;AAC9D,KAAI,CAAC,MAAM,QAAQ,KAAK,WAAW,UAAU,CAC3C;CAEF,MAAM,SAAS;CACf,MAAM,WAAW,KAAK,WAAW,UAAU,MACxC,SAAS,OAAO,SAAS,YAAY,KAAK,WAAW,OAAO,CAC9D;AACD,KAAI,OAAO,aAAa,SACtB,QAAO,SAAS,MAAM,EAAc;AAEtC,QAAO"}
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { HastElement as Element, HastNode as Node } from "../../types.js";
|
|
2
|
+
import { Plugin } from "unified";
|
|
3
3
|
|
|
4
|
+
//#region src/plugins/tree-processor/plugin.d.ts
|
|
4
5
|
type MdxTreeProcessor = (node: Element, index: number, parent: Element) => void | Promise<void>;
|
|
5
6
|
/**
|
|
6
7
|
* Options for {@linkcode treeProcessorPlugin}.
|
|
7
8
|
*/
|
|
8
9
|
type TreeProcessorPluginOptions = {
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
/** The processor. */
|
|
11
|
+
process: MdxTreeProcessor;
|
|
11
12
|
};
|
|
12
13
|
/**
|
|
13
14
|
* A plugin that allows to process MDX tree nodes.
|
|
14
15
|
* It can be async or sync.
|
|
15
16
|
*/
|
|
16
|
-
declare const treeProcessorPlugin: Plugin<[
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
export { type MdxTreeProcessor, treeProcessorPlugin };
|
|
17
|
+
declare const treeProcessorPlugin: Plugin<[TreeProcessorPluginOptions?], Node, Node>;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { MdxTreeProcessor, treeProcessorPlugin };
|
|
20
|
+
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","names":[],"sources":["../../../src/plugins/tree-processor/plugin.ts"],"sourcesContent":[],"mappings":";;;;KAKY,gBAAA,UACJ,gCAEE,mBACE;;AAJZ;;KASK,0BAAA,GANK;EACE;EAAO,OAAA,EAOR,gBAPQ;AAAO,CAAA;AAc1B;;;;AAAkC,cAArB,mBAAqB,EAAA,MAAA,CAAA,CAC/B,0BADqC,CAAA,GAEtC,MACA"}
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { visit } from "unist-util-visit";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
export {
|
|
21
|
-
treeProcessorPlugin
|
|
2
|
+
|
|
3
|
+
//#region src/plugins/tree-processor/plugin.ts
|
|
4
|
+
/**
|
|
5
|
+
* A plugin that allows to process MDX tree nodes.
|
|
6
|
+
* It can be async or sync.
|
|
7
|
+
*/
|
|
8
|
+
const treeProcessorPlugin = ({ process } = { process: () => {} }) => {
|
|
9
|
+
return async (tree) => {
|
|
10
|
+
const promises = [];
|
|
11
|
+
visit(tree, "element", function visitor(node, index, parent) {
|
|
12
|
+
const p = process(node, index, parent);
|
|
13
|
+
if (p instanceof Promise || Object.prototype.toString.call(p) === "[object Promise]") promises.push(p);
|
|
14
|
+
});
|
|
15
|
+
await Promise.allSettled(promises);
|
|
16
|
+
return tree;
|
|
17
|
+
};
|
|
22
18
|
};
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
export { treeProcessorPlugin };
|
|
22
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","names":["treeProcessorPlugin: Plugin<\n [TreeProcessorPluginOptions?],\n HastNode,\n HastNode\n>","promises: Promise<void>[]"],"sources":["../../../src/plugins/tree-processor/plugin.ts"],"sourcesContent":["import { visit } from \"unist-util-visit\";\nimport type { Plugin } from \"unified\";\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,MAAaA,uBAIR,EAAE,YAAY,EAAE,eAAe,IAAI,KAAK;AAC3C,QAAO,OAAO,SAAS;EACrB,MAAMC,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/dist/types.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { Element, Node } from "hast";
|
|
2
|
+
export { type Element as HastElement, type Node as HastNode };
|
package/dist/types.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@temelj/mdx",
|
|
3
3
|
"description": "MDX processing utilities.",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"version": "0.8.
|
|
5
|
+
"version": "0.8.2",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/mod.js",
|
|
8
8
|
"types": "./dist/mod.d.ts",
|
|
@@ -32,18 +32,18 @@
|
|
|
32
32
|
"vfile": "^6.0.3",
|
|
33
33
|
"vfile-matter": "^5.0.1",
|
|
34
34
|
"zod": "^4.1.12",
|
|
35
|
-
"@temelj/iterator": "0.8.
|
|
35
|
+
"@temelj/iterator": "0.8.2"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@types/mdast": "^4.0.4",
|
|
39
39
|
"@types/node": "24.10.1",
|
|
40
|
-
"
|
|
40
|
+
"tsdown": "^0.16.4",
|
|
41
41
|
"shx": "^0.4.0",
|
|
42
42
|
"typescript": "^5.9.3",
|
|
43
43
|
"vitest": "^4.0.8"
|
|
44
44
|
},
|
|
45
45
|
"scripts": {
|
|
46
|
-
"build": "
|
|
46
|
+
"build": "tsdown",
|
|
47
47
|
"test": "vitest run",
|
|
48
48
|
"typecheck": "tsc --noEmit",
|
|
49
49
|
"clean": "shx rm -rf dist"
|