@temelj/mdx 0.1.8 → 0.2.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,5 +1,5 @@
|
|
|
1
1
|
import { toString as hastToString } from "hast-util-to-string";
|
|
2
|
-
import { codeToHast, } from "shiki";
|
|
2
|
+
import { codeToHast, ShikiError, } from "shiki";
|
|
3
3
|
import { visit } from "unist-util-visit";
|
|
4
4
|
import { transformerNotationHighlight, } from "@shikijs/transformers";
|
|
5
5
|
import { numericRangeContains } from "@temelj/iterator";
|
|
@@ -42,11 +42,9 @@ export function syntaxHighlightPlugin(options = {}) {
|
|
|
42
42
|
if (options.highlight) {
|
|
43
43
|
hastOptions.transformers.push(transformerNotationHighlight(options.highlight.transformer));
|
|
44
44
|
}
|
|
45
|
+
let lineCount = 0;
|
|
45
46
|
hastOptions.transformers.push({
|
|
46
47
|
code(node) {
|
|
47
|
-
if (options.languageClassNamePrefix) {
|
|
48
|
-
this.addClassToHast(node, `${options.languageClassNamePrefix}${lang}`);
|
|
49
|
-
}
|
|
50
48
|
let lineIndex = 0;
|
|
51
49
|
for (const line of node.children.filter((node) => node.type === "element")) {
|
|
52
50
|
line.properties["data-line"] = lineIndex + 1;
|
|
@@ -71,6 +69,7 @@ export function syntaxHighlightPlugin(options = {}) {
|
|
|
71
69
|
}
|
|
72
70
|
lineIndex++;
|
|
73
71
|
}
|
|
72
|
+
lineCount = lineIndex;
|
|
74
73
|
if (meta.showLineNumbers) {
|
|
75
74
|
if (typeof node.properties.style !== "string") {
|
|
76
75
|
node.properties.style = "";
|
|
@@ -79,16 +78,39 @@ export function syntaxHighlightPlugin(options = {}) {
|
|
|
79
78
|
}
|
|
80
79
|
},
|
|
81
80
|
});
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
properties: {
|
|
81
|
+
try {
|
|
82
|
+
const hast = await codeToHast(sourceCode, hastOptions);
|
|
83
|
+
// Insert highlighted code into parent
|
|
84
|
+
const hastPre = hast.children[0];
|
|
85
|
+
const properties = {
|
|
88
86
|
...hastPre.properties,
|
|
89
87
|
dataFileName: meta.fileName,
|
|
90
|
-
}
|
|
91
|
-
|
|
88
|
+
};
|
|
89
|
+
if (options.includeDataAttributes) {
|
|
90
|
+
if (options.includeDataAttributes.includes("language")) {
|
|
91
|
+
properties.dataLanguage = lang;
|
|
92
|
+
}
|
|
93
|
+
if (options.includeDataAttributes.includes("source-code")) {
|
|
94
|
+
properties.dataSourceCode = sourceCode;
|
|
95
|
+
}
|
|
96
|
+
if (options.includeDataAttributes.includes("line-count")) {
|
|
97
|
+
properties.dataLineCount = lineCount;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
parent.children.splice(index, 1, {
|
|
101
|
+
...hastPre,
|
|
102
|
+
properties,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
if (error instanceof ShikiError) {
|
|
107
|
+
if (error.message.includes("Language `math` is not included in this bundle")) {
|
|
108
|
+
// Skip if the block is math block.
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
92
114
|
}
|
|
93
115
|
visit(tree, "element", (node, index, parent) => {
|
|
94
116
|
promises.push(visitor(node, index, parent));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@temelj/mdx",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Utilities for working with MDX.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mdx"
|
|
@@ -26,20 +26,20 @@
|
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@temelj/iterator": "^0.2.0",
|
|
28
28
|
"@mdx-js/mdx": "^3.1.0",
|
|
29
|
-
"@shikijs/transformers": "^1.
|
|
29
|
+
"@shikijs/transformers": "^1.27.2",
|
|
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.
|
|
36
|
+
"shiki": "^1.27.2",
|
|
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.
|
|
42
|
+
"zod": "^3.24.1"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@types/node": "^20.9.0"
|
|
@@ -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
|
-
*
|
|
11
|
+
* Include these data attributes on the `<pre>` element.
|
|
12
|
+
* These might be useful to have when building custom code components.
|
|
11
13
|
*/
|
|
12
|
-
|
|
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,
|
|
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,OAAO,CAAC;AAEf,OAAO,EAEL,KAAK,mCAAmC,EACzC,MAAM,uBAAuB,CAAC;AAG/B,OAAO,KAAK,EAAyB,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAG3E,KAAK,gBAAgB,GAAG,OAAO,CAC7B,iBAAiB,CAAC,eAAe,EAAE,YAAY,CAAC,CACjD,CAAC;AAEF,KAAK,mBAAmB,GAAG,aAAa,GAAG,UAAU,GAAG,YAAY,CAAC;AAErE;;GAEG;AACH,MAAM,WAAW,4BAA4B;IAC3C;;;OAGG;IACH,qBAAqB,CAAC,EAAE,mBAAmB,EAAE,CAAC;IAE9C;;OAEG;IACH,SAAS,CAAC,EAAE;QACV,WAAW,CAAC,EAAE,mCAAmC,CAAC;KACnD,CAAC;IAEF;;OAEG;IACH,WAAW,CAAC,EAAE;QACZ;;;WAGG;QACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;QAC3B;;WAEG;QACH,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,CAAC;IAEF;;OAEG;IACH,WAAW,CAAC,EAAE;QACZ;;WAEG;QACH,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,CAAC;IAEF;;OAEG;IACH,gBAAgB,CAAC,EAAE,gBAAgB,CAAC;CACrC;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,GAAE,4BAAiC,GACzC,aAAa,CAwKf"}
|