@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.
@@ -1,38 +1,40 @@
1
- import { type CompileOptions as MdxJsCompileOptions } from "@mdx-js/mdx";
2
- import remarkFrontmatterPlugin from "remark-frontmatter";
3
- import remarkGfmPlugin from "remark-gfm";
4
- import type { Plugin } from "unified";
5
- import type { z } from "zod";
6
- import type { HastNode } from "./types";
7
- export { remarkFrontmatterPlugin, remarkGfmPlugin };
1
+ import { CompileOptions } from '@mdx-js/mdx';
2
+ export { default as remarkFrontmatterPlugin } from 'remark-frontmatter';
3
+ export { default as remarkGfmPlugin } from 'remark-gfm';
4
+ import { Plugin } from 'unified';
5
+ import { z } from 'zod';
6
+ import { Node } from 'hast';
7
+
8
8
  /**
9
9
  * Represents the source content for an MDX document.
10
10
  */
11
- export type MdxSource = string | Uint8Array;
11
+ type MdxSource = string | Uint8Array;
12
12
  /**
13
13
  * Options for compiling an MDX document.
14
14
  */
15
- export interface MdxCompileOptions {
15
+ interface MdxCompileOptions {
16
16
  frontmatterOnly?: boolean | undefined;
17
- mdxOptions?: MdxJsCompileOptions;
17
+ mdxOptions?: CompileOptions;
18
18
  }
19
19
  /**
20
20
  * The result of compiling an MDX document.
21
21
  * Compiled content is optional, because sometimes we only need the frontmatter.
22
22
  */
23
- export interface MdxArtifact<TFrontmatter = Record<string, unknown>> {
23
+ interface MdxArtifact<TFrontmatter = Record<string, unknown>> {
24
24
  compiled?: string | undefined;
25
25
  frontmatter: TFrontmatter;
26
26
  }
27
27
  /**
28
28
  * A reusable compiler for compiling MDX.
29
29
  */
30
- export declare class MdxCompiler {
30
+ declare class MdxCompiler {
31
31
  private readonly mdxOptions;
32
32
  private readonly remarkPlugins;
33
33
  private readonly rehypePlugins;
34
- constructor(mdxOptions?: MdxJsCompileOptions);
35
- withRemarkPlugin<TOptions>(plugin: Plugin<[TOptions?], HastNode, HastNode>, options?: TOptions): MdxCompiler;
36
- withRehypePlugin<TOptions>(plugin: Plugin<[TOptions?], HastNode, HastNode>, options?: TOptions): MdxCompiler;
34
+ constructor(mdxOptions?: CompileOptions);
35
+ withRemarkPlugin<TOptions>(plugin: Plugin<[TOptions?], Node, Node>, options?: TOptions): MdxCompiler;
36
+ withRehypePlugin<TOptions>(plugin: Plugin<[TOptions?], Node, Node>, options?: TOptions): MdxCompiler;
37
37
  compile<TFrontmatterSchema extends z.ZodSchema>(source: MdxSource, { frontmatterOnly, mdxOptions }?: MdxCompileOptions, frontmatterSchema?: TFrontmatterSchema): Promise<MdxArtifact<z.output<TFrontmatterSchema>>>;
38
38
  }
39
+
40
+ export { type MdxArtifact, type MdxCompileOptions, MdxCompiler, type MdxSource };
package/dist/compiler.js CHANGED
@@ -1,76 +1,75 @@
1
- import { compile as compileMdxJs, } from "@mdx-js/mdx";
1
+ import {
2
+ compile as compileMdxJs
3
+ } from "@mdx-js/mdx";
2
4
  import remarkFrontmatterPlugin from "remark-frontmatter";
3
5
  import remarkGfmPlugin from "remark-gfm";
4
6
  import { VFile } from "vfile";
5
7
  import { matter } from "vfile-matter";
6
- export { remarkFrontmatterPlugin, remarkGfmPlugin };
7
- /**
8
- * A reusable compiler for compiling MDX.
9
- */
10
- export class MdxCompiler {
11
- mdxOptions;
12
- remarkPlugins;
13
- rehypePlugins;
14
- constructor(mdxOptions = {}) {
15
- this.mdxOptions = mdxOptions;
16
- this.remarkPlugins = [remarkFrontmatterPlugin, remarkGfmPlugin];
17
- this.rehypePlugins = [];
8
+ class MdxCompiler {
9
+ mdxOptions;
10
+ remarkPlugins;
11
+ rehypePlugins;
12
+ constructor(mdxOptions = {}) {
13
+ this.mdxOptions = mdxOptions;
14
+ this.remarkPlugins = [remarkFrontmatterPlugin, remarkGfmPlugin];
15
+ this.rehypePlugins = [];
16
+ }
17
+ withRemarkPlugin(plugin, options) {
18
+ if (options === void 0) {
19
+ this.remarkPlugins.push(plugin);
20
+ } else {
21
+ this.remarkPlugins.push([plugin, options]);
18
22
  }
19
- withRemarkPlugin(plugin, options) {
20
- if (options === undefined) {
21
- this.remarkPlugins.push(plugin);
22
- }
23
- else {
24
- this.remarkPlugins.push([plugin, options]);
25
- }
26
- return this;
23
+ return this;
24
+ }
25
+ withRehypePlugin(plugin, options) {
26
+ if (options === void 0) {
27
+ this.rehypePlugins.push(plugin);
28
+ } else {
29
+ this.rehypePlugins.push([plugin, options]);
27
30
  }
28
- withRehypePlugin(plugin, options) {
29
- if (options === undefined) {
30
- this.rehypePlugins.push(plugin);
31
- }
32
- else {
33
- this.rehypePlugins.push([plugin, options]);
34
- }
35
- return this;
36
- }
37
- async compile(source, { frontmatterOnly = false, mdxOptions } = {}, frontmatterSchema) {
38
- const vfile = new VFile({
39
- value: source,
40
- // Needed so vfile doesn't need to access fs.
41
- cwd: "/",
42
- path: "/",
43
- message: {},
44
- messages: {},
45
- });
46
- matter(vfile, { strip: true });
47
- const remarkPlugins = [
48
- ...this.remarkPlugins,
49
- ...(this.mdxOptions?.remarkPlugins ?? []),
50
- ...(mdxOptions?.remarkPlugins ?? []),
51
- ];
52
- const rehypePlugins = [
53
- ...this.rehypePlugins,
54
- ...(this.mdxOptions?.rehypePlugins ?? []),
55
- ...(mdxOptions?.rehypePlugins ?? []),
56
- ];
57
- let compiled;
58
- if (!frontmatterOnly) {
59
- const compiledFile = await compileMdxJs(vfile, {
60
- outputFormat: "function-body",
61
- ...(this.mdxOptions ?? {}),
62
- ...(mdxOptions ?? {}),
63
- remarkPlugins,
64
- rehypePlugins,
65
- });
66
- compiled = compiledFile.value.toString();
67
- }
68
- const frontmatter = frontmatterSchema === undefined
69
- ? (vfile.data.matter ?? {})
70
- : frontmatterSchema.parse(vfile.data.matter ?? {});
71
- return {
72
- compiled,
73
- frontmatter: frontmatter,
74
- };
31
+ return this;
32
+ }
33
+ async compile(source, { frontmatterOnly = false, mdxOptions } = {}, frontmatterSchema) {
34
+ const vfile = new VFile({
35
+ value: source,
36
+ // Needed so vfile doesn't need to access fs.
37
+ cwd: "/",
38
+ path: "/",
39
+ message: {},
40
+ messages: {}
41
+ });
42
+ matter(vfile, { strip: true });
43
+ const remarkPlugins = [
44
+ ...this.remarkPlugins,
45
+ ...this.mdxOptions?.remarkPlugins ?? [],
46
+ ...mdxOptions?.remarkPlugins ?? []
47
+ ];
48
+ const rehypePlugins = [
49
+ ...this.rehypePlugins,
50
+ ...this.mdxOptions?.rehypePlugins ?? [],
51
+ ...mdxOptions?.rehypePlugins ?? []
52
+ ];
53
+ let compiled;
54
+ if (!frontmatterOnly) {
55
+ const compiledFile = await compileMdxJs(vfile, {
56
+ outputFormat: "function-body",
57
+ ...this.mdxOptions ?? {},
58
+ ...mdxOptions ?? {},
59
+ remarkPlugins,
60
+ rehypePlugins
61
+ });
62
+ compiled = compiledFile.value.toString();
75
63
  }
64
+ const frontmatter = frontmatterSchema === void 0 ? vfile.data.matter ?? {} : frontmatterSchema.parse(vfile.data.matter ?? {});
65
+ return {
66
+ compiled,
67
+ frontmatter
68
+ };
69
+ }
76
70
  }
71
+ export {
72
+ MdxCompiler,
73
+ remarkFrontmatterPlugin,
74
+ remarkGfmPlugin
75
+ };
package/dist/mod.d.ts CHANGED
@@ -1,3 +1,13 @@
1
- export * from "./plugins/mod";
2
- export * from "./types";
3
- export * from "./compiler";
1
+ export { MdxArtifact, MdxCompileOptions, MdxCompiler, MdxSource } from './compiler.js';
2
+ export { HeadingIdPluginOptions, headingIdPlugin } from './plugins/heading-id/plugin.js';
3
+ export { SyntaxHighlightPluginOptions, syntaxHighlightPlugin } from './plugins/syntax-highlight/plugin.js';
4
+ export { MdxTreeProcessor, treeProcessorPlugin } from './plugins/tree-processor/plugin.js';
5
+ export { removeImportsExportsPlugin } from './plugins/remove-imports-exports.js';
6
+ export { Element as HastElement, Node as HastNode } from 'hast';
7
+ export { default as remarkFrontmatterPlugin } from 'remark-frontmatter';
8
+ export { default as remarkGfmPlugin } from 'remark-gfm';
9
+ import '@mdx-js/mdx';
10
+ import 'unified';
11
+ import 'zod';
12
+ import 'shiki';
13
+ import '@shikijs/transformers';
package/dist/mod.js CHANGED
@@ -1,3 +1,3 @@
1
+ export * from "./compiler";
1
2
  export * from "./plugins/mod";
2
3
  export * from "./types";
3
- export * from "./compiler";
@@ -1,15 +1,18 @@
1
- import type { Plugin } from "unified";
2
- import type { HastNode } from "../../types";
1
+ import { Plugin } from 'unified';
2
+ import { Node } from 'hast';
3
+
3
4
  /**
4
5
  * Options for {@linkcode headingIdPlugin}.
5
6
  */
6
- export interface HeadingIdPluginOptions {
7
+ interface HeadingIdPluginOptions {
7
8
  /** Prefix for title slugs. */
8
9
  prefix?: string;
9
10
  }
10
11
  /**
11
12
  * A rehype plugin that adds IDs to heading tags.
12
13
  */
13
- export declare const headingIdPlugin: Plugin<[
14
+ declare const headingIdPlugin: Plugin<[
14
15
  HeadingIdPluginOptions?
15
- ], HastNode, HastNode>;
16
+ ], Node, Node>;
17
+
18
+ export { type HeadingIdPluginOptions, headingIdPlugin };
@@ -1,19 +1,22 @@
1
1
  import GithubSlugger from "github-slugger";
2
2
  import { toString as hastToString } from "hast-util-to-string";
3
3
  import { visit } from "unist-util-visit";
4
- /**
5
- * A rehype plugin that adds IDs to heading tags.
6
- */
7
- export const headingIdPlugin = ({ prefix } = {}) => {
8
- const slugger = new GithubSlugger();
9
- return (tree) => {
10
- slugger.reset();
11
- visit(tree, "element", function visitor(node, index, parent) {
12
- if (!parent || index === null || !node.tagName.startsWith("h"))
13
- return;
14
- const id = (prefix ?? "") + slugger.slug(hastToString(node));
15
- node.properties.id = id;
16
- });
17
- return tree;
18
- };
4
+ const headingIdPlugin = ({ prefix } = {}) => {
5
+ const slugger = new GithubSlugger();
6
+ return (tree) => {
7
+ slugger.reset();
8
+ visit(
9
+ tree,
10
+ "element",
11
+ function visitor(node, index, parent) {
12
+ if (!parent || index === null || !node.tagName.startsWith("h")) return;
13
+ const id = (prefix ?? "") + slugger.slug(hastToString(node));
14
+ node.properties.id = id;
15
+ }
16
+ );
17
+ return tree;
18
+ };
19
+ };
20
+ export {
21
+ headingIdPlugin
19
22
  };
@@ -1,4 +1,8 @@
1
- export * from "./heading-id/plugin";
2
- export * from "./syntax-highlight/plugin";
3
- export * from "./tree-processor/plugin";
4
- export * from "./remove-imports-exports";
1
+ export { HeadingIdPluginOptions, headingIdPlugin } from './heading-id/plugin.js';
2
+ export { SyntaxHighlightPluginOptions, syntaxHighlightPlugin } from './syntax-highlight/plugin.js';
3
+ export { MdxTreeProcessor, treeProcessorPlugin } from './tree-processor/plugin.js';
4
+ export { removeImportsExportsPlugin } from './remove-imports-exports.js';
5
+ import 'unified';
6
+ import 'hast';
7
+ import 'shiki';
8
+ import '@shikijs/transformers';
@@ -1,8 +1,11 @@
1
- import type { Plugin } from "unified";
2
- import type { HastNode } from "../types";
1
+ import { Plugin } from 'unified';
2
+ import { Node } from 'hast';
3
+
3
4
  /**
4
5
  * A remark plugin which removes all import and export statements.
5
6
  */
6
- export declare const removeImportsExportsPlugin: Plugin<[
7
+ declare const removeImportsExportsPlugin: Plugin<[
7
8
  unknown
8
- ], HastNode, HastNode>;
9
+ ], Node, Node>;
10
+
11
+ export { removeImportsExportsPlugin };
@@ -1,10 +1,10 @@
1
1
  import { remove } from "unist-util-remove";
2
- /**
3
- * A remark plugin which removes all import and export statements.
4
- */
5
- export const removeImportsExportsPlugin = () => {
6
- return (tree) => {
7
- remove(tree, "mdxjsEsm");
8
- return tree;
9
- };
2
+ const removeImportsExportsPlugin = () => {
3
+ return (tree) => {
4
+ remove(tree, "mdxjsEsm");
5
+ return tree;
6
+ };
7
+ };
8
+ export {
9
+ removeImportsExportsPlugin
10
10
  };
@@ -1,18 +1,21 @@
1
- import { type NumericRange } from "@temelj/iterator";
2
- import type { HastElement } from "../../types";
3
- export interface MdxCodeMeta {
1
+ import { NumericRange } from '@temelj/iterator';
2
+ import { Element } from 'hast';
3
+
4
+ interface MdxCodeMeta {
4
5
  highlight?: MdxCodeMetaHighlightLine;
5
6
  showLineNumbers?: MdxCodeMetaLineNumbers;
6
7
  commandLine?: MdxCodeMetaCommandLine;
7
8
  fileName?: string;
8
9
  }
9
- export type MdxCodeMetaHighlightLine = {
10
+ type MdxCodeMetaHighlightLine = {
10
11
  lineRange: NumericRange;
11
12
  };
12
- export type MdxCodeMetaLineNumbers = {
13
+ type MdxCodeMetaLineNumbers = {
13
14
  lineRange: NumericRange;
14
15
  };
15
- export type MdxCodeMetaCommandLine = {
16
+ type MdxCodeMetaCommandLine = {
16
17
  commandRange: NumericRange;
17
18
  };
18
- export declare function extractCodeMeta(node: HastElement): MdxCodeMeta;
19
+ declare function extractCodeMeta(node: Element): MdxCodeMeta;
20
+
21
+ export { type MdxCodeMeta, type MdxCodeMetaCommandLine, type MdxCodeMetaHighlightLine, type MdxCodeMetaLineNumbers, extractCodeMeta };
@@ -1,50 +1,50 @@
1
1
  import { parseNumericRange } from "@temelj/iterator";
2
2
  import { z } from "zod";
3
- export function extractCodeMeta(node) {
4
- const meta = node?.data?.meta ?? node?.properties?.metastring ?? "";
5
- return parseMetaString(meta);
3
+ function extractCodeMeta(node) {
4
+ const meta = node?.data?.meta ?? node?.properties?.metastring ?? "";
5
+ return parseMetaString(meta);
6
6
  }
7
7
  const metaSchema = z.object({
8
- highlight: z.optional(z.string()),
9
- showLineNumbers: z.optional(z.union([z.boolean(), z.string()])),
10
- commandLine: z.optional(z.union([z.boolean(), z.string()])),
11
- fileName: z.optional(z.string()),
8
+ highlight: z.optional(z.string()),
9
+ showLineNumbers: z.optional(z.union([z.boolean(), z.string()])),
10
+ commandLine: z.optional(z.union([z.boolean(), z.string()])),
11
+ fileName: z.optional(z.string())
12
12
  });
13
13
  function parseMetaString(meta) {
14
- let raw;
15
- try {
16
- raw = JSON.parse(meta.trim());
14
+ let raw;
15
+ try {
16
+ raw = JSON.parse(meta.trim());
17
+ } catch {
18
+ return {};
19
+ }
20
+ const parsed = metaSchema.parse(raw);
21
+ const options = {};
22
+ if (parsed.highlight !== void 0) {
23
+ options.highlight = { lineRange: parseNumericRange(parsed.highlight) };
24
+ }
25
+ if (parsed.showLineNumbers !== void 0) {
26
+ if (typeof parsed.showLineNumbers === "string") {
27
+ options.showLineNumbers = {
28
+ lineRange: parseNumericRange(parsed.showLineNumbers)
29
+ };
30
+ } else if (parsed.showLineNumbers) {
31
+ options.showLineNumbers = { lineRange: [] };
17
32
  }
18
- catch {
19
- return {};
33
+ }
34
+ if (parsed.commandLine !== void 0) {
35
+ if (typeof parsed.commandLine === "string") {
36
+ options.commandLine = {
37
+ commandRange: parseNumericRange(parsed.commandLine)
38
+ };
39
+ } else if (parsed.commandLine) {
40
+ options.commandLine = { commandRange: [] };
20
41
  }
21
- const parsed = metaSchema.parse(raw);
22
- const options = {};
23
- if (parsed.highlight !== undefined) {
24
- options.highlight = { lineRange: parseNumericRange(parsed.highlight) };
25
- }
26
- if (parsed.showLineNumbers !== undefined) {
27
- if (typeof parsed.showLineNumbers === "string") {
28
- options.showLineNumbers = {
29
- lineRange: parseNumericRange(parsed.showLineNumbers),
30
- };
31
- }
32
- else if (parsed.showLineNumbers) {
33
- options.showLineNumbers = { lineRange: [] };
34
- }
35
- }
36
- if (parsed.commandLine !== undefined) {
37
- if (typeof parsed.commandLine === "string") {
38
- options.commandLine = {
39
- commandRange: parseNumericRange(parsed.commandLine),
40
- };
41
- }
42
- else if (parsed.commandLine) {
43
- options.commandLine = { commandRange: [] };
44
- }
45
- }
46
- if (parsed.fileName !== undefined) {
47
- options.fileName = parsed.fileName.trim();
48
- }
49
- return options;
42
+ }
43
+ if (parsed.fileName !== void 0) {
44
+ options.fileName = parsed.fileName.trim();
45
+ }
46
+ return options;
50
47
  }
48
+ export {
49
+ extractCodeMeta
50
+ };
@@ -1,13 +1,14 @@
1
- import { type BundledLanguage, type BundledTheme, type CodeToHastOptions } from "shiki";
2
- import { type TransformerNotationHighlightOptions } from "@shikijs/transformers";
3
- import type { Plugin } from "unified";
4
- import type { HastNode } from "../../types";
1
+ import { CodeToHastOptions, BundledLanguage, BundledTheme } from 'shiki';
2
+ import { TransformerNotationHighlightOptions } from '@shikijs/transformers';
3
+ import { Plugin } from 'unified';
4
+ import { Node } from 'hast';
5
+
5
6
  type ShikiHastOptions = Partial<CodeToHastOptions<BundledLanguage, BundledTheme>>;
6
7
  type SyntaxDataAttribute = "source-code" | "language" | "line-count";
7
8
  /**
8
9
  * Options for {@linkcode syntaxHighlightPlugin}.
9
10
  */
10
- export interface SyntaxHighlightPluginOptions {
11
+ interface SyntaxHighlightPluginOptions {
11
12
  /**
12
13
  * Include these data attributes on the `<pre>` element.
13
14
  * These might be useful to have when building custom code components.
@@ -50,7 +51,8 @@ export interface SyntaxHighlightPluginOptions {
50
51
  /**
51
52
  * A plugin that highlights code blocks using Shiki.
52
53
  */
53
- export declare const syntaxHighlightPlugin: Plugin<[
54
+ declare const syntaxHighlightPlugin: Plugin<[
54
55
  SyntaxHighlightPluginOptions?
55
- ], HastNode, HastNode>;
56
- export {};
56
+ ], Node, Node>;
57
+
58
+ export { type SyntaxHighlightPluginOptions, syntaxHighlightPlugin };