@temelj/mdx 0.1.5 → 0.1.6

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/esm/compiler.js CHANGED
@@ -49,23 +49,34 @@ export class MdxCompiler {
49
49
  async compile(source, { frontmatterOnly = false, mdxOptions } = {}) {
50
50
  const vfile = new VFile({
51
51
  value: source,
52
- // Needed so vfile doesn't access fs
52
+ // Needed so vfile doesn't need to access fs.
53
53
  cwd: "/",
54
54
  path: "/",
55
55
  message: {},
56
56
  messages: {},
57
57
  });
58
58
  matter(vfile, { strip: true });
59
+ const remarkPlugins = this.mdxOptions.remarkPlugins ?? this.remarkPlugins;
60
+ const rehypePlugins = this.mdxOptions.rehypePlugins ?? this.rehypePlugins;
61
+ if (mdxOptions?.remarkPlugins) {
62
+ remarkPlugins.push(...mdxOptions.remarkPlugins);
63
+ }
64
+ if (mdxOptions?.rehypePlugins) {
65
+ rehypePlugins.push(...mdxOptions.rehypePlugins);
66
+ }
59
67
  let compiled;
60
68
  if (!frontmatterOnly) {
61
69
  compiled = await compileMdxJs(vfile, {
62
- remarkPlugins: this.remarkPlugins,
63
- rehypePlugins: this.rehypePlugins,
64
70
  outputFormat: "function-body",
65
71
  ...(this.mdxOptions ?? {}),
66
72
  ...(mdxOptions ?? {}),
73
+ remarkPlugins,
74
+ rehypePlugins,
67
75
  });
68
76
  }
69
- return { compiled, frontmatter: (vfile.data.matter ?? {}) };
77
+ return {
78
+ compiled: compiled?.value.toString(),
79
+ frontmatter: (vfile.data.matter ?? {}),
80
+ };
70
81
  }
71
82
  }
@@ -44,12 +44,12 @@ export function syntaxHighlightPlugin(options = {}) {
44
44
  if (options.languageClassNamePrefix) {
45
45
  this.addClassToHast(node, `${options.languageClassNamePrefix}${lang}`);
46
46
  }
47
- let lineNumber = 1;
47
+ let lineIndex = 0;
48
48
  for (const line of node.children.filter((node) => node.type === "element")) {
49
- line.properties["data-line"] = lineNumber;
49
+ line.properties["data-line"] = lineIndex + 1;
50
50
  if (options.highlight) {
51
51
  if (meta.highlight &&
52
- numericRangeContains(meta.highlight.lineRange, lineNumber)) {
52
+ numericRangeContains(meta.highlight.lineRange, lineIndex)) {
53
53
  this.addClassToHast(line, options.highlight.transformer?.classActiveLine ??
54
54
  "highlighted");
55
55
  }
@@ -57,15 +57,15 @@ export function syntaxHighlightPlugin(options = {}) {
57
57
  if (options.lineNumbers &&
58
58
  meta.showLineNumbers &&
59
59
  (meta.showLineNumbers.lineRange.length === 0 ||
60
- numericRangeContains(meta.showLineNumbers.lineRange, lineNumber))) {
60
+ numericRangeContains(meta.showLineNumbers.lineRange, lineIndex))) {
61
61
  this.addClassToHast(line, options.lineNumbers.className ?? "line-number");
62
62
  }
63
63
  if (options.commandLine &&
64
64
  meta.commandLine &&
65
- numericRangeContains(meta.commandLine.commandRange, lineNumber)) {
65
+ numericRangeContains(meta.commandLine.commandRange, lineIndex)) {
66
66
  this.addClassToHast(line, options.commandLine.className ?? "line-command");
67
67
  }
68
- lineNumber++;
68
+ lineIndex++;
69
69
  }
70
70
  if (meta.showLineNumbers) {
71
71
  if (typeof node.properties.style !== "string") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@temelj/mdx",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "Utilities for working with MDX.",
5
5
  "keywords": [
6
6
  "mdx"
@@ -24,8 +24,10 @@
24
24
  }
25
25
  },
26
26
  "dependencies": {
27
+ "@temelj/iterator": "^0.2.0",
27
28
  "@mdx-js/mdx": "^3.1.0",
28
29
  "@shikijs/transformers": "^1.23.1",
30
+ "@types/hast": "^3.0.4",
29
31
  "github-slugger": "^2.0.0",
30
32
  "hast": "^1.0.0",
31
33
  "hast-util-to-string": "^3.0.1",
@@ -37,9 +39,7 @@
37
39
  "unist-util-visit": "^5.0.0",
38
40
  "vfile": "^6.0.3",
39
41
  "vfile-matter": "^5.0.0",
40
- "zod": "^3.23.8",
41
- "@temelj/iterator": "^0.2.0",
42
- "@types/hast": "^3.0.4"
42
+ "zod": "^3.23.8"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@types/node": "^20.9.0"
@@ -1,7 +1,6 @@
1
1
  import { type CompileOptions as MdxJsCompileOptions } from "@mdx-js/mdx";
2
2
  import remarkFrontmatterPlugin from "remark-frontmatter";
3
3
  import remarkGfmPlugin from "remark-gfm";
4
- import { VFile } from "vfile";
5
4
  import type { PluginFactory } from "./types.js";
6
5
  export { remarkFrontmatterPlugin, remarkGfmPlugin };
7
6
  export type MdxSource = string | Uint8Array;
@@ -10,14 +9,14 @@ export interface MdxCompileOptions {
10
9
  mdxOptions?: MdxJsCompileOptions;
11
10
  }
12
11
  export interface MdxArtifact<TFrontmatter = Record<string, unknown>> {
13
- compiled?: VFile | undefined;
12
+ compiled?: string | undefined;
14
13
  frontmatter: TFrontmatter;
15
14
  }
16
15
  export declare class MdxCompiler {
17
16
  private readonly mdxOptions;
18
17
  private readonly remarkPlugins;
19
18
  private readonly rehypePlugins;
20
- constructor(mdxOptions?: MdxCompileOptions);
19
+ constructor(mdxOptions?: MdxJsCompileOptions);
21
20
  withRemarkPlugin<TOptions>(plugin: (options?: TOptions) => PluginFactory, options?: TOptions): MdxCompiler;
22
21
  withRehypePlugin<TOptions>(plugin: (options?: TOptions) => PluginFactory, options?: TOptions): MdxCompiler;
23
22
  compile<TFrontmatter = Record<string, unknown>>(source: MdxSource, { frontmatterOnly, mdxOptions }?: MdxCompileOptions): Promise<MdxArtifact<TFrontmatter>>;
@@ -1 +1 @@
1
- {"version":3,"file":"compiler.d.ts","sourceRoot":"","sources":["../src/compiler.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,cAAc,IAAI,mBAAmB,EAC3C,MAAM,aAAa,CAAC;AACrB,OAAO,uBAAuB,MAAM,oBAAoB,CAAC;AACzD,OAAO,eAAe,MAAM,YAAY,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAI9B,OA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