@temelj/mdx 0.10.2 → 0.11.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/README.md CHANGED
@@ -116,5 +116,4 @@ assertEquals(artifact.frontmatter, {
116
116
 
117
117
  ## About
118
118
 
119
- This package is part of [Temelj](https://github.com/tinrab/temelj) - a big
120
- standard library for TypeScript.
119
+ This package is part of [Temelj](https://github.com/tinrab/temelj) - a core library for TypeScript.
@@ -17,6 +17,31 @@ interface MdxCompileOptions {
17
17
  frontmatterOnly?: boolean | undefined;
18
18
  mdxOptions?: CompileOptions;
19
19
  }
20
+ interface MdxMessagePoint {
21
+ line?: number | undefined;
22
+ column?: number | undefined;
23
+ offset?: number | undefined;
24
+ }
25
+ interface MdxMessagePlace {
26
+ start?: MdxMessagePoint | undefined;
27
+ end?: MdxMessagePoint | undefined;
28
+ }
29
+ interface MdxMessage {
30
+ message: string;
31
+ reason: string;
32
+ source?: string | undefined;
33
+ ruleId?: string | undefined;
34
+ fatal?: boolean | undefined;
35
+ line?: number | undefined;
36
+ column?: number | undefined;
37
+ place?: MdxMessagePlace | undefined;
38
+ sourceLine?: string | undefined;
39
+ snippet?: string | undefined;
40
+ cause?: {
41
+ name?: string | undefined;
42
+ message: string;
43
+ };
44
+ }
20
45
  /**
21
46
  * The result of compiling an MDX document.
22
47
  * Compiled content is optional, because sometimes we only need the frontmatter.
@@ -24,6 +49,29 @@ interface MdxCompileOptions {
24
49
  interface MdxArtifact<TFrontmatter = Record<string, unknown>> {
25
50
  compiled?: string | undefined;
26
51
  frontmatter: TFrontmatter;
52
+ messages: MdxMessage[];
53
+ }
54
+ declare class MdxCompileError extends Error {
55
+ readonly reason: string;
56
+ readonly source?: string | undefined;
57
+ readonly ruleId?: string | undefined;
58
+ readonly line?: number | undefined;
59
+ readonly column?: number | undefined;
60
+ readonly place?: MdxMessagePlace | undefined;
61
+ readonly sourceLine?: string | undefined;
62
+ readonly snippet?: string | undefined;
63
+ readonly diagnostics: MdxMessage[];
64
+ constructor(reason: string, options?: {
65
+ source?: string | undefined;
66
+ ruleId?: string | undefined;
67
+ line?: number | undefined;
68
+ column?: number | undefined;
69
+ place?: MdxMessagePlace | undefined;
70
+ sourceLine?: string | undefined;
71
+ snippet?: string | undefined;
72
+ diagnostics?: MdxMessage[] | undefined;
73
+ cause?: unknown;
74
+ });
27
75
  }
28
76
  /**
29
77
  * A reusable compiler for compiling MDX.
@@ -41,5 +89,5 @@ declare class MdxCompiler {
41
89
  }?: MdxCompileOptions, frontmatterSchema?: TFrontmatterSchema): Promise<MdxArtifact<z.output<TFrontmatterSchema>>>;
42
90
  }
43
91
  //#endregion
44
- export { MdxArtifact, MdxCompileOptions, MdxCompiler, MdxSource, remarkFrontmatterPlugin, remarkGfmPlugin };
92
+ export { MdxArtifact, MdxCompileError, MdxCompileOptions, MdxCompiler, MdxMessage, MdxMessagePlace, MdxMessagePoint, MdxSource, remarkFrontmatterPlugin, remarkGfmPlugin };
45
93
  //# sourceMappingURL=compiler.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"compiler.d.mts","names":[],"sources":["../src/compiler.ts"],"mappings":";;;;;;;;;AAkBA;;KAAY,SAAA,YAAqB,UAAA;;;AAKjC;UAAiB,iBAAA;EACf,eAAA;EACA,UAAA,GAAa,cAAA;AAAA;;;;;UAOE,WAAA,gBAA2B,MAAA;EAC1C,QAAA;EACA,WAAA,EAAa,YAAA;AAAA;;;;cAMF,WAAA;EAAA,iBACM,UAAA;EAAA,iBACA,aAAA;EAAA,iBACA,aAAA;cAEE,UAAA,GAAY,cAAA;EAMxB,gBAAA,UAAA,CACL,MAAA,EAAQ,MAAA,EAAQ,QAAA,IAAY,IAAA,EAAU,IAAA,GACtC,OAAA,GAAU,QAAA,GACT,WAAA;EASI,gBAAA,UAAA,CACL,MAAA,EAAQ,MAAA,EAAQ,QAAA,IAAY,IAAA,EAAU,IAAA,GACtC,OAAA,GAAU,QAAA,GACT,WAAA;EASU,O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AA;EAMxB,gBAAA,UAAA,CACL,MAAA,EAAQ,MAAA,EAAQ,QAAA,IAAY,IAAA,EAAU,IAAA,GACtC,OAAA,GAAU,QAAA,GACT,WAAA;EASI,gBAAA,UAAA,CACL,MAAA,EAAQ,MAAA,EAAQ,QAAA,IAAY,IAAA,EAAU,IAAA,GACtC,OAAA,GAAU,QAAA,GACT,WAAA;EASU,OAAA,4BAAmC,CAAA,CAAE,SAAA,CAAA,CAChD,MAAA,EAAQ,SAAA;IACN,eAAA;IAAyB;EAAA,IAAc,iBAAA,EACzC,iBAAA,GAAoB,kBAAA,GACnB,OAAA,CAAQ,WAAA,CAAY,CAAA,CAAE,MAAA,CAAO,kBAAA;AAAA"}
package/dist/compiler.mjs CHANGED
@@ -3,8 +3,38 @@ import remarkFrontmatterPlugin from "remark-frontmatter";
3
3
  import remarkGfmPlugin from "remark-gfm";
4
4
  import { VFile } from "vfile";
5
5
  import { matter } from "vfile-matter";
6
-
7
6
  //#region src/compiler.ts
7
+ var MdxCompileError = class extends Error {
8
+ reason;
9
+ source;
10
+ ruleId;
11
+ line;
12
+ column;
13
+ place;
14
+ sourceLine;
15
+ snippet;
16
+ diagnostics;
17
+ constructor(reason, options = {}) {
18
+ super(formatDiagnosticMessage(reason, {
19
+ source: options.source,
20
+ ruleId: options.ruleId,
21
+ line: options.line,
22
+ column: options.column,
23
+ sourceLine: options.sourceLine,
24
+ snippet: options.snippet
25
+ }), options.cause === void 0 ? void 0 : { cause: options.cause });
26
+ this.name = "MdxCompileError";
27
+ this.reason = reason;
28
+ this.source = options.source;
29
+ this.ruleId = options.ruleId;
30
+ this.line = options.line;
31
+ this.column = options.column;
32
+ this.place = options.place;
33
+ this.sourceLine = options.sourceLine;
34
+ this.snippet = options.snippet;
35
+ this.diagnostics = options.diagnostics ?? [];
36
+ }
37
+ };
8
38
  /**
9
39
  * A reusable compiler for compiling MDX.
10
40
  */
@@ -28,12 +58,11 @@ var MdxCompiler = class {
28
58
  return this;
29
59
  }
30
60
  async compile(source, { frontmatterOnly = false, mdxOptions } = {}, frontmatterSchema) {
61
+ const sourceText = toSourceText(source);
31
62
  const vfile = new VFile({
32
63
  value: source,
33
64
  cwd: "/",
34
- path: "/",
35
- message: {},
36
- messages: {}
65
+ path: "/source.mdx"
37
66
  });
38
67
  matter(vfile, { strip: true });
39
68
  const remarkPlugins = [
@@ -47,21 +76,184 @@ var MdxCompiler = class {
47
76
  ...mdxOptions?.rehypePlugins ?? []
48
77
  ];
49
78
  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();
79
+ if (!frontmatterOnly) try {
80
+ compiled = (await compile(vfile, {
81
+ outputFormat: "function-body",
82
+ ...this.mdxOptions ?? {},
83
+ ...mdxOptions ?? {},
84
+ remarkPlugins,
85
+ rehypePlugins
86
+ })).value.toString();
87
+ } catch (error) {
88
+ throw toMdxCompileError(error, sourceText, vfile.messages);
89
+ }
57
90
  const frontmatter = frontmatterSchema === void 0 ? vfile.data.matter ?? {} : frontmatterSchema.parse(vfile.data.matter ?? {});
58
91
  return {
59
92
  compiled,
60
- frontmatter
93
+ frontmatter,
94
+ messages: normalizeMessages(vfile.messages, sourceText)
61
95
  };
62
96
  }
63
97
  };
64
-
98
+ function toMdxCompileError(error, sourceText, diagnostics) {
99
+ const details = getDiagnosticDetails(sourceText, getErrorPlace(error), error);
100
+ return new MdxCompileError(getErrorReason(error), {
101
+ source: getErrorField(error, "source"),
102
+ ruleId: getErrorField(error, "ruleId"),
103
+ line: details.line,
104
+ column: details.column,
105
+ place: details.place,
106
+ sourceLine: details.sourceLine,
107
+ snippet: details.snippet,
108
+ diagnostics: normalizeMessages(diagnostics, sourceText),
109
+ cause: error
110
+ });
111
+ }
112
+ function normalizeMessages(messages, sourceText) {
113
+ return messages.map((message) => {
114
+ const details = getDiagnosticDetails(sourceText, toMessagePlace(message), message);
115
+ const reason = getErrorReason(message);
116
+ const source = getErrorField(message, "source");
117
+ const ruleId = getErrorField(message, "ruleId");
118
+ const cause = getErrorCause(message);
119
+ return {
120
+ message: formatDiagnosticMessage(reason, {
121
+ source,
122
+ ruleId,
123
+ line: details.line,
124
+ column: details.column,
125
+ sourceLine: details.sourceLine,
126
+ snippet: details.snippet,
127
+ causeMessage: cause?.message
128
+ }),
129
+ reason,
130
+ source,
131
+ ruleId,
132
+ fatal: typeof message.fatal === "boolean" ? message.fatal : void 0,
133
+ line: details.line,
134
+ column: details.column,
135
+ place: details.place,
136
+ sourceLine: details.sourceLine,
137
+ snippet: details.snippet,
138
+ cause
139
+ };
140
+ });
141
+ }
142
+ function formatDiagnosticMessage(reason, options) {
143
+ const lines = [[
144
+ reason,
145
+ options.source === void 0 ? void 0 : options.ruleId === void 0 ? options.source : `${options.source}:${options.ruleId}`,
146
+ options.line === void 0 ? void 0 : options.column === void 0 ? `at ${options.line}` : `at ${options.line}:${options.column}`
147
+ ].filter(Boolean).join(" ")];
148
+ if (options.snippet !== void 0) lines.push(`Source: ${options.snippet}`);
149
+ if (options.sourceLine !== void 0 && options.sourceLine !== options.snippet) lines.push(`Line: ${options.sourceLine}`);
150
+ if (options.causeMessage !== void 0 && options.causeMessage !== "" && options.causeMessage !== reason) lines.push(`Cause: ${options.causeMessage}`);
151
+ return lines.join("\n");
152
+ }
153
+ function getDiagnosticDetails(sourceText, place, error) {
154
+ const line = place?.start?.line ?? getErrorNumberField(error, "line");
155
+ const column = place?.start?.column ?? getErrorNumberField(error, "column");
156
+ const normalizedPlace = line === void 0 && column === void 0 && place === void 0 ? void 0 : {
157
+ start: {
158
+ line,
159
+ column,
160
+ offset: place?.start?.offset
161
+ },
162
+ end: place?.end === void 0 ? void 0 : {
163
+ line: place.end.line,
164
+ column: place.end.column,
165
+ offset: place.end.offset
166
+ }
167
+ };
168
+ return {
169
+ line,
170
+ column,
171
+ place: normalizedPlace,
172
+ sourceLine: getSourceLine(sourceText, line),
173
+ snippet: getSourceSnippet(sourceText, normalizedPlace)
174
+ };
175
+ }
176
+ function getSourceLine(sourceText, line) {
177
+ if (line === void 0 || line < 1) return;
178
+ return sourceText.split(/\r?\n/u)[line - 1];
179
+ }
180
+ function getSourceSnippet(sourceText, place) {
181
+ if (place?.start?.offset !== void 0 && place.end?.offset !== void 0) return sourceText.slice(place.start.offset, place.end.offset) || void 0;
182
+ if (place?.start?.line === void 0 || place.start.column === void 0) return;
183
+ const lines = sourceText.split(/\r?\n/u);
184
+ const startLineIndex = place.start.line - 1;
185
+ const endLineIndex = (place.end?.line ?? place.start.line) - 1;
186
+ if (startLineIndex < 0 || startLineIndex >= lines.length) return;
187
+ const selectedLines = lines.slice(startLineIndex, endLineIndex + 1);
188
+ if (selectedLines.length === 0) return;
189
+ selectedLines[0] = selectedLines[0].slice(place.start.column - 1);
190
+ const endColumn = place.end?.column;
191
+ if (endColumn !== void 0) {
192
+ const lastIndex = selectedLines.length - 1;
193
+ selectedLines[lastIndex] = selectedLines[lastIndex].slice(0, endColumn - 1);
194
+ }
195
+ return selectedLines.join("\n") || void 0;
196
+ }
197
+ function getErrorPlace(error) {
198
+ if (typeof error !== "object" || error === null) return;
199
+ const candidate = error;
200
+ return toPlace(candidate.place) ?? toPlace(candidate.position);
201
+ }
202
+ function toMessagePlace(message) {
203
+ return toPlace(message.place);
204
+ }
205
+ function toPlace(value) {
206
+ if (typeof value !== "object" || value === null) return;
207
+ const candidate = value;
208
+ if (candidate.start !== void 0 || candidate.end !== void 0) return {
209
+ start: toPoint(candidate.start),
210
+ end: toPoint(candidate.end)
211
+ };
212
+ const point = toPoint(candidate);
213
+ return point === void 0 ? void 0 : { start: point };
214
+ }
215
+ function toPoint(value) {
216
+ if (typeof value !== "object" || value === null) return;
217
+ const candidate = value;
218
+ const line = typeof candidate.line === "number" ? candidate.line : void 0;
219
+ const column = typeof candidate.column === "number" ? candidate.column : void 0;
220
+ const offset = typeof candidate.offset === "number" ? candidate.offset : void 0;
221
+ if (line === void 0 && column === void 0 && offset === void 0) return;
222
+ return {
223
+ line,
224
+ column,
225
+ offset
226
+ };
227
+ }
228
+ function getErrorReason(error) {
229
+ if (error instanceof Error && error.message !== "") return error.message;
230
+ const reason = getErrorField(error, "reason");
231
+ if (reason !== void 0 && reason !== "") return reason;
232
+ return String(error);
233
+ }
234
+ function getErrorCause(error) {
235
+ if (typeof error !== "object" || error === null || !("cause" in error)) return;
236
+ const cause = error.cause;
237
+ if (cause instanceof Error) return {
238
+ name: cause.name,
239
+ message: cause.message
240
+ };
241
+ if (typeof cause === "string" && cause !== "") return { message: cause };
242
+ }
243
+ function getErrorField(error, field) {
244
+ if (typeof error !== "object" || error === null || !(field in error)) return;
245
+ const value = error[field];
246
+ return typeof value === "string" && value !== "" ? value : void 0;
247
+ }
248
+ function getErrorNumberField(error, field) {
249
+ if (typeof error !== "object" || error === null || !(field in error)) return;
250
+ const value = error[field];
251
+ return typeof value === "number" ? value : void 0;
252
+ }
253
+ function toSourceText(source) {
254
+ return typeof source === "string" ? source : new TextDecoder().decode(source);
255
+ }
65
256
  //#endregion
66
- export { MdxCompiler, remarkFrontmatterPlugin, remarkGfmPlugin };
257
+ export { MdxCompileError, MdxCompiler, remarkFrontmatterPlugin, remarkGfmPlugin };
258
+
67
259
  //# sourceMappingURL=compiler.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"compiler.mjs","names":["compileMdxJs"],"sources":["../src/compiler.ts"],"sourcesContent":["import {\n compile as compileMdxJs,\n type CompileOptions as MdxJsCompileOptions,\n} from \"@mdx-js/mdx\";\nimport remarkFrontmatterPlugin from \"remark-frontmatter\";\nimport remarkGfmPlugin from \"remark-gfm\";\nimport type { Pluggable, PluggableList, Plugin } from \"unified\";\nimport { VFile } from \"vfile\";\nimport { matter } from \"vfile-matter\";\nimport type { z } from \"zod\";\n\nimport type { HastNode } from \"./types\";\n\nexport { remarkFrontmatterPlugin, remarkGfmPlugin };\n\n/**\n * Represents the source content for an MDX document.\n */\nexport type MdxSource = string | Uint8Array;\n\n/**\n * Options for compiling an MDX document.\n */\nexport interface MdxCompileOptions {\n frontmatterOnly?: boolean | undefined;\n mdxOptions?: MdxJsCompileOptions;\n}\n\n/**\n * The result of compiling an MDX document.\n * Compiled content is optional, because sometimes we only need the frontmatter.\n */\nexport interface MdxArtifact<TFrontmatter = Record<string, unknown>> {\n compiled?: string | undefined;\n frontmatter: TFrontmatter;\n}\n\n/**\n * A reusable compiler for compiling MDX.\n */\nexport class MdxCompiler {\n private readonly mdxOptions: MdxJsCompileOptions;\n private readonly remarkPlugins: PluggableList;\n private readonly rehypePlugins: PluggableList;\n\n public constructor(mdxOptions: MdxJsCompileOptions = {}) {\n this.mdxOptions = mdxOptions;\n this.remarkPlugins = [remarkFrontmatterPlugin, remarkGfmPlugin];\n this.rehypePlugins = [];\n }\n\n public withRemarkPlugin<TOptions>(\n plugin: Plugin<[TOptions?], HastNode, HastNode>,\n options?: TOptions,\n ): MdxCompiler {\n if (options === undefined) {\n this.remarkPlugins.push(plugin as Pluggable);\n } else {\n this.remarkPlugins.push([plugin, options] as Pluggable);\n }\n return this;\n }\n\n public withRehypePlugin<TOptions>(\n plugin: Plugin<[TOptions?], HastNode, HastNode>,\n options?: TOptions,\n ): MdxCompiler {\n if (options === undefined) {\n this.rehypePlugins.push(plugin as Pluggable);\n } else {\n this.rehypePlugins.push([plugin, options] as Pluggable);\n }\n return this;\n }\n\n public async compile<TFrontmatterSchema extends z.ZodSchema>(\n source: MdxSource,\n { frontmatterOnly = false, mdxOptions }: MdxCompileOptions = {},\n frontmatterSchema?: TFrontmatterSchema,\n ): Promise<MdxArtifact<z.output<TFrontmatterSchema>>> {\n const vfile = new VFile({\n value: source,\n // Needed so vfile doesn't need to access fs.\n cwd: \"/\",\n path: \"/\",\n message: {},\n messages: {},\n });\n matter(vfile, { strip: true });\n\n const remarkPlugins = [\n ...this.remarkPlugins,\n ...(this.mdxOptions?.remarkPlugins ?? []),\n ...(mdxOptions?.remarkPlugins ?? []),\n ];\n const rehypePlugins = [\n ...this.rehypePlugins,\n ...(this.mdxOptions?.rehypePlugins ?? []),\n ...(mdxOptions?.rehypePlugins ?? []),\n ];\n\n let compiled: string | undefined;\n if (!frontmatterOnly) {\n const compiledFile = await compileMdxJs(vfile, {\n outputFormat: \"function-body\",\n ...(this.mdxOptions ?? {}),\n ...(mdxOptions ?? {}),\n remarkPlugins,\n rehypePlugins,\n });\n compiled = compiledFile.value.toString();\n }\n\n const frontmatter =\n frontmatterSchema === undefined\n ? (vfile.data.matter ?? {})\n : frontmatterSchema.parse(vfile.data.matter ?? {});\n\n return {\n compiled,\n frontmatter: frontmatter as z.output<TFrontmatterSchema>,\n };\n }\n}\n"],"mappings":";;;;;;;;;;AAwCA,IAAa,cAAb,MAAyB;CACvB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,AAAO,YAAY,aAAkC,EAAE,EAAE;AACvD,OAAK,aAAa;AAClB,OAAK,gBAAgB,CAAC,yBAAyB,gBAAgB;AAC/D,OAAK,gBAAgB,EAAE;;CAGzB,AAAO,iBACL,QACA,SACa;AACb,MAAI,YAAY,OACd,MAAK,cAAc,KAAK,OAAoB;MAE5C,MAAK,cAAc,KAAK,CAAC,QAAQ,QAAQ,CAAc;AAEzD,SAAO;;CAGT,AAAO,iBACL,QACA,SACa;AACb,MAAI,YAAY,OACd,MAAK,cAAc,KAAK,OAAoB;MAE5C,MAAK,cAAc,KAAK,CAAC,QAAQ,QAAQ,CAAc;AAEzD,SAAO;;CAGT,MAAa,QACX,QACA,EAAE,kBAAkB,OAAO,eAAkC,EAAE,EAC/D,mBACoD;EACpD,MAAM,QAAQ,IAAI,MAAM;GACtB,OAAO;GAEP,KAAK;GACL,MAAM;GACN,SAAS,EAAE;GACX,UAAU,EAAE;GACb,CAAC;AACF,SAAO,OAAO,EAAE,OAAO,MAAM,CAAC;EAE9B,MAAM,gBAAgB;GACpB,GAAG,KAAK;GACR,GAAI,KAAK,YAAY,iBAAiB,EAAE;GACxC,GAAI,YAAY,iBAAiB,EAAE;GACpC;EACD,MAAM,gBAAgB;GACpB,GAAG,KAAK;GACR,GAAI,KAAK,YAAY,iBAAiB,EAAE;GACxC,GAAI,YAAY,iBAAiB,EAAE;GACpC;EAED,IAAI;AACJ,MAAI,CAAC,gBAQH,aAPqB,MAAMA,QAAa,OAAO;GAC7C,cAAc;GACd,GAAI,KAAK,cAAc,EAAE;GACzB,GAAI,cAAc,EAAE;GACpB;GACA;GACD,CAAC,EACsB,MAAM,UAAU;EAG1C,MAAM,cACJ,sBAAsB,SACjB,MAAM,KAAK,UAAU,EAAE,GACxB,kBAAkB,MAAM,MAAM,KAAK,UAAU,EAAE,CAAC;AAEtD,SAAO;GACL;GACa;GACd"}
1
+ {"version":3,"file":"compiler.mjs","names":["compileMdxJs"],"sources":["../src/compiler.ts"],"sourcesContent":["import {\n compile as compileMdxJs,\n type CompileOptions as MdxJsCompileOptions,\n} from \"@mdx-js/mdx\";\nimport remarkFrontmatterPlugin from \"remark-frontmatter\";\nimport remarkGfmPlugin from \"remark-gfm\";\nimport type { Pluggable, PluggableList, Plugin } from \"unified\";\nimport { VFile } from \"vfile\";\nimport { matter } from \"vfile-matter\";\nimport type { z } from \"zod\";\n\nimport type { HastNode } from \"./types\";\n\nexport { remarkFrontmatterPlugin, remarkGfmPlugin };\n\n/**\n * Represents the source content for an MDX document.\n */\nexport type MdxSource = string | Uint8Array;\n\n/**\n * Options for compiling an MDX document.\n */\nexport interface MdxCompileOptions {\n frontmatterOnly?: boolean | undefined;\n mdxOptions?: MdxJsCompileOptions;\n}\n\nexport interface MdxMessagePoint {\n line?: number | undefined;\n column?: number | undefined;\n offset?: number | undefined;\n}\n\nexport interface MdxMessagePlace {\n start?: MdxMessagePoint | undefined;\n end?: MdxMessagePoint | undefined;\n}\n\nexport interface MdxMessage {\n message: string;\n reason: string;\n source?: string | undefined;\n ruleId?: string | undefined;\n fatal?: boolean | undefined;\n line?: number | undefined;\n column?: number | undefined;\n place?: MdxMessagePlace | undefined;\n sourceLine?: string | undefined;\n snippet?: string | undefined;\n cause?: {\n name?: string | undefined;\n message: string;\n };\n}\n\ntype VFileLikeMessage = VFile[\"messages\"][number];\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 messages: MdxMessage[];\n}\n\nexport class MdxCompileError extends Error {\n public readonly reason: string;\n public readonly source?: string | undefined;\n public readonly ruleId?: string | undefined;\n public readonly line?: number | undefined;\n public readonly column?: number | undefined;\n public readonly place?: MdxMessagePlace | undefined;\n public readonly sourceLine?: string | undefined;\n public readonly snippet?: string | undefined;\n public readonly diagnostics: MdxMessage[];\n\n public constructor(\n reason: string,\n options: {\n source?: string | undefined;\n ruleId?: string | undefined;\n line?: number | undefined;\n column?: number | undefined;\n place?: MdxMessagePlace | undefined;\n sourceLine?: string | undefined;\n snippet?: string | undefined;\n diagnostics?: MdxMessage[] | undefined;\n cause?: unknown;\n } = {},\n ) {\n super(\n formatDiagnosticMessage(reason, {\n source: options.source,\n ruleId: options.ruleId,\n line: options.line,\n column: options.column,\n sourceLine: options.sourceLine,\n snippet: options.snippet,\n }),\n options.cause === undefined ? undefined : { cause: options.cause },\n );\n\n this.name = \"MdxCompileError\";\n this.reason = reason;\n this.source = options.source;\n this.ruleId = options.ruleId;\n this.line = options.line;\n this.column = options.column;\n this.place = options.place;\n this.sourceLine = options.sourceLine;\n this.snippet = options.snippet;\n this.diagnostics = options.diagnostics ?? [];\n }\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 sourceText = toSourceText(source);\n const vfile = new VFile({\n value: source,\n // Needed so vfile doesn't need to access fs.\n cwd: \"/\",\n path: \"/source.mdx\",\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 try {\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 } catch (error) {\n throw toMdxCompileError(error, sourceText, vfile.messages);\n }\n }\n\n const frontmatter =\n frontmatterSchema === undefined\n ? (vfile.data.matter ?? {})\n : (frontmatterSchema as z.ZodSchema).parse(vfile.data.matter ?? {});\n\n return {\n compiled,\n frontmatter: frontmatter as z.output<TFrontmatterSchema>,\n messages: normalizeMessages(vfile.messages, sourceText),\n };\n }\n}\n\nfunction toMdxCompileError(\n error: unknown,\n sourceText: string,\n diagnostics: readonly VFileLikeMessage[],\n): MdxCompileError {\n const details = getDiagnosticDetails(sourceText, getErrorPlace(error), error);\n\n return new MdxCompileError(getErrorReason(error), {\n source: getErrorField(error, \"source\"),\n ruleId: getErrorField(error, \"ruleId\"),\n line: details.line,\n column: details.column,\n place: details.place,\n sourceLine: details.sourceLine,\n snippet: details.snippet,\n diagnostics: normalizeMessages(diagnostics, sourceText),\n cause: error,\n });\n}\n\nfunction normalizeMessages(\n messages: readonly VFileLikeMessage[],\n sourceText: string,\n): MdxMessage[] {\n return messages.map((message) => {\n const details = getDiagnosticDetails(\n sourceText,\n toMessagePlace(message),\n message,\n );\n const reason = getErrorReason(message);\n const source = getErrorField(message, \"source\");\n const ruleId = getErrorField(message, \"ruleId\");\n const cause = getErrorCause(message);\n\n return {\n message: formatDiagnosticMessage(reason, {\n source,\n ruleId,\n line: details.line,\n column: details.column,\n sourceLine: details.sourceLine,\n snippet: details.snippet,\n causeMessage: cause?.message,\n }),\n reason,\n source,\n ruleId,\n fatal: typeof message.fatal === \"boolean\" ? message.fatal : undefined,\n line: details.line,\n column: details.column,\n place: details.place,\n sourceLine: details.sourceLine,\n snippet: details.snippet,\n cause,\n };\n });\n}\n\nfunction formatDiagnosticMessage(\n reason: string,\n options: {\n source?: string | undefined;\n ruleId?: string | undefined;\n line?: number | undefined;\n column?: number | undefined;\n sourceLine?: string | undefined;\n snippet?: string | undefined;\n causeMessage?: string | undefined;\n },\n): string {\n const origin =\n options.source === undefined\n ? undefined\n : options.ruleId === undefined\n ? options.source\n : `${options.source}:${options.ruleId}`;\n const location =\n options.line === undefined\n ? undefined\n : options.column === undefined\n ? `at ${options.line}`\n : `at ${options.line}:${options.column}`;\n\n const lines = [[reason, origin, location].filter(Boolean).join(\" \")];\n\n if (options.snippet !== undefined) {\n lines.push(`Source: ${options.snippet}`);\n }\n if (\n options.sourceLine !== undefined &&\n options.sourceLine !== options.snippet\n ) {\n lines.push(`Line: ${options.sourceLine}`);\n }\n if (\n options.causeMessage !== undefined &&\n options.causeMessage !== \"\" &&\n options.causeMessage !== reason\n ) {\n lines.push(`Cause: ${options.causeMessage}`);\n }\n\n return lines.join(\"\\n\");\n}\n\nfunction getDiagnosticDetails(\n sourceText: string,\n place: MdxMessagePlace | undefined,\n error: unknown,\n): {\n line?: number | undefined;\n column?: number | undefined;\n place?: MdxMessagePlace | undefined;\n sourceLine?: string | undefined;\n snippet?: string | undefined;\n} {\n const line = place?.start?.line ?? getErrorNumberField(error, \"line\");\n const column = place?.start?.column ?? getErrorNumberField(error, \"column\");\n const normalizedPlace =\n line === undefined && column === undefined && place === undefined\n ? undefined\n : {\n start: {\n line,\n column,\n offset: place?.start?.offset,\n },\n end:\n place?.end === undefined\n ? undefined\n : {\n line: place.end.line,\n column: place.end.column,\n offset: place.end.offset,\n },\n };\n\n return {\n line,\n column,\n place: normalizedPlace,\n sourceLine: getSourceLine(sourceText, line),\n snippet: getSourceSnippet(sourceText, normalizedPlace),\n };\n}\n\nfunction getSourceLine(\n sourceText: string,\n line: number | undefined,\n): string | undefined {\n if (line === undefined || line < 1) {\n return undefined;\n }\n return sourceText.split(/\\r?\\n/u)[line - 1];\n}\n\nfunction getSourceSnippet(\n sourceText: string,\n place: MdxMessagePlace | undefined,\n): string | undefined {\n if (place?.start?.offset !== undefined && place.end?.offset !== undefined) {\n return sourceText.slice(place.start.offset, place.end.offset) || undefined;\n }\n\n if (place?.start?.line === undefined || place.start.column === undefined) {\n return undefined;\n }\n\n const lines = sourceText.split(/\\r?\\n/u);\n const startLineIndex = place.start.line - 1;\n const endLineIndex = (place.end?.line ?? place.start.line) - 1;\n\n if (startLineIndex < 0 || startLineIndex >= lines.length) {\n return undefined;\n }\n\n const selectedLines = lines.slice(startLineIndex, endLineIndex + 1);\n if (selectedLines.length === 0) {\n return undefined;\n }\n\n selectedLines[0] = selectedLines[0].slice(place.start.column - 1);\n\n const endColumn = place.end?.column;\n if (endColumn !== undefined) {\n const lastIndex = selectedLines.length - 1;\n selectedLines[lastIndex] = selectedLines[lastIndex].slice(0, endColumn - 1);\n }\n\n return selectedLines.join(\"\\n\") || undefined;\n}\n\nfunction getErrorPlace(error: unknown): MdxMessagePlace | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n\n const candidate = error as {\n place?: unknown;\n position?: unknown;\n };\n\n return toPlace(candidate.place) ?? toPlace(candidate.position);\n}\n\nfunction toMessagePlace(\n message: VFileLikeMessage,\n): MdxMessagePlace | undefined {\n return toPlace(message.place);\n}\n\nfunction toPlace(value: unknown): MdxMessagePlace | undefined {\n if (typeof value !== \"object\" || value === null) {\n return undefined;\n }\n\n const candidate = value as {\n start?: unknown;\n end?: unknown;\n line?: unknown;\n column?: unknown;\n offset?: unknown;\n };\n\n if (candidate.start !== undefined || candidate.end !== undefined) {\n return {\n start: toPoint(candidate.start),\n end: toPoint(candidate.end),\n };\n }\n\n const point = toPoint(candidate);\n return point === undefined ? undefined : { start: point };\n}\n\nfunction toPoint(value: unknown): MdxMessagePoint | undefined {\n if (typeof value !== \"object\" || value === null) {\n return undefined;\n }\n\n const candidate = value as {\n line?: unknown;\n column?: unknown;\n offset?: unknown;\n };\n const line = typeof candidate.line === \"number\" ? candidate.line : undefined;\n const column =\n typeof candidate.column === \"number\" ? candidate.column : undefined;\n const offset =\n typeof candidate.offset === \"number\" ? candidate.offset : undefined;\n\n if (line === undefined && column === undefined && offset === undefined) {\n return undefined;\n }\n\n return { line, column, offset };\n}\n\nfunction getErrorReason(error: unknown): string {\n if (error instanceof Error && error.message !== \"\") {\n return error.message;\n }\n\n const reason = getErrorField(error, \"reason\");\n if (reason !== undefined && reason !== \"\") {\n return reason;\n }\n\n return String(error);\n}\n\nfunction getErrorCause(\n error: unknown,\n): { name?: string | undefined; message: string } | undefined {\n if (typeof error !== \"object\" || error === null || !(\"cause\" in error)) {\n return undefined;\n }\n\n const cause = (error as { cause?: unknown }).cause;\n if (cause instanceof Error) {\n return {\n name: cause.name,\n message: cause.message,\n };\n }\n if (typeof cause === \"string\" && cause !== \"\") {\n return {\n message: cause,\n };\n }\n\n return undefined;\n}\n\nfunction getErrorField(\n error: unknown,\n field: \"source\" | \"ruleId\" | \"reason\",\n): string | undefined {\n if (typeof error !== \"object\" || error === null || !(field in error)) {\n return undefined;\n }\n\n const value = (error as Record<string, unknown>)[field];\n return typeof value === \"string\" && value !== \"\" ? value : undefined;\n}\n\nfunction getErrorNumberField(\n error: unknown,\n field: \"line\" | \"column\",\n): number | undefined {\n if (typeof error !== \"object\" || error === null || !(field in error)) {\n return undefined;\n }\n\n const value = (error as Record<string, unknown>)[field];\n return typeof value === \"number\" ? value : undefined;\n}\n\nfunction toSourceText(source: MdxSource): string {\n return typeof source === \"string\" ? source : new TextDecoder().decode(source);\n}\n"],"mappings":";;;;;;AAoEA,IAAa,kBAAb,cAAqC,MAAM;CACzC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CAEA,YACE,QACA,UAUI,EAAE,EACN;AACA,QACE,wBAAwB,QAAQ;GAC9B,QAAQ,QAAQ;GAChB,QAAQ,QAAQ;GAChB,MAAM,QAAQ;GACd,QAAQ,QAAQ;GAChB,YAAY,QAAQ;GACpB,SAAS,QAAQ;GAClB,CAAC,EACF,QAAQ,UAAU,KAAA,IAAY,KAAA,IAAY,EAAE,OAAO,QAAQ,OAAO,CACnE;AAED,OAAK,OAAO;AACZ,OAAK,SAAS;AACd,OAAK,SAAS,QAAQ;AACtB,OAAK,SAAS,QAAQ;AACtB,OAAK,OAAO,QAAQ;AACpB,OAAK,SAAS,QAAQ;AACtB,OAAK,QAAQ,QAAQ;AACrB,OAAK,aAAa,QAAQ;AAC1B,OAAK,UAAU,QAAQ;AACvB,OAAK,cAAc,QAAQ,eAAe,EAAE;;;;;;AAOhD,IAAa,cAAb,MAAyB;CACvB;CACA;CACA;CAEA,YAAmB,aAAkC,EAAE,EAAE;A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AO,UAAU,WAAW,WAAW,UAAU,SAAS,KAAA;CAC5D,MAAM,SACJ,OAAO,UAAU,WAAW,WAAW,UAAU,SAAS,KAAA;AAE5D,KAAI,SAAS,KAAA,KAAa,WAAW,KAAA,KAAa,WAAW,KAAA,EAC3D;AAGF,QAAO;EAAE;EAAM;EAAQ;EAAQ;;AAGjC,SAAS,eAAe,OAAwB;AAC9C,KAAI,iBAAiB,SAAS,MAAM,YAAY,GAC9C,QAAO,MAAM;CAGf,MAAM,SAAS,cAAc,OAAO,SAAS;AAC7C,KAAI,WAAW,KAAA,KAAa,WAAW,GACrC,QAAO;AAGT,QAAO,OAAO,MAAM;;AAGtB,SAAS,cACP,OAC4D;AAC5D,KAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,EAAE,WAAW,OAC9D;CAGF,MAAM,QAAS,MAA8B;AAC7C,KAAI,iBAAiB,MACnB,QAAO;EACL,MAAM,MAAM;EACZ,SAAS,MAAM;EAChB;AAEH,KAAI,OAAO,UAAU,YAAY,UAAU,GACzC,QAAO,EACL,SAAS,OACV;;AAML,SAAS,cACP,OACA,OACoB;AACpB,KAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,EAAE,SAAS,OAC5D;CAGF,MAAM,QAAS,MAAkC;AACjD,QAAO,OAAO,UAAU,YAAY,UAAU,KAAK,QAAQ,KAAA;;AAG7D,SAAS,oBACP,OACA,OACoB;AACpB,KAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,EAAE,SAAS,OAC5D;CAGF,MAAM,QAAS,MAAkC;AACjD,QAAO,OAAO,UAAU,WAAW,QAAQ,KAAA;;AAG7C,SAAS,aAAa,QAA2B;AAC/C,QAAO,OAAO,WAAW,WAAW,SAAS,IAAI,aAAa,CAAC,OAAO,OAAO"}
package/dist/mod.d.mts CHANGED
@@ -1,8 +1,7 @@
1
1
  import { HastElement as Element, HastNode as Node } from "./types.mjs";
2
- import { MdxArtifact, MdxCompileOptions, MdxCompiler, MdxSource, remarkFrontmatterPlugin, remarkGfmPlugin } from "./compiler.mjs";
2
+ import { MdxArtifact, MdxCompileError, MdxCompileOptions, MdxCompiler, MdxMessage, MdxMessagePlace, MdxMessagePoint, MdxSource, remarkFrontmatterPlugin, remarkGfmPlugin } from "./compiler.mjs";
3
3
  import { HeadingIdPluginOptions, headingIdPlugin } from "./plugins/heading-id/plugin.mjs";
4
4
  import { removeImportsExportsPlugin } from "./plugins/remove-imports-exports.mjs";
5
5
  import { SyntaxHighlightPluginOptions, syntaxHighlightPlugin } from "./plugins/syntax-highlight/plugin.mjs";
6
6
  import { MdxTreeProcessor, treeProcessorPlugin } from "./plugins/tree-processor/plugin.mjs";
7
- import "./plugins/mod.mjs";
8
- export { Element as HastElement, Node as HastNode, HeadingIdPluginOptions, MdxArtifact, MdxCompileOptions, MdxCompiler, MdxSource, MdxTreeProcessor, SyntaxHighlightPluginOptions, headingIdPlugin, remarkFrontmatterPlugin, remarkGfmPlugin, removeImportsExportsPlugin, syntaxHighlightPlugin, treeProcessorPlugin };
7
+ export { Element as HastElement, Node as HastNode, HeadingIdPluginOptions, MdxArtifact, MdxCompileError, MdxCompileOptions, MdxCompiler, MdxMessage, MdxMessagePlace, MdxMessagePoint, MdxSource, MdxTreeProcessor, SyntaxHighlightPluginOptions, headingIdPlugin, remarkFrontmatterPlugin, remarkGfmPlugin, removeImportsExportsPlugin, syntaxHighlightPlugin, treeProcessorPlugin };
package/dist/mod.mjs CHANGED
@@ -1,8 +1,7 @@
1
- import { MdxCompiler, remarkFrontmatterPlugin, remarkGfmPlugin } from "./compiler.mjs";
1
+ import { MdxCompileError, MdxCompiler, remarkFrontmatterPlugin, remarkGfmPlugin } from "./compiler.mjs";
2
2
  import { headingIdPlugin } from "./plugins/heading-id/plugin.mjs";
3
3
  import { removeImportsExportsPlugin } from "./plugins/remove-imports-exports.mjs";
4
4
  import { syntaxHighlightPlugin } from "./plugins/syntax-highlight/plugin.mjs";
5
5
  import { treeProcessorPlugin } from "./plugins/tree-processor/plugin.mjs";
6
6
  import "./plugins/mod.mjs";
7
-
8
- export { MdxCompiler, headingIdPlugin, remarkFrontmatterPlugin, remarkGfmPlugin, removeImportsExportsPlugin, syntaxHighlightPlugin, treeProcessorPlugin };
7
+ export { MdxCompileError, MdxCompiler, headingIdPlugin, remarkFrontmatterPlugin, remarkGfmPlugin, removeImportsExportsPlugin, syntaxHighlightPlugin, treeProcessorPlugin };
@@ -1,7 +1,6 @@
1
1
  import GithubSlugger from "github-slugger";
2
2
  import { toString } from "hast-util-to-string";
3
3
  import { visit } from "unist-util-visit";
4
-
5
4
  //#region src/plugins/heading-id/plugin.ts
6
5
  /**
7
6
  * A rehype plugin that adds IDs to heading tags.
@@ -18,7 +17,7 @@ const headingIdPlugin = ({ prefix } = {}) => {
18
17
  return tree;
19
18
  };
20
19
  };
21
-
22
20
  //#endregion
23
21
  export { headingIdPlugin };
22
+
24
23
  //# sourceMappingURL=plugin.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.mjs","names":["hastToString"],"sources":["../../../src/plugins/heading-id/plugin.ts"],"sourcesContent":["import GithubSlugger from \"github-slugger\";\nimport { toString as hastToString } from \"hast-util-to-string\";\nimport type { Plugin } from \"unified\";\nimport { visit } from \"unist-util-visit\";\n\nimport type { HastElement, HastNode } from \"../../types\";\n\n/**\n * Options for {@linkcode headingIdPlugin}.\n */\nexport interface HeadingIdPluginOptions {\n /** Prefix for title slugs. */\n prefix?: string;\n}\n\n/**\n * A rehype plugin that adds IDs to heading tags.\n */\nexport const headingIdPlugin: Plugin<\n [HeadingIdPluginOptions?],\n HastNode,\n HastNode\n> = ({ prefix } = {}) => {\n const slugger = new GithubSlugger();\n return (tree) => {\n slugger.reset();\n visit(\n tree,\n \"element\",\n function visitor(\n node: HastElement,\n index: number,\n parent: HastElement,\n ): void {\n if (!parent || index === null || !node.tagName.startsWith(\"h\")) return;\n const id = (prefix ?? \"\") + slugger.slug(hastToString(node));\n node.properties.id = id;\n },\n );\n return tree;\n };\n};\n"],"mappings":";;;;;;;;AAkBA,MAAa,mBAIR,EAAE,WAAW,EAAE,KAAK;CACvB,MAAM,UAAU,IAAI,eAAe;AACnC,SAAQ,SAAS;AACf,UAAQ,OAAO;AACf,QACE,MACA,WACA,SAAS,QACP,MACA,OACA,QACM;AACN,OAAI,CAAC,UAAU,UAAU,QAAQ,CAAC,KAAK,QAAQ,WAAW,IAAI,CAAE;GAChE,MAAM,MAAM,UAAU,MAAM,QAAQ,KAAKA,SAAa,KAAK,CAAC;AAC5D,QAAK,WAAW,KAAK;IAExB;AACD,SAAO"}
1
+ {"version":3,"file":"plugin.mjs","names":["hastToString"],"sources":["../../../src/plugins/heading-id/plugin.ts"],"sourcesContent":["import GithubSlugger from \"github-slugger\";\nimport { toString as hastToString } from \"hast-util-to-string\";\nimport type { Plugin } from \"unified\";\nimport { visit } from \"unist-util-visit\";\n\nimport type { HastElement, HastNode } from \"../../types\";\n\n/**\n * Options for {@linkcode headingIdPlugin}.\n */\nexport interface HeadingIdPluginOptions {\n /** Prefix for title slugs. */\n prefix?: string;\n}\n\n/**\n * A rehype plugin that adds IDs to heading tags.\n */\nexport const headingIdPlugin: Plugin<\n [HeadingIdPluginOptions?],\n HastNode,\n HastNode\n> = ({ prefix } = {}) => {\n const slugger = new GithubSlugger();\n return (tree) => {\n slugger.reset();\n visit(\n tree,\n \"element\",\n function visitor(\n node: HastElement,\n index: number,\n parent: HastElement,\n ): void {\n if (!parent || index === null || !node.tagName.startsWith(\"h\")) return;\n const id = (prefix ?? \"\") + slugger.slug(hastToString(node));\n node.properties.id = id;\n },\n );\n return tree;\n };\n};\n"],"mappings":";;;;;;;AAkBA,MAAa,mBAIR,EAAE,WAAW,EAAE,KAAK;CACvB,MAAM,UAAU,IAAI,eAAe;AACnC,SAAQ,SAAS;AACf,UAAQ,OAAO;AACf,QACE,MACA,WACA,SAAS,QACP,MACA,OACA,QACM;AACN,OAAI,CAAC,UAAU,UAAU,QAAQ,CAAC,KAAK,QAAQ,WAAW,IAAI,CAAE;GAChE,MAAM,MAAM,UAAU,MAAM,QAAQ,KAAKA,SAAa,KAAK,CAAC;AAC5D,QAAK,WAAW,KAAK;IAExB;AACD,SAAO"}
@@ -2,5 +2,4 @@ import { headingIdPlugin } from "./heading-id/plugin.mjs";
2
2
  import { removeImportsExportsPlugin } from "./remove-imports-exports.mjs";
3
3
  import { syntaxHighlightPlugin } from "./syntax-highlight/plugin.mjs";
4
4
  import { treeProcessorPlugin } from "./tree-processor/plugin.mjs";
5
-
6
- export { headingIdPlugin, removeImportsExportsPlugin, syntaxHighlightPlugin, treeProcessorPlugin };
5
+ export { headingIdPlugin, removeImportsExportsPlugin, syntaxHighlightPlugin, treeProcessorPlugin };
@@ -1,5 +1,4 @@
1
1
  import { remove } from "unist-util-remove";
2
-
3
2
  //#region src/plugins/remove-imports-exports.ts
4
3
  /**
5
4
  * A remark plugin which removes all import and export statements.
@@ -10,7 +9,7 @@ const removeImportsExportsPlugin = () => {
10
9
  return tree;
11
10
  };
12
11
  };
13
-
14
12
  //#endregion
15
13
  export { removeImportsExportsPlugin };
14
+
16
15
  //# sourceMappingURL=remove-imports-exports.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"remove-imports-exports.mjs","names":[],"sources":["../../src/plugins/remove-imports-exports.ts"],"sourcesContent":["import type { Plugin } from \"unified\";\nimport { remove } from \"unist-util-remove\";\n\nimport type { HastNode } from \"../types\";\n\n/**\n * A remark plugin which removes all import and export statements.\n */\nexport const removeImportsExportsPlugin: Plugin<\n [unknown],\n HastNode,\n HastNode\n> = () => {\n return (tree: HastNode) => {\n remove(tree, \"mdxjsEsm\");\n return tree;\n };\n};\n"],"mappings":";;;;;;AAQA,MAAa,mCAIH;AACR,SAAQ,SAAmB;AACzB,SAAO,MAAM,WAAW;AACxB,SAAO"}
1
+ {"version":3,"file":"remove-imports-exports.mjs","names":[],"sources":["../../src/plugins/remove-imports-exports.ts"],"sourcesContent":["import type { Plugin } from \"unified\";\nimport { remove } from \"unist-util-remove\";\n\nimport type { HastNode } from \"../types\";\n\n/**\n * A remark plugin which removes all import and export statements.\n */\nexport const removeImportsExportsPlugin: Plugin<\n [unknown],\n HastNode,\n HastNode\n> = () => {\n return (tree: HastNode) => {\n remove(tree, \"mdxjsEsm\");\n return tree;\n };\n};\n"],"mappings":";;;;;AAQA,MAAa,mCAIH;AACR,SAAQ,SAAmB;AACzB,SAAO,MAAM,WAAW;AACxB,SAAO"}
@@ -1,6 +1,5 @@
1
1
  import { parseNumericRange } from "@temelj/iterator";
2
2
  import * as z$1 from "zod";
3
-
4
3
  //#region src/plugins/syntax-highlight/code-meta.ts
5
4
  function extractCodeMeta(node) {
6
5
  return parseMetaString(node?.data?.meta ?? node?.properties?.metastring ?? "");
@@ -32,7 +31,7 @@ function parseMetaString(meta) {
32
31
  if (parsed.fileName !== void 0) options.fileName = parsed.fileName.trim();
33
32
  return options;
34
33
  }
35
-
36
34
  //#endregion
37
35
  export { extractCodeMeta };
36
+
38
37
  //# sourceMappingURL=code-meta.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"code-meta.mjs","names":["z"],"sources":["../../../src/plugins/syntax-highlight/code-meta.ts"],"sourcesContent":["import { type NumericRange, parseNumericRange } from \"@temelj/iterator\";\nimport * as z from \"zod\";\n\nimport type { HastElement } from \"../../types\";\n\nexport interface MdxCodeMeta {\n highlight?: MdxCodeMetaHighlightLine;\n showLineNumbers?: MdxCodeMetaLineNumbers;\n commandLine?: MdxCodeMetaCommandLine;\n fileName?: string;\n}\n\nexport type MdxCodeMetaHighlightLine = {\n lineRange: NumericRange;\n};\n\nexport type MdxCodeMetaLineNumbers = {\n lineRange: NumericRange;\n};\n\nexport type MdxCodeMetaCommandLine = {\n commandRange: NumericRange;\n};\n\nexport function extractCodeMeta(node: HastElement): MdxCodeMeta {\n const meta = node?.data?.meta ?? node?.properties?.metastring ?? \"\";\n return parseMetaString(meta as string);\n}\n\nconst metaSchema = z.object({\n highlight: z.optional(z.string()),\n showLineNumbers: z.optional(z.union([z.boolean(), z.string()])),\n commandLine: z.optional(z.union([z.boolean(), z.string()])),\n fileName: z.optional(z.string()),\n});\n\nfunction parseMetaString(meta: string): MdxCodeMeta {\n let raw: unknown;\n try {\n raw = JSON.parse(meta.trim());\n } catch {\n return {};\n }\n const parsed = metaSchema.parse(raw);\n const options: MdxCodeMeta = {};\n\n if (parsed.highlight !== undefined) {\n options.highlight = { lineRange: parseNumericRange(parsed.highlight) };\n }\n\n if (parsed.showLineNumbers !== undefined) {\n if (typeof parsed.showLineNumbers === \"string\") {\n options.showLineNumbers = {\n lineRange: parseNumericRange(parsed.showLineNumbers),\n };\n } else if (parsed.showLineNumbers) {\n options.showLineNumbers = { lineRange: [] };\n }\n }\n\n if (parsed.commandLine !== undefined) {\n if (typeof parsed.commandLine === \"string\") {\n options.commandLine = {\n commandRange: parseNumericRange(parsed.commandLine),\n };\n } else if (parsed.commandLine) {\n options.commandLine = { commandRange: [] };\n }\n }\n\n if (parsed.fileName !== undefined) {\n options.fileName = parsed.fileName.trim();\n }\n\n return options;\n}\n"],"mappings":";;;;AAwBA,SAAgB,gBAAgB,MAAgC;AAE9D,QAAO,gBADM,MAAM,MAAM,QAAQ,MAAM,YAAY,cAAc,GAC3B;;AAGxC,MAAM,aAAaA,IAAE,OAAO;CAC1B,WAAWA,IAAE,SAASA,IAAE,QAAQ,CAAC;CACjC,iBAAiBA,IAAE,SAASA,IAAE,MAAM,CAACA,IAAE,SAAS,EAAEA,IAAE,QAAQ,CAAC,CAAC,CAAC;CAC/D,aAAaA,IAAE,SAASA,IAAE,MAAM,CAACA,IAAE,SAAS,EAAEA,IAAE,QAAQ,CAAC,CAAC,CAAC;CAC3D,UAAUA,IAAE,SAASA,IAAE,QAAQ,CAAC;CACjC,CAAC;AAEF,SAAS,gBAAgB,MAA2B;CAClD,IAAI;AACJ,KAAI;AACF,QAAM,KAAK,MAAM,KAAK,MAAM,CAAC;SACvB;AACN,SAAO,EAAE;;CAEX,MAAM,SAAS,WAAW,MAAM,IAAI;CACpC,MAAM,UAAuB,EAAE;AAE/B,KAAI,OAAO,cAAc,OACvB,SAAQ,YAAY,EAAE,WAAW,kBAAkB,OAAO,UAAU,EAAE;AAGxE,KAAI,OAAO,oBAAoB,QAC7B;MAAI,OAAO,OAAO,oBAAoB,SACpC,SAAQ,kBAAkB,EACxB,WAAW,kBAAkB,OAAO,gBAAgB,EACrD;WACQ,OAAO,gBAChB,SAAQ,kBAAkB,EAAE,WAAW,EAAE,EAAE;;AAI/C,KAAI,OAAO,gBAAgB,QACzB;MAAI,OAAO,OAAO,gBAAgB,SAChC,SAAQ,cAAc,EACpB,cAAc,kBAAkB,OAAO,YAAY,EACpD;WACQ,OAAO,YAChB,SAAQ,cAAc,EAAE,cAAc,EAAE,EAAE;;AAI9C,KAAI,OAAO,aAAa,OACtB,SAAQ,WAAW,OAAO,SAAS,MAAM;AAG3C,QAAO"}
1
+ {"version":3,"file":"code-meta.mjs","names":["z"],"sources":["../../../src/plugins/syntax-highlight/code-meta.ts"],"sourcesContent":["import { type NumericRange, parseNumericRange } from \"@temelj/iterator\";\nimport * as z from \"zod\";\n\nimport type { HastElement } from \"../../types\";\n\nexport interface MdxCodeMeta {\n highlight?: MdxCodeMetaHighlightLine;\n showLineNumbers?: MdxCodeMetaLineNumbers;\n commandLine?: MdxCodeMetaCommandLine;\n fileName?: string;\n}\n\nexport type MdxCodeMetaHighlightLine = {\n lineRange: NumericRange;\n};\n\nexport type MdxCodeMetaLineNumbers = {\n lineRange: NumericRange;\n};\n\nexport type MdxCodeMetaCommandLine = {\n commandRange: NumericRange;\n};\n\nexport function extractCodeMeta(node: HastElement): MdxCodeMeta {\n const meta = node?.data?.meta ?? node?.properties?.metastring ?? \"\";\n return parseMetaString(meta as string);\n}\n\nconst metaSchema = z.object({\n highlight: z.optional(z.string()),\n showLineNumbers: z.optional(z.union([z.boolean(), z.string()])),\n commandLine: z.optional(z.union([z.boolean(), z.string()])),\n fileName: z.optional(z.string()),\n});\n\nfunction parseMetaString(meta: string): MdxCodeMeta {\n let raw: unknown;\n try {\n raw = JSON.parse(meta.trim());\n } catch {\n return {};\n }\n const parsed = metaSchema.parse(raw);\n const options: MdxCodeMeta = {};\n\n if (parsed.highlight !== undefined) {\n options.highlight = { lineRange: parseNumericRange(parsed.highlight) };\n }\n\n if (parsed.showLineNumbers !== undefined) {\n if (typeof parsed.showLineNumbers === \"string\") {\n options.showLineNumbers = {\n lineRange: parseNumericRange(parsed.showLineNumbers),\n };\n } else if (parsed.showLineNumbers) {\n options.showLineNumbers = { lineRange: [] };\n }\n }\n\n if (parsed.commandLine !== undefined) {\n if (typeof parsed.commandLine === \"string\") {\n options.commandLine = {\n commandRange: parseNumericRange(parsed.commandLine),\n };\n } else if (parsed.commandLine) {\n options.commandLine = { commandRange: [] };\n }\n }\n\n if (parsed.fileName !== undefined) {\n options.fileName = parsed.fileName.trim();\n }\n\n return options;\n}\n"],"mappings":";;;AAwBA,SAAgB,gBAAgB,MAAgC;AAE9D,QAAO,gBADM,MAAM,MAAM,QAAQ,MAAM,YAAY,cAAc,GAC3B;;AAGxC,MAAM,aAAaA,IAAE,OAAO;CAC1B,WAAWA,IAAE,SAASA,IAAE,QAAQ,CAAC;CACjC,iBAAiBA,IAAE,SAASA,IAAE,MAAM,CAACA,IAAE,SAAS,EAAEA,IAAE,QAAQ,CAAC,CAAC,CAAC;CAC/D,aAAaA,IAAE,SAASA,IAAE,MAAM,CAACA,IAAE,SAAS,EAAEA,IAAE,QAAQ,CAAC,CAAC,CAAC;CAC3D,UAAUA,IAAE,SAASA,IAAE,QAAQ,CAAC;CACjC,CAAC;AAEF,SAAS,gBAAgB,MAA2B;CAClD,IAAI;AACJ,KAAI;AACF,QAAM,KAAK,MAAM,KAAK,MAAM,CAAC;SACvB;AACN,SAAO,EAAE;;CAEX,MAAM,SAAS,WAAW,MAAM,IAAI;CACpC,MAAM,UAAuB,EAAE;AAE/B,KAAI,OAAO,cAAc,KAAA,EACvB,SAAQ,YAAY,EAAE,WAAW,kBAAkB,OAAO,UAAU,EAAE;AAGxE,KAAI,OAAO,oBAAoB,KAAA;MACzB,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,KAAA;MACrB,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,KAAA,EACtB,SAAQ,WAAW,OAAO,SAAS,MAAM;AAG3C,QAAO"}
@@ -4,7 +4,6 @@ import { visit } from "unist-util-visit";
4
4
  import { transformerNotationHighlight } from "@shikijs/transformers";
5
5
  import { numericRangeContains } from "@temelj/iterator";
6
6
  import { ShikiError, codeToHast } from "shiki";
7
-
8
7
  //#region src/plugins/syntax-highlight/plugin.ts
9
8
  /**
10
9
  * A plugin that highlights code blocks using Shiki.
@@ -88,7 +87,7 @@ function getNodeLanguage(node) {
88
87
  if (typeof language === "string") return language.slice(9);
89
88
  return language;
90
89
  }
91
-
92
90
  //#endregion
93
91
  export { syntaxHighlightPlugin };
92
+
94
93
  //# sourceMappingURL=plugin.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.mjs","names":["hastToString"],"sources":["../../../src/plugins/syntax-highlight/plugin.ts"],"sourcesContent":["import {\n type TransformerNotationHighlightOptions,\n transformerNotationHighlight,\n} from \"@shikijs/transformers\";\nimport { numericRangeContains } from \"@temelj/iterator\";\nimport type { Properties } from \"hast\";\nimport { toString as hastToString } from \"hast-util-to-string\";\nimport {\n type BundledLanguage,\n type BundledTheme,\n type CodeToHastOptions,\n codeToHast,\n ShikiError,\n} from \"shiki\";\nimport type { Plugin } from \"unified\";\nimport { visit } from \"unist-util-visit\";\n\nimport type { HastElement, HastNode } from \"../../types\";\nimport { extractCodeMeta } from \"./code-meta\";\n\ntype ShikiHastOptions = Partial<\n CodeToHastOptions<BundledLanguage, BundledTheme>\n>;\n\ntype SyntaxDataAttribute = \"source-code\" | \"language\" | \"line-count\";\n\n/**\n * Options for {@linkcode syntaxHighlightPlugin}.\n */\nexport interface SyntaxHighlightPluginOptions {\n /**\n * Include these data attributes on the `<pre>` element.\n * These might be useful to have when building custom code components.\n */\n includeDataAttributes?: SyntaxDataAttribute[];\n\n /**\n * Options for the highlight transformer.\n */\n highlight?: {\n transformer?: TransformerNotationHighlightOptions;\n };\n\n /**\n * Options for line numbers.\n */\n lineNumbers?: {\n /**\n * The name of the variable that contains the line number width.\n * This is used for padding the line numbers so that they align.\n */\n widthVariableName?: string;\n /**\n * A class name to be added to the line numbers.\n */\n className?: string;\n };\n\n /**\n * Options for command line.\n */\n commandLine?: {\n /**\n * A class name to be added to the prompt line.\n */\n className?: string;\n };\n\n /**\n * Extra options for the Shiki hast transformer.\n */\n shikiHastOptions?: ShikiHastOptions;\n}\n\n/**\n * A plugin that highlights code blocks using Shiki.\n */\nexport const syntaxHighlightPlugin: Plugin<\n [SyntaxHighlightPluginOptions?],\n HastNode,\n HastNode\n> = (options: SyntaxHighlightPluginOptions = {}) => {\n return async (tree) => {\n const promises: Promise<void>[] = [];\n\n async function visitor(\n node: HastElement,\n index: number,\n parent: HastElement,\n ): Promise<void> {\n if (!parent || index === null || node.tagName !== \"pre\") {\n return;\n }\n\n const codeNode = node.children[0];\n if (\n !codeNode ||\n codeNode.type !== \"element\" ||\n codeNode.tagName !== \"code\" ||\n !codeNode.properties\n ) {\n return;\n }\n\n const lang = getNodeLanguage(codeNode);\n if (typeof lang !== \"string\") {\n return;\n }\n\n const meta = extractCodeMeta(codeNode);\n\n const sourceCode = hastToString(codeNode).trim();\n\n const hastOptions: CodeToHastOptions<BundledLanguage, BundledTheme> = {\n tokenizeTimeLimit: 10_000,\n lang,\n themes: {\n light: \"github-light-default\",\n dark: \"github-dark-default\",\n },\n ...(options.shikiHastOptions ?? {}),\n };\n if (hastOptions.transformers === undefined) {\n hastOptions.transformers = [];\n }\n\n if (options.highlight) {\n hastOptions.transformers.push(\n transformerNotationHighlight({\n ...(options.highlight.transformer ?? {}),\n matchAlgorithm: \"v3\",\n }),\n );\n }\n\n let lineCount = 0;\n hastOptions.transformers.push({\n code(node): void {\n let lineIndex = 0;\n for (const line of node.children.filter(\n (node: HastNode) => node.type === \"element\",\n ) as HastElement[]) {\n line.properties[\"data-line\"] = lineIndex + 1;\n\n if (options.highlight) {\n if (\n meta.highlight &&\n numericRangeContains(meta.highlight.lineRange, lineIndex)\n ) {\n this.addClassToHast(\n line,\n options.highlight.transformer?.classActiveLine ??\n \"highlighted\",\n );\n }\n }\n\n if (\n options.lineNumbers &&\n meta.showLineNumbers &&\n (meta.showLineNumbers.lineRange.length === 0 ||\n numericRangeContains(meta.showLineNumbers.lineRange, lineIndex))\n ) {\n this.addClassToHast(\n line,\n options.lineNumbers.className ?? \"line-number\",\n );\n }\n\n if (\n options.commandLine &&\n meta.commandLine &&\n (meta.commandLine.commandRange.length === 0 ||\n numericRangeContains(meta.commandLine.commandRange, lineIndex))\n ) {\n this.addClassToHast(\n line,\n options.commandLine.className ?? \"line-command\",\n );\n }\n\n lineIndex++;\n }\n\n lineCount = lineIndex;\n\n if (meta.showLineNumbers) {\n if (typeof node.properties.style !== \"string\") {\n node.properties.style = \"\";\n }\n node.properties.style += `--${\n options.lineNumbers?.widthVariableName ?? \"line-number-width\"\n }:${1 + Math.floor(Math.log10(this.lines.length))}ch;`;\n }\n },\n });\n\n try {\n const hast = await codeToHast(sourceCode, hastOptions);\n\n // Insert highlighted code into parent\n const hastPre = hast.children[0] as HastElement;\n\n const properties: Properties = {\n ...hastPre.properties,\n dataFileName: meta.fileName,\n };\n if (options.includeDataAttributes) {\n if (options.includeDataAttributes.includes(\"language\")) {\n properties.dataLanguage = lang;\n }\n if (options.includeDataAttributes.includes(\"source-code\")) {\n properties.dataSourceCode = sourceCode;\n }\n if (options.includeDataAttributes.includes(\"line-count\")) {\n properties.dataLineCount = lineCount;\n }\n }\n\n parent.children.splice(index, 1, {\n ...hastPre,\n properties,\n });\n } catch (error) {\n if (error instanceof ShikiError) {\n if (\n error.message.includes(\n \"Language `math` is not included in this bundle\",\n )\n ) {\n // Skip if the block is math block.\n return;\n }\n }\n throw error;\n }\n }\n\n visit(\n tree,\n \"element\",\n (node: HastElement, index: number, parent: HastElement) => {\n promises.push(visitor(node, index, parent));\n },\n );\n\n await Promise.all(promises);\n\n return tree;\n };\n};\n\nfunction getNodeLanguage(node: HastElement): string | undefined {\n if (!Array.isArray(node.properties.className)) {\n return undefined;\n }\n const prefix = \"language-\";\n const language = node.properties.className.find(\n (name) => typeof name === \"string\" && name.startsWith(prefix),\n ) as string | undefined;\n if (typeof language === \"string\") {\n return language.slice(prefix.length);\n }\n return language;\n}\n"],"mappings":";;;;;;;;;;;AA6EA,MAAa,yBAIR,UAAwC,EAAE,KAAK;AAClD,QAAO,OAAO,SAAS;EACrB,MAAM,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,aAAaA,SAAa,SAAS,CAAC,MAAM;GAEhD,MAAM,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,GAA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AS,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
+ {"version":3,"file":"plugin.mjs","names":["hastToString"],"sources":["../../../src/plugins/syntax-highlight/plugin.ts"],"sourcesContent":["import {\n type TransformerNotationHighlightOptions,\n transformerNotationHighlight,\n} from \"@shikijs/transformers\";\nimport { numericRangeContains } from \"@temelj/iterator\";\nimport type { Properties } from \"hast\";\nimport { toString as hastToString } from \"hast-util-to-string\";\nimport {\n type BundledLanguage,\n type BundledTheme,\n type CodeToHastOptions,\n codeToHast,\n ShikiError,\n} from \"shiki\";\nimport type { Plugin } from \"unified\";\nimport { visit } from \"unist-util-visit\";\n\nimport type { HastElement, HastNode } from \"../../types\";\nimport { extractCodeMeta } from \"./code-meta\";\n\ntype ShikiHastOptions = Partial<\n CodeToHastOptions<BundledLanguage, BundledTheme>\n>;\n\ntype SyntaxDataAttribute = \"source-code\" | \"language\" | \"line-count\";\n\n/**\n * Options for {@linkcode syntaxHighlightPlugin}.\n */\nexport interface SyntaxHighlightPluginOptions {\n /**\n * Include these data attributes on the `<pre>` element.\n * These might be useful to have when building custom code components.\n */\n includeDataAttributes?: SyntaxDataAttribute[];\n\n /**\n * Options for the highlight transformer.\n */\n highlight?: {\n transformer?: TransformerNotationHighlightOptions;\n };\n\n /**\n * Options for line numbers.\n */\n lineNumbers?: {\n /**\n * The name of the variable that contains the line number width.\n * This is used for padding the line numbers so that they align.\n */\n widthVariableName?: string;\n /**\n * A class name to be added to the line numbers.\n */\n className?: string;\n };\n\n /**\n * Options for command line.\n */\n commandLine?: {\n /**\n * A class name to be added to the prompt line.\n */\n className?: string;\n };\n\n /**\n * Extra options for the Shiki hast transformer.\n */\n shikiHastOptions?: ShikiHastOptions;\n}\n\n/**\n * A plugin that highlights code blocks using Shiki.\n */\nexport const syntaxHighlightPlugin: Plugin<\n [SyntaxHighlightPluginOptions?],\n HastNode,\n HastNode\n> = (options: SyntaxHighlightPluginOptions = {}) => {\n return async (tree) => {\n const promises: Promise<void>[] = [];\n\n async function visitor(\n node: HastElement,\n index: number,\n parent: HastElement,\n ): Promise<void> {\n if (!parent || index === null || node.tagName !== \"pre\") {\n return;\n }\n\n const codeNode = node.children[0];\n if (\n !codeNode ||\n codeNode.type !== \"element\" ||\n codeNode.tagName !== \"code\" ||\n !codeNode.properties\n ) {\n return;\n }\n\n const lang = getNodeLanguage(codeNode);\n if (typeof lang !== \"string\") {\n return;\n }\n\n const meta = extractCodeMeta(codeNode);\n\n const sourceCode = hastToString(codeNode).trim();\n\n const hastOptions: CodeToHastOptions<BundledLanguage, BundledTheme> = {\n tokenizeTimeLimit: 10_000,\n lang,\n themes: {\n light: \"github-light-default\",\n dark: \"github-dark-default\",\n },\n ...(options.shikiHastOptions ?? {}),\n };\n if (hastOptions.transformers === undefined) {\n hastOptions.transformers = [];\n }\n\n if (options.highlight) {\n hastOptions.transformers.push(\n transformerNotationHighlight({\n ...(options.highlight.transformer ?? {}),\n matchAlgorithm: \"v3\",\n }),\n );\n }\n\n let lineCount = 0;\n hastOptions.transformers.push({\n code(node): void {\n let lineIndex = 0;\n for (const line of node.children.filter(\n (node: HastNode) => node.type === \"element\",\n ) as HastElement[]) {\n line.properties[\"data-line\"] = lineIndex + 1;\n\n if (options.highlight) {\n if (\n meta.highlight &&\n numericRangeContains(meta.highlight.lineRange, lineIndex)\n ) {\n this.addClassToHast(\n line,\n options.highlight.transformer?.classActiveLine ??\n \"highlighted\",\n );\n }\n }\n\n if (\n options.lineNumbers &&\n meta.showLineNumbers &&\n (meta.showLineNumbers.lineRange.length === 0 ||\n numericRangeContains(meta.showLineNumbers.lineRange, lineIndex))\n ) {\n this.addClassToHast(\n line,\n options.lineNumbers.className ?? \"line-number\",\n );\n }\n\n if (\n options.commandLine &&\n meta.commandLine &&\n (meta.commandLine.commandRange.length === 0 ||\n numericRangeContains(meta.commandLine.commandRange, lineIndex))\n ) {\n this.addClassToHast(\n line,\n options.commandLine.className ?? \"line-command\",\n );\n }\n\n lineIndex++;\n }\n\n lineCount = lineIndex;\n\n if (meta.showLineNumbers) {\n if (typeof node.properties.style !== \"string\") {\n node.properties.style = \"\";\n }\n node.properties.style += `--${\n options.lineNumbers?.widthVariableName ?? \"line-number-width\"\n }:${1 + Math.floor(Math.log10(this.lines.length))}ch;`;\n }\n },\n });\n\n try {\n const hast = await codeToHast(sourceCode, hastOptions);\n\n // Insert highlighted code into parent\n const hastPre = hast.children[0] as HastElement;\n\n const properties: Properties = {\n ...hastPre.properties,\n dataFileName: meta.fileName,\n };\n if (options.includeDataAttributes) {\n if (options.includeDataAttributes.includes(\"language\")) {\n properties.dataLanguage = lang;\n }\n if (options.includeDataAttributes.includes(\"source-code\")) {\n properties.dataSourceCode = sourceCode;\n }\n if (options.includeDataAttributes.includes(\"line-count\")) {\n properties.dataLineCount = lineCount;\n }\n }\n\n parent.children.splice(index, 1, {\n ...hastPre,\n properties,\n });\n } catch (error) {\n if (error instanceof ShikiError) {\n if (\n error.message.includes(\n \"Language `math` is not included in this bundle\",\n )\n ) {\n // Skip if the block is math block.\n return;\n }\n }\n throw error;\n }\n }\n\n visit(\n tree,\n \"element\",\n (node: HastElement, index: number, parent: HastElement) => {\n promises.push(visitor(node, index, parent));\n },\n );\n\n await Promise.all(promises);\n\n return tree;\n };\n};\n\nfunction getNodeLanguage(node: HastElement): string | undefined {\n if (!Array.isArray(node.properties.className)) {\n return undefined;\n }\n const prefix = \"language-\";\n const language = node.properties.className.find(\n (name) => typeof name === \"string\" && name.startsWith(prefix),\n ) as string | undefined;\n if (typeof language === \"string\") {\n return language.slice(prefix.length);\n }\n return language;\n}\n"],"mappings":";;;;;;;;;;AA6EA,MAAa,yBAIR,UAAwC,EAAE,KAAK;AAClD,QAAO,OAAO,SAAS;EACrB,MAAM,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,aAAaA,SAAa,SAAS,CAAC,MAAM;GAEhD,MAAM,cAAgE;IACpE,mBAAmB;IACnB;IACA,QAAQ;KACN,OAAO;KACP,MAAM;KACP;IACD,GAAI,QAAQ,oBAAoB,EAAE;IACnC;AACD,OAAI,YAAY,iBAAiB,KAAA,EAC/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,MAAY;IACf,IAAI,YAAY;AAChB,SAAK,MAAM,QAAQ,KAAK,SAAS,QAC9B,SAAmB,KAAK,SAAS,UACnC,EAAmB;AAClB,UAAK,WAAW,eAAe,YAAY;AAE3C,SAAI,Q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AO,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,5 +1,4 @@
1
1
  import { visit } from "unist-util-visit";
2
-
3
2
  //#region src/plugins/tree-processor/plugin.ts
4
3
  /**
5
4
  * A plugin that allows to process MDX tree nodes.
@@ -16,7 +15,7 @@ const treeProcessorPlugin = ({ process } = { process: () => {} }) => {
16
15
  return tree;
17
16
  };
18
17
  };
19
-
20
18
  //#endregion
21
19
  export { treeProcessorPlugin };
20
+
22
21
  //# sourceMappingURL=plugin.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.mjs","names":[],"sources":["../../../src/plugins/tree-processor/plugin.ts"],"sourcesContent":["import type { Plugin } from \"unified\";\nimport { visit } from \"unist-util-visit\";\n\nimport type { HastElement, HastNode } from \"../../types\";\n\nexport type MdxTreeProcessor = (\n node: HastElement,\n index: number,\n parent: HastElement,\n) => void | Promise<void>;\n\n/**\n * Options for {@linkcode treeProcessorPlugin}.\n */\ntype TreeProcessorPluginOptions = {\n /** The processor. */\n process: MdxTreeProcessor;\n};\n\n/**\n * A plugin that allows to process MDX tree nodes.\n * It can be async or sync.\n */\nexport const treeProcessorPlugin: Plugin<\n [TreeProcessorPluginOptions?],\n HastNode,\n HastNode\n> = ({ process } = { process: () => {} }) => {\n return async (tree) => {\n const promises: Promise<void>[] = [];\n\n visit(\n tree,\n \"element\",\n function visitor(\n node: HastElement,\n index: number,\n parent: HastElement,\n ): void {\n const p = process(node, index, parent);\n if (\n p instanceof Promise ||\n Object.prototype.toString.call(p) === \"[object Promise]\"\n ) {\n promises.push(p as Promise<void>);\n }\n },\n );\n\n await Promise.allSettled(promises);\n\n return tree;\n };\n};\n"],"mappings":";;;;;;;AAuBA,MAAa,uBAIR,EAAE,YAAY,EAAE,eAAe,IAAI,KAAK;AAC3C,QAAO,OAAO,SAAS;EACrB,MAAM,WAA4B,EAAE;AAEpC,QACE,MACA,WACA,SAAS,QACP,MACA,OACA,QACM;GACN,MAAM,IAAI,QAAQ,MAAM,OAAO,OAAO;AACtC,OACE,aAAa,WACb,OAAO,UAAU,SAAS,KAAK,EAAE,KAAK,mBAEtC,UAAS,KAAK,EAAmB;IAGtC;AAED,QAAM,QAAQ,WAAW,SAAS;AAElC,SAAO"}
1
+ {"version":3,"file":"plugin.mjs","names":[],"sources":["../../../src/plugins/tree-processor/plugin.ts"],"sourcesContent":["import type { Plugin } from \"unified\";\nimport { visit } from \"unist-util-visit\";\n\nimport type { HastElement, HastNode } from \"../../types\";\n\nexport type MdxTreeProcessor = (\n node: HastElement,\n index: number,\n parent: HastElement,\n) => void | Promise<void>;\n\n/**\n * Options for {@linkcode treeProcessorPlugin}.\n */\ntype TreeProcessorPluginOptions = {\n /** The processor. */\n process: MdxTreeProcessor;\n};\n\n/**\n * A plugin that allows to process MDX tree nodes.\n * It can be async or sync.\n */\nexport const treeProcessorPlugin: Plugin<\n [TreeProcessorPluginOptions?],\n HastNode,\n HastNode\n> = ({ process } = { process: () => {} }) => {\n return async (tree) => {\n const promises: Promise<void>[] = [];\n\n visit(\n tree,\n \"element\",\n function visitor(\n node: HastElement,\n index: number,\n parent: HastElement,\n ): void {\n const p = process(node, index, parent);\n if (\n p instanceof Promise ||\n Object.prototype.toString.call(p) === \"[object Promise]\"\n ) {\n promises.push(p as Promise<void>);\n }\n },\n );\n\n await Promise.allSettled(promises);\n\n return tree;\n };\n};\n"],"mappings":";;;;;;AAuBA,MAAa,uBAIR,EAAE,YAAY,EAAE,eAAe,IAAI,KAAK;AAC3C,QAAO,OAAO,SAAS;EACrB,MAAM,WAA4B,EAAE;AAEpC,QACE,MACA,WACA,SAAS,QACP,MACA,OACA,QACM;GACN,MAAM,IAAI,QAAQ,MAAM,OAAO,OAAO;AACtC,OACE,aAAa,WACb,OAAO,UAAU,SAAS,KAAK,EAAE,KAAK,mBAEtC,UAAS,KAAK,EAAmB;IAGtC;AAED,QAAM,QAAQ,WAAW,SAAS;AAElC,SAAO"}
package/dist/types.mjs CHANGED
@@ -1 +1 @@
1
- export { };
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@temelj/mdx",
3
- "version": "0.10.2",
3
+ "version": "0.11.1",
4
4
  "description": "MDX processing utilities.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -20,30 +20,32 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "@mdx-js/mdx": "^3.1.1",
23
- "@shikijs/transformers": "^3.22.0",
23
+ "@shikijs/transformers": "^4.0.2",
24
24
  "@types/hast": "^3.0.4",
25
25
  "github-slugger": "^2.0.0",
26
26
  "hast": "^1.0.0",
27
27
  "hast-util-to-string": "^3.0.1",
28
28
  "remark-frontmatter": "^5.0.0",
29
29
  "remark-gfm": "^4.0.1",
30
+ "remark-math": "^6.0.0",
30
31
  "remark-parse": "^11.0.0",
31
- "shiki": "^3.22.0",
32
+ "rehype-katex": "^7.0.1",
33
+ "shiki": "^4.0.2",
32
34
  "unified": "^11.0.5",
33
35
  "unist-util-remove": "^4.0.0",
34
36
  "unist-util-visit": "^5.1.0",
35
37
  "vfile": "^6.0.3",
36
38
  "vfile-matter": "^5.0.1",
37
39
  "zod": "^4.3.6",
38
- "@temelj/iterator": "0.10.2"
40
+ "@temelj/iterator": "0.11.0"
39
41
  },
40
42
  "devDependencies": {
41
43
  "@types/mdast": "^4.0.4",
42
- "@types/node": "25.2.1",
44
+ "@types/node": "25.5.0",
43
45
  "shx": "^0.4.0",
44
- "tsdown": "^0.20.3",
45
- "typescript": "^5.9.3",
46
- "vitest": "^4.0.18"
46
+ "tsdown": "^0.21.7",
47
+ "typescript": "^6.0.2",
48
+ "vitest": "^4.1.2"
47
49
  },
48
50
  "scripts": {
49
51
  "build": "tsdown",