@temelj/mdx 0.8.0 → 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 +28 -20
- package/dist/compiler.d.ts.map +1 -0
- package/dist/compiler.js +62 -71
- package/dist/compiler.js.map +1 -0
- package/dist/mod.d.ts +8 -3
- package/dist/mod.js +8 -3
- package/dist/plugins/heading-id/plugin.d.ts +12 -8
- package/dist/plugins/heading-id/plugin.d.ts.map +1 -0
- package/dist/plugins/heading-id/plugin.js +20 -15
- package/dist/plugins/heading-id/plugin.js.map +1 -0
- package/dist/plugins/mod.d.ts +5 -4
- package/dist/plugins/mod.js +6 -4
- package/dist/plugins/remove-imports-exports.d.ts +9 -5
- 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 +19 -14
- package/dist/plugins/syntax-highlight/code-meta.d.ts.map +1 -0
- package/dist/plugins/syntax-highlight/code-meta.js +31 -43
- package/dist/plugins/syntax-highlight/code-meta.js.map +1 -0
- package/dist/plugins/syntax-highlight/plugin.d.ts +43 -41
- package/dist/plugins/syntax-highlight/plugin.d.ts.map +1 -0
- package/dist/plugins/syntax-highlight/plugin.js +88 -129
- package/dist/plugins/syntax-highlight/plugin.js.map +1 -0
- package/dist/plugins/tree-processor/plugin.d.ts +11 -9
- package/dist/plugins/tree-processor/plugin.d.ts.map +1 -0
- package/dist/plugins/tree-processor/plugin.js +19 -16
- package/dist/plugins/tree-processor/plugin.js.map +1 -0
- package/dist/types.d.ts +2 -2
- package/dist/types.js +1 -1
- package/package.json +9 -4
- package/.turbo/turbo-build.log +0 -4
- package/compiler.test.ts +0 -117
- package/compiler.ts +0 -124
- package/mod.ts +0 -3
- package/plugins/heading-id/plugin.ts +0 -42
- package/plugins/mod.ts +0 -4
- package/plugins/remove-imports-exports.ts +0 -18
- package/plugins/syntax-highlight/code-meta.test.ts +0 -22
- package/plugins/syntax-highlight/code-meta.ts +0 -76
- package/plugins/syntax-highlight/plugin.ts +0 -266
- package/plugins/tree-processor/plugin.ts +0 -54
- package/tsconfig.json +0 -18
- package/types.ts +0 -3
package/compiler.ts
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
compile as compileMdxJs,
|
|
3
|
-
type CompileOptions as MdxJsCompileOptions,
|
|
4
|
-
} from "@mdx-js/mdx";
|
|
5
|
-
import remarkFrontmatterPlugin from "remark-frontmatter";
|
|
6
|
-
import remarkGfmPlugin from "remark-gfm";
|
|
7
|
-
import { VFile } from "vfile";
|
|
8
|
-
import { matter } from "vfile-matter";
|
|
9
|
-
import type { Pluggable, PluggableList, Plugin } from "unified";
|
|
10
|
-
import type { z } from "zod";
|
|
11
|
-
|
|
12
|
-
import type { HastNode } from "./types";
|
|
13
|
-
|
|
14
|
-
export { remarkFrontmatterPlugin, remarkGfmPlugin };
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Represents the source content for an MDX document.
|
|
18
|
-
*/
|
|
19
|
-
export type MdxSource = string | Uint8Array;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Options for compiling an MDX document.
|
|
23
|
-
*/
|
|
24
|
-
export interface MdxCompileOptions {
|
|
25
|
-
frontmatterOnly?: boolean | undefined;
|
|
26
|
-
mdxOptions?: MdxJsCompileOptions;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* The result of compiling an MDX document.
|
|
31
|
-
* Compiled content is optional, because sometimes we only need the frontmatter.
|
|
32
|
-
*/
|
|
33
|
-
export interface MdxArtifact<TFrontmatter = Record<string, unknown>> {
|
|
34
|
-
compiled?: string | undefined;
|
|
35
|
-
frontmatter: TFrontmatter;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* A reusable compiler for compiling MDX.
|
|
40
|
-
*/
|
|
41
|
-
export class MdxCompiler {
|
|
42
|
-
private readonly mdxOptions: MdxJsCompileOptions;
|
|
43
|
-
private readonly remarkPlugins: PluggableList;
|
|
44
|
-
private readonly rehypePlugins: PluggableList;
|
|
45
|
-
|
|
46
|
-
public constructor(mdxOptions: MdxJsCompileOptions = {}) {
|
|
47
|
-
this.mdxOptions = mdxOptions;
|
|
48
|
-
this.remarkPlugins = [remarkFrontmatterPlugin, remarkGfmPlugin];
|
|
49
|
-
this.rehypePlugins = [];
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
public withRemarkPlugin<TOptions>(
|
|
53
|
-
plugin: Plugin<[TOptions?], HastNode, HastNode>,
|
|
54
|
-
options?: TOptions,
|
|
55
|
-
): MdxCompiler {
|
|
56
|
-
if (options === undefined) {
|
|
57
|
-
this.remarkPlugins.push(plugin as Pluggable);
|
|
58
|
-
} else {
|
|
59
|
-
this.remarkPlugins.push([plugin, options] as Pluggable);
|
|
60
|
-
}
|
|
61
|
-
return this;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
public withRehypePlugin<TOptions>(
|
|
65
|
-
plugin: Plugin<[TOptions?], HastNode, HastNode>,
|
|
66
|
-
options?: TOptions,
|
|
67
|
-
): MdxCompiler {
|
|
68
|
-
if (options === undefined) {
|
|
69
|
-
this.rehypePlugins.push(plugin as Pluggable);
|
|
70
|
-
} else {
|
|
71
|
-
this.rehypePlugins.push([plugin, options] as Pluggable);
|
|
72
|
-
}
|
|
73
|
-
return this;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
public async compile<TFrontmatterSchema extends z.ZodSchema>(
|
|
77
|
-
source: MdxSource,
|
|
78
|
-
{ frontmatterOnly = false, mdxOptions }: MdxCompileOptions = {},
|
|
79
|
-
frontmatterSchema?: TFrontmatterSchema,
|
|
80
|
-
): Promise<MdxArtifact<z.output<TFrontmatterSchema>>> {
|
|
81
|
-
const vfile = new VFile({
|
|
82
|
-
value: source,
|
|
83
|
-
// Needed so vfile doesn't need to access fs.
|
|
84
|
-
cwd: "/",
|
|
85
|
-
path: "/",
|
|
86
|
-
message: {},
|
|
87
|
-
messages: {},
|
|
88
|
-
});
|
|
89
|
-
matter(vfile, { strip: true });
|
|
90
|
-
|
|
91
|
-
const remarkPlugins = [
|
|
92
|
-
...this.remarkPlugins,
|
|
93
|
-
...(this.mdxOptions?.remarkPlugins ?? []),
|
|
94
|
-
...(mdxOptions?.remarkPlugins ?? []),
|
|
95
|
-
];
|
|
96
|
-
const rehypePlugins = [
|
|
97
|
-
...this.rehypePlugins,
|
|
98
|
-
...(this.mdxOptions?.rehypePlugins ?? []),
|
|
99
|
-
...(mdxOptions?.rehypePlugins ?? []),
|
|
100
|
-
];
|
|
101
|
-
|
|
102
|
-
let compiled: string | undefined;
|
|
103
|
-
if (!frontmatterOnly) {
|
|
104
|
-
const compiledFile = await compileMdxJs(vfile, {
|
|
105
|
-
outputFormat: "function-body",
|
|
106
|
-
...(this.mdxOptions ?? {}),
|
|
107
|
-
...(mdxOptions ?? {}),
|
|
108
|
-
remarkPlugins,
|
|
109
|
-
rehypePlugins,
|
|
110
|
-
});
|
|
111
|
-
compiled = compiledFile.value.toString();
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const frontmatter =
|
|
115
|
-
frontmatterSchema === undefined
|
|
116
|
-
? (vfile.data.matter ?? {})
|
|
117
|
-
: frontmatterSchema.parse(vfile.data.matter ?? {});
|
|
118
|
-
|
|
119
|
-
return {
|
|
120
|
-
compiled,
|
|
121
|
-
frontmatter: frontmatter as z.output<TFrontmatterSchema>,
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
}
|
package/mod.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import GithubSlugger from "github-slugger";
|
|
2
|
-
import { toString as hastToString } from "hast-util-to-string";
|
|
3
|
-
import { visit } from "unist-util-visit";
|
|
4
|
-
import type { Plugin } from "unified";
|
|
5
|
-
|
|
6
|
-
import type { HastElement, HastNode } from "../../types";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Options for {@linkcode headingIdPlugin}.
|
|
10
|
-
*/
|
|
11
|
-
export interface HeadingIdPluginOptions {
|
|
12
|
-
/** Prefix for title slugs. */
|
|
13
|
-
prefix?: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* A rehype plugin that adds IDs to heading tags.
|
|
18
|
-
*/
|
|
19
|
-
export const headingIdPlugin: Plugin<
|
|
20
|
-
[HeadingIdPluginOptions?],
|
|
21
|
-
HastNode,
|
|
22
|
-
HastNode
|
|
23
|
-
> = ({ prefix } = {}) => {
|
|
24
|
-
const slugger = new GithubSlugger();
|
|
25
|
-
return (tree) => {
|
|
26
|
-
slugger.reset();
|
|
27
|
-
visit(
|
|
28
|
-
tree,
|
|
29
|
-
"element",
|
|
30
|
-
function visitor(
|
|
31
|
-
node: HastElement,
|
|
32
|
-
index: number,
|
|
33
|
-
parent: HastElement,
|
|
34
|
-
): void {
|
|
35
|
-
if (!parent || index === null || !node.tagName.startsWith("h")) return;
|
|
36
|
-
const id = (prefix ?? "") + slugger.slug(hastToString(node));
|
|
37
|
-
node.properties.id = id;
|
|
38
|
-
},
|
|
39
|
-
);
|
|
40
|
-
return tree;
|
|
41
|
-
};
|
|
42
|
-
};
|
package/plugins/mod.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { remove } from "unist-util-remove";
|
|
2
|
-
import type { Plugin } from "unified";
|
|
3
|
-
|
|
4
|
-
import type { HastNode } from "../types";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A remark plugin which removes all import and export statements.
|
|
8
|
-
*/
|
|
9
|
-
export const removeImportsExportsPlugin: Plugin<
|
|
10
|
-
[unknown],
|
|
11
|
-
HastNode,
|
|
12
|
-
HastNode
|
|
13
|
-
> = () => {
|
|
14
|
-
return (tree: HastNode) => {
|
|
15
|
-
remove(tree, "mdxjsEsm");
|
|
16
|
-
return tree;
|
|
17
|
-
};
|
|
18
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest";
|
|
2
|
-
|
|
3
|
-
import { extractCodeMeta } from "./code-meta";
|
|
4
|
-
|
|
5
|
-
test("code meta - extract", () => {
|
|
6
|
-
expect(
|
|
7
|
-
extractCodeMeta({
|
|
8
|
-
type: "element",
|
|
9
|
-
tagName: "pre",
|
|
10
|
-
properties: {},
|
|
11
|
-
children: [],
|
|
12
|
-
data: {
|
|
13
|
-
meta: '{"highlight": "3..5", "showLineNumbers":true, "fileName":"test"}',
|
|
14
|
-
},
|
|
15
|
-
}),
|
|
16
|
-
{
|
|
17
|
-
highlight: { lineRange: [{ from: 3, to: 4 }] },
|
|
18
|
-
showLineNumbers: { lineRange: [] },
|
|
19
|
-
fileName: "test",
|
|
20
|
-
},
|
|
21
|
-
);
|
|
22
|
-
});
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { type NumericRange, parseNumericRange } from "@temelj/iterator";
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
|
|
4
|
-
import type { HastElement } from "../../types";
|
|
5
|
-
|
|
6
|
-
export interface MdxCodeMeta {
|
|
7
|
-
highlight?: MdxCodeMetaHighlightLine;
|
|
8
|
-
showLineNumbers?: MdxCodeMetaLineNumbers;
|
|
9
|
-
commandLine?: MdxCodeMetaCommandLine;
|
|
10
|
-
fileName?: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export type MdxCodeMetaHighlightLine = {
|
|
14
|
-
lineRange: NumericRange;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export type MdxCodeMetaLineNumbers = {
|
|
18
|
-
lineRange: NumericRange;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export type MdxCodeMetaCommandLine = {
|
|
22
|
-
commandRange: NumericRange;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export function extractCodeMeta(node: HastElement): MdxCodeMeta {
|
|
26
|
-
const meta = node?.data?.meta ?? node?.properties?.metastring ?? "";
|
|
27
|
-
return parseMetaString(meta as string);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const metaSchema = z.object({
|
|
31
|
-
highlight: z.optional(z.string()),
|
|
32
|
-
showLineNumbers: z.optional(z.union([z.boolean(), z.string()])),
|
|
33
|
-
commandLine: z.optional(z.union([z.boolean(), z.string()])),
|
|
34
|
-
fileName: z.optional(z.string()),
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
function parseMetaString(meta: string): MdxCodeMeta {
|
|
38
|
-
let raw: unknown;
|
|
39
|
-
try {
|
|
40
|
-
raw = JSON.parse(meta.trim());
|
|
41
|
-
} catch {
|
|
42
|
-
return {};
|
|
43
|
-
}
|
|
44
|
-
const parsed = metaSchema.parse(raw);
|
|
45
|
-
const options: MdxCodeMeta = {};
|
|
46
|
-
|
|
47
|
-
if (parsed.highlight !== undefined) {
|
|
48
|
-
options.highlight = { lineRange: parseNumericRange(parsed.highlight) };
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (parsed.showLineNumbers !== undefined) {
|
|
52
|
-
if (typeof parsed.showLineNumbers === "string") {
|
|
53
|
-
options.showLineNumbers = {
|
|
54
|
-
lineRange: parseNumericRange(parsed.showLineNumbers),
|
|
55
|
-
};
|
|
56
|
-
} else if (parsed.showLineNumbers) {
|
|
57
|
-
options.showLineNumbers = { lineRange: [] };
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (parsed.commandLine !== undefined) {
|
|
62
|
-
if (typeof parsed.commandLine === "string") {
|
|
63
|
-
options.commandLine = {
|
|
64
|
-
commandRange: parseNumericRange(parsed.commandLine),
|
|
65
|
-
};
|
|
66
|
-
} else if (parsed.commandLine) {
|
|
67
|
-
options.commandLine = { commandRange: [] };
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (parsed.fileName !== undefined) {
|
|
72
|
-
options.fileName = parsed.fileName.trim();
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return options;
|
|
76
|
-
}
|
|
@@ -1,266 +0,0 @@
|
|
|
1
|
-
import { toString as hastToString } from "hast-util-to-string";
|
|
2
|
-
import {
|
|
3
|
-
type BundledLanguage,
|
|
4
|
-
type BundledTheme,
|
|
5
|
-
codeToHast,
|
|
6
|
-
type CodeToHastOptions,
|
|
7
|
-
ShikiError,
|
|
8
|
-
} from "shiki";
|
|
9
|
-
import { visit } from "unist-util-visit";
|
|
10
|
-
import {
|
|
11
|
-
transformerNotationHighlight,
|
|
12
|
-
type TransformerNotationHighlightOptions,
|
|
13
|
-
} from "@shikijs/transformers";
|
|
14
|
-
import { numericRangeContains } from "@temelj/iterator";
|
|
15
|
-
import type { Plugin } from "unified";
|
|
16
|
-
import type { Properties } from "hast";
|
|
17
|
-
|
|
18
|
-
import type { HastElement, HastNode } from "../../types";
|
|
19
|
-
|
|
20
|
-
import { extractCodeMeta } from "./code-meta";
|
|
21
|
-
|
|
22
|
-
type ShikiHastOptions = Partial<
|
|
23
|
-
CodeToHastOptions<BundledLanguage, BundledTheme>
|
|
24
|
-
>;
|
|
25
|
-
|
|
26
|
-
type SyntaxDataAttribute = "source-code" | "language" | "line-count";
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Options for {@linkcode syntaxHighlightPlugin}.
|
|
30
|
-
*/
|
|
31
|
-
export interface SyntaxHighlightPluginOptions {
|
|
32
|
-
/**
|
|
33
|
-
* Include these data attributes on the `<pre>` element.
|
|
34
|
-
* These might be useful to have when building custom code components.
|
|
35
|
-
*/
|
|
36
|
-
includeDataAttributes?: SyntaxDataAttribute[];
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Options for the highlight transformer.
|
|
40
|
-
*/
|
|
41
|
-
highlight?: {
|
|
42
|
-
transformer?: TransformerNotationHighlightOptions;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Options for line numbers.
|
|
47
|
-
*/
|
|
48
|
-
lineNumbers?: {
|
|
49
|
-
/**
|
|
50
|
-
* The name of the variable that contains the line number width.
|
|
51
|
-
* This is used for padding the line numbers so that they align.
|
|
52
|
-
*/
|
|
53
|
-
widthVariableName?: string;
|
|
54
|
-
/**
|
|
55
|
-
* A class name to be added to the line numbers.
|
|
56
|
-
*/
|
|
57
|
-
className?: string;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Options for command line.
|
|
62
|
-
*/
|
|
63
|
-
commandLine?: {
|
|
64
|
-
/**
|
|
65
|
-
* A class name to be added to the prompt line.
|
|
66
|
-
*/
|
|
67
|
-
className?: string;
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Extra options for the Shiki hast transformer.
|
|
72
|
-
*/
|
|
73
|
-
shikiHastOptions?: ShikiHastOptions;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* A plugin that highlights code blocks using Shiki.
|
|
78
|
-
*/
|
|
79
|
-
export const syntaxHighlightPlugin: Plugin<
|
|
80
|
-
[SyntaxHighlightPluginOptions?],
|
|
81
|
-
HastNode,
|
|
82
|
-
HastNode
|
|
83
|
-
> = (options: SyntaxHighlightPluginOptions = {}) => {
|
|
84
|
-
return async (tree) => {
|
|
85
|
-
const promises: Promise<void>[] = [];
|
|
86
|
-
|
|
87
|
-
async function visitor(
|
|
88
|
-
node: HastElement,
|
|
89
|
-
index: number,
|
|
90
|
-
parent: HastElement,
|
|
91
|
-
): Promise<void> {
|
|
92
|
-
if (!parent || index === null || node.tagName !== "pre") {
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const codeNode = node.children[0];
|
|
97
|
-
if (
|
|
98
|
-
!codeNode ||
|
|
99
|
-
codeNode.type !== "element" ||
|
|
100
|
-
codeNode.tagName !== "code" ||
|
|
101
|
-
!codeNode.properties
|
|
102
|
-
) {
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const lang = getNodeLanguage(codeNode);
|
|
107
|
-
if (typeof lang !== "string") {
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const meta = extractCodeMeta(codeNode);
|
|
112
|
-
|
|
113
|
-
const sourceCode = hastToString(codeNode).trim();
|
|
114
|
-
|
|
115
|
-
const hastOptions: CodeToHastOptions<BundledLanguage, BundledTheme> = {
|
|
116
|
-
tokenizeTimeLimit: 10_000,
|
|
117
|
-
lang,
|
|
118
|
-
themes: {
|
|
119
|
-
light: "github-light-default",
|
|
120
|
-
dark: "github-dark-default",
|
|
121
|
-
},
|
|
122
|
-
...(options.shikiHastOptions ?? {}),
|
|
123
|
-
};
|
|
124
|
-
if (hastOptions.transformers === undefined) {
|
|
125
|
-
hastOptions.transformers = [];
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (options.highlight) {
|
|
129
|
-
hastOptions.transformers.push(
|
|
130
|
-
transformerNotationHighlight({
|
|
131
|
-
...(options.highlight.transformer ?? {}),
|
|
132
|
-
matchAlgorithm: "v3",
|
|
133
|
-
}),
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
let lineCount = 0;
|
|
138
|
-
hastOptions.transformers.push({
|
|
139
|
-
code(node): void {
|
|
140
|
-
let lineIndex = 0;
|
|
141
|
-
for (const line of node.children.filter(
|
|
142
|
-
(node: HastNode) => node.type === "element",
|
|
143
|
-
) as HastElement[]) {
|
|
144
|
-
line.properties["data-line"] = lineIndex + 1;
|
|
145
|
-
|
|
146
|
-
if (options.highlight) {
|
|
147
|
-
if (
|
|
148
|
-
meta.highlight &&
|
|
149
|
-
numericRangeContains(meta.highlight.lineRange, lineIndex)
|
|
150
|
-
) {
|
|
151
|
-
this.addClassToHast(
|
|
152
|
-
line,
|
|
153
|
-
options.highlight.transformer?.classActiveLine ??
|
|
154
|
-
"highlighted",
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if (
|
|
160
|
-
options.lineNumbers &&
|
|
161
|
-
meta.showLineNumbers &&
|
|
162
|
-
(meta.showLineNumbers.lineRange.length === 0 ||
|
|
163
|
-
numericRangeContains(meta.showLineNumbers.lineRange, lineIndex))
|
|
164
|
-
) {
|
|
165
|
-
this.addClassToHast(
|
|
166
|
-
line,
|
|
167
|
-
options.lineNumbers.className ?? "line-number",
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
if (
|
|
172
|
-
options.commandLine &&
|
|
173
|
-
meta.commandLine &&
|
|
174
|
-
(meta.commandLine.commandRange.length === 0 ||
|
|
175
|
-
numericRangeContains(meta.commandLine.commandRange, lineIndex))
|
|
176
|
-
) {
|
|
177
|
-
this.addClassToHast(
|
|
178
|
-
line,
|
|
179
|
-
options.commandLine.className ?? "line-command",
|
|
180
|
-
);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
lineIndex++;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
lineCount = lineIndex;
|
|
187
|
-
|
|
188
|
-
if (meta.showLineNumbers) {
|
|
189
|
-
if (typeof node.properties.style !== "string") {
|
|
190
|
-
node.properties.style = "";
|
|
191
|
-
}
|
|
192
|
-
node.properties.style += `--${
|
|
193
|
-
options.lineNumbers?.widthVariableName ?? "line-number-width"
|
|
194
|
-
}:${1 + Math.floor(Math.log10(this.lines.length))}ch;`;
|
|
195
|
-
}
|
|
196
|
-
},
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
try {
|
|
200
|
-
const hast = await codeToHast(sourceCode, hastOptions);
|
|
201
|
-
|
|
202
|
-
// Insert highlighted code into parent
|
|
203
|
-
const hastPre = hast.children[0] as HastElement;
|
|
204
|
-
|
|
205
|
-
const properties: Properties = {
|
|
206
|
-
...hastPre.properties,
|
|
207
|
-
dataFileName: meta.fileName,
|
|
208
|
-
};
|
|
209
|
-
if (options.includeDataAttributes) {
|
|
210
|
-
if (options.includeDataAttributes.includes("language")) {
|
|
211
|
-
properties.dataLanguage = lang;
|
|
212
|
-
}
|
|
213
|
-
if (options.includeDataAttributes.includes("source-code")) {
|
|
214
|
-
properties.dataSourceCode = sourceCode;
|
|
215
|
-
}
|
|
216
|
-
if (options.includeDataAttributes.includes("line-count")) {
|
|
217
|
-
properties.dataLineCount = lineCount;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
parent.children.splice(index, 1, {
|
|
222
|
-
...hastPre,
|
|
223
|
-
properties,
|
|
224
|
-
});
|
|
225
|
-
} catch (error) {
|
|
226
|
-
if (error instanceof ShikiError) {
|
|
227
|
-
if (
|
|
228
|
-
error.message.includes(
|
|
229
|
-
"Language `math` is not included in this bundle",
|
|
230
|
-
)
|
|
231
|
-
) {
|
|
232
|
-
// Skip if the block is math block.
|
|
233
|
-
return;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
throw error;
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
visit(
|
|
241
|
-
tree,
|
|
242
|
-
"element",
|
|
243
|
-
(node: HastElement, index: number, parent: HastElement) => {
|
|
244
|
-
promises.push(visitor(node, index, parent));
|
|
245
|
-
},
|
|
246
|
-
);
|
|
247
|
-
|
|
248
|
-
await Promise.all(promises);
|
|
249
|
-
|
|
250
|
-
return tree;
|
|
251
|
-
};
|
|
252
|
-
};
|
|
253
|
-
|
|
254
|
-
function getNodeLanguage(node: HastElement): string | undefined {
|
|
255
|
-
if (!Array.isArray(node.properties.className)) {
|
|
256
|
-
return undefined;
|
|
257
|
-
}
|
|
258
|
-
const prefix = "language-";
|
|
259
|
-
const language = node.properties.className.find(
|
|
260
|
-
(name) => typeof name === "string" && name.startsWith(prefix),
|
|
261
|
-
) as string | undefined;
|
|
262
|
-
if (typeof language === "string") {
|
|
263
|
-
return language.slice(prefix.length);
|
|
264
|
-
}
|
|
265
|
-
return language;
|
|
266
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { visit } from "unist-util-visit";
|
|
2
|
-
import type { Plugin } from "unified";
|
|
3
|
-
|
|
4
|
-
import type { HastElement, HastNode } from "../../types";
|
|
5
|
-
|
|
6
|
-
export type MdxTreeProcessor = (
|
|
7
|
-
node: HastElement,
|
|
8
|
-
index: number,
|
|
9
|
-
parent: HastElement,
|
|
10
|
-
) => void | Promise<void>;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Options for {@linkcode treeProcessorPlugin}.
|
|
14
|
-
*/
|
|
15
|
-
type TreeProcessorPluginOptions = {
|
|
16
|
-
/** The processor. */
|
|
17
|
-
process: MdxTreeProcessor;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* A plugin that allows to process MDX tree nodes.
|
|
22
|
-
* It can be async or sync.
|
|
23
|
-
*/
|
|
24
|
-
export const treeProcessorPlugin: Plugin<
|
|
25
|
-
[TreeProcessorPluginOptions?],
|
|
26
|
-
HastNode,
|
|
27
|
-
HastNode
|
|
28
|
-
> = ({ process } = { process: () => {} }) => {
|
|
29
|
-
return async (tree) => {
|
|
30
|
-
const promises: Promise<void>[] = [];
|
|
31
|
-
|
|
32
|
-
visit(
|
|
33
|
-
tree,
|
|
34
|
-
"element",
|
|
35
|
-
function visitor(
|
|
36
|
-
node: HastElement,
|
|
37
|
-
index: number,
|
|
38
|
-
parent: HastElement,
|
|
39
|
-
): void {
|
|
40
|
-
const p = process(node, index, parent);
|
|
41
|
-
if (
|
|
42
|
-
p instanceof Promise ||
|
|
43
|
-
Object.prototype.toString.call(p) === "[object Promise]"
|
|
44
|
-
) {
|
|
45
|
-
promises.push(p as Promise<void>);
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
await Promise.allSettled(promises);
|
|
51
|
-
|
|
52
|
-
return tree;
|
|
53
|
-
};
|
|
54
|
-
};
|
package/tsconfig.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2022",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"moduleResolution": "bundler",
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"outDir": "./dist",
|
|
8
|
-
"strict": true,
|
|
9
|
-
"esModuleInterop": true,
|
|
10
|
-
"skipLibCheck": true,
|
|
11
|
-
"forceConsistentCasingInFileNames": true,
|
|
12
|
-
"paths": {
|
|
13
|
-
"@temelj/iterator": ["../iterator/src/mod"]
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
"include": ["./**/*"],
|
|
17
|
-
"exclude": ["node_modules", "dist", "**/*.test.ts"]
|
|
18
|
-
}
|
package/types.ts
DELETED