@temelj/mdx 0.8.0 → 0.8.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/dist/compiler.d.ts +17 -15
- package/dist/compiler.js +67 -68
- package/dist/mod.d.ts +13 -3
- package/dist/mod.js +1 -1
- package/dist/plugins/heading-id/plugin.d.ts +8 -5
- package/dist/plugins/heading-id/plugin.js +18 -15
- package/dist/plugins/mod.d.ts +8 -4
- package/dist/plugins/remove-imports-exports.d.ts +7 -4
- package/dist/plugins/remove-imports-exports.js +8 -8
- package/dist/plugins/syntax-highlight/code-meta.d.ts +10 -7
- package/dist/plugins/syntax-highlight/code-meta.js +41 -41
- package/dist/plugins/syntax-highlight/plugin.d.ts +10 -8
- package/dist/plugins/syntax-highlight/plugin.js +134 -122
- package/dist/plugins/tree-processor/plugin.d.ts +8 -6
- package/dist/plugins/tree-processor/plugin.js +20 -17
- package/dist/types.d.ts +1 -2
- package/dist/types.js +0 -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/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