@temelj/mdx 0.1.9 → 0.3.0

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2024 Flinect
3
+ Copyright (c) 2025 Flinect
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/esm/compiler.js CHANGED
@@ -49,7 +49,7 @@ export class MdxCompiler {
49
49
  }
50
50
  return this;
51
51
  }
52
- async compile(source, { frontmatterOnly = false, frontmatterSchema, mdxOptions } = {}) {
52
+ async compile(source, { frontmatterOnly = false, frontmatterSchema, mdxOptions, } = {}) {
53
53
  const vfile = new VFile({
54
54
  value: source,
55
55
  // Needed so vfile doesn't need to access fs.
@@ -59,14 +59,16 @@ export class MdxCompiler {
59
59
  messages: {},
60
60
  });
61
61
  matter(vfile, { strip: true });
62
- const remarkPlugins = this.mdxOptions.remarkPlugins ?? this.remarkPlugins;
63
- const rehypePlugins = this.mdxOptions.rehypePlugins ?? this.rehypePlugins;
64
- if (mdxOptions?.remarkPlugins) {
65
- remarkPlugins.push(...mdxOptions.remarkPlugins);
66
- }
67
- if (mdxOptions?.rehypePlugins) {
68
- rehypePlugins.push(...mdxOptions.rehypePlugins);
69
- }
62
+ const remarkPlugins = [
63
+ ...this.remarkPlugins,
64
+ ...(this.mdxOptions?.remarkPlugins ?? []),
65
+ ...(mdxOptions?.remarkPlugins ?? []),
66
+ ];
67
+ const rehypePlugins = [
68
+ ...this.rehypePlugins,
69
+ ...(this.mdxOptions?.rehypePlugins ?? []),
70
+ ...(mdxOptions?.rehypePlugins ?? []),
71
+ ];
70
72
  let compiled;
71
73
  if (!frontmatterOnly) {
72
74
  const compiledFile = await compileMdxJs(vfile, {
@@ -40,13 +40,14 @@ export function syntaxHighlightPlugin(options = {}) {
40
40
  hastOptions.transformers = [];
41
41
  }
42
42
  if (options.highlight) {
43
- hastOptions.transformers.push(transformerNotationHighlight(options.highlight.transformer));
43
+ hastOptions.transformers.push(transformerNotationHighlight({
44
+ ...(options.highlight.transformer ?? {}),
45
+ matchAlgorithm: "v3",
46
+ }));
44
47
  }
48
+ let lineCount = 0;
45
49
  hastOptions.transformers.push({
46
50
  code(node) {
47
- if (options.languageClassNamePrefix) {
48
- this.addClassToHast(node, `${options.languageClassNamePrefix}${lang}`);
49
- }
50
51
  let lineIndex = 0;
51
52
  for (const line of node.children.filter((node) => node.type === "element")) {
52
53
  line.properties["data-line"] = lineIndex + 1;
@@ -71,6 +72,7 @@ export function syntaxHighlightPlugin(options = {}) {
71
72
  }
72
73
  lineIndex++;
73
74
  }
75
+ lineCount = lineIndex;
74
76
  if (meta.showLineNumbers) {
75
77
  if (typeof node.properties.style !== "string") {
76
78
  node.properties.style = "";
@@ -83,12 +85,24 @@ export function syntaxHighlightPlugin(options = {}) {
83
85
  const hast = await codeToHast(sourceCode, hastOptions);
84
86
  // Insert highlighted code into parent
85
87
  const hastPre = hast.children[0];
88
+ const properties = {
89
+ ...hastPre.properties,
90
+ dataFileName: meta.fileName,
91
+ };
92
+ if (options.includeDataAttributes) {
93
+ if (options.includeDataAttributes.includes("language")) {
94
+ properties.dataLanguage = lang;
95
+ }
96
+ if (options.includeDataAttributes.includes("source-code")) {
97
+ properties.dataSourceCode = sourceCode;
98
+ }
99
+ if (options.includeDataAttributes.includes("line-count")) {
100
+ properties.dataLineCount = lineCount;
101
+ }
102
+ }
86
103
  parent.children.splice(index, 1, {
87
104
  ...hastPre,
88
- properties: {
89
- ...hastPre.properties,
90
- dataFileName: meta.fileName,
91
- },
105
+ properties,
92
106
  });
93
107
  }
94
108
  catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@temelj/mdx",
3
- "version": "0.1.9",
3
+ "version": "0.3.0",
4
4
  "description": "Utilities for working with MDX.",
5
5
  "keywords": [
6
6
  "mdx"
@@ -24,22 +24,22 @@
24
24
  }
25
25
  },
26
26
  "dependencies": {
27
- "@temelj/iterator": "^0.2.0",
27
+ "@temelj/iterator": "^0.2.1",
28
28
  "@mdx-js/mdx": "^3.1.0",
29
- "@shikijs/transformers": "^1.23.1",
29
+ "@shikijs/transformers": "^2.3.0",
30
30
  "@types/hast": "^3.0.4",
31
31
  "github-slugger": "^2.0.0",
32
32
  "hast": "^1.0.0",
33
33
  "hast-util-to-string": "^3.0.1",
34
34
  "remark-frontmatter": "^5.0.0",
35
35
  "remark-gfm": "^4.0.0",
36
- "shiki": "^1.23.1",
36
+ "shiki": "^2.3.0",
37
37
  "unified": "^11.0.5",
38
38
  "unist-util-remove": "^4.0.0",
39
39
  "unist-util-visit": "^5.0.0",
40
40
  "vfile": "^6.0.3",
41
41
  "vfile-matter": "^5.0.0",
42
- "zod": "^3.23.8"
42
+ "zod": "^3.24.1"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@types/node": "^20.9.0"
@@ -34,6 +34,6 @@ export declare class MdxCompiler {
34
34
  constructor(mdxOptions?: MdxJsCompileOptions);
35
35
  withRemarkPlugin<TOptions>(plugin: (options?: TOptions) => PluginFactory, options?: TOptions): MdxCompiler;
36
36
  withRehypePlugin<TOptions>(plugin: (options?: TOptions) => PluginFactory, options?: TOptions): MdxCompiler;
37
- compile<TFrontmatterSchema extends z.ZodSchema>(source: MdxSource, { frontmatterOnly, frontmatterSchema, mdxOptions }?: MdxCompileOptions<TFrontmatterSchema>): Promise<MdxArtifact<z.output<TFrontmatterSchema>>>;
37
+ compile<TFrontmatterSchema extends z.ZodSchema>(source: MdxSource, { frontmatterOnly, frontmatterSchema, mdxOptions, }?: MdxCompileOptions<TFrontmatterSchema>): Promise<MdxArtifact<z.output<TFrontmatterSchema>>>;
38
38
  }
39
39
  //# sourceMappingURL=compiler.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"compiler.d.ts","sourceRoot":"","sources":["../src/compiler.ts"],"names":[],"mappings":"AAAA,OAA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gB;gBAE3B,UAAU,GAAE,mBAAwB;IAMhD,gBAAgB,CAAC,QAAQ,EAC9B,MAAM,EAAE,CAAC,OAAO,CAAC,EAAE,QAAQ,KAAK,aAAa,EAC7C,OAAO,CAAC,EAAE,QAAQ,GACjB,WAAW;IASP,gBAAgB,CAAC,QAAQ,EAC9B,MAAM,EAAE,CAAC,OAAO,CAAC,EAAE,QAAQ,KAAK,aAAa,EAC7C,OAAO,CAAC,EAAE,QAAQ,GACjB,WAAW;IASD,OAAO,CAAC,kBAAkB,SAAS,CAAC,CAAC,SAAS,EACzD,MAAM,EAAE,SAAS,EACjB,EACE,eAAuB,EACvB,iBAAiB,EACjB,UAAU,GACX,GAAE,iBAAiB,CAAC,kBAAkB,CAAM,GAC5C,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,kBAAkB,CAAC,CAAC,CAAC;CA2CtD"}
@@ -2,14 +2,16 @@ import { type BundledLanguage, type BundledTheme, type CodeToHastOptions } from
2
2
  import { type TransformerNotationHighlightOptions } from "@shikijs/transformers";
3
3
  import type { PluginFactory } from "../../types.js";
4
4
  type ShikiHastOptions = Partial<CodeToHastOptions<BundledLanguage, BundledTheme>>;
5
+ type SyntaxDataAttribute = "source-code" | "language" | "line-count";
5
6
  /**
6
7
  * Options for {@linkcode syntaxHighlightPlugin}.
7
8
  */
8
9
  export interface SyntaxHighlightPluginOptions {
9
10
  /**
10
- * A prefix for the language class name on the code tag.
11
+ * Include these data attributes on the `<pre>` element.
12
+ * These might be useful to have when building custom code components.
11
13
  */
12
- languageClassNamePrefix?: string;
14
+ includeDataAttributes?: SyntaxDataAttribute[];
13
15
  /**
14
16
  * Options for the highlight transformer.
15
17
  */
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../src/plugins/syntax-highlight/plugin.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,YAAY,EAEjB,KAAK,iBAAiB,EAEvB,MAAM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