-
Notifications
You must be signed in to change notification settings - Fork 1
/
mdsx.config.js
160 lines (151 loc) · 4.35 KB
/
mdsx.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// @ts-check
import remarkGfm from "remark-gfm";
import rehypePrettyCode from "rehype-pretty-code";
import { fileURLToPath } from "url";
import { visit } from "unist-util-visit";
import { resolve } from "path";
import { readFileSync } from "fs";
import { getHighlighter } from "shiki";
import rehypeSlug from "rehype-slug";
import { defineConfig } from "mdsx";
const __dirname = fileURLToPath(new URL(".", import.meta.url));
/**
* @typedef {import('mdast').Root} MdastRoot
* @typedef {import('hast').Root} HastRoot
* @typedef {import('unified').Transformer<HastRoot, HastRoot>} HastTransformer
* @typedef {import('unified').Transformer<MdastRoot, MdastRoot>} MdastTransformer
*/
/**
* @type {import('rehype-pretty-code').Options}
*/
const prettyCodeOptions = {
theme: {
dark: JSON.parse(
String(
readFileSync(
resolve(
__dirname,
"./src/lib/docs/styles/themes/tokyo-night-storm.json",
),
),
),
),
light: JSON.parse(
String(
readFileSync(
resolve(
__dirname,
"./src/lib/docs/styles/themes/tokyo-night-light.json",
),
),
),
),
},
getHighlighter: (options) =>
getHighlighter({
...options,
langs: [
"plaintext",
import("shiki/langs/javascript.mjs"),
import("shiki/langs/typescript.mjs"),
import("shiki/langs/css.mjs"),
import("shiki/langs/svelte.mjs"),
import("shiki/langs/shellscript.mjs"),
import("shiki/langs/markdown.mjs"),
],
}),
keepBackground: false,
onVisitLine(node) {
if (node.children.length === 0) {
// @ts-expect-error - we're changing the node type
node.children = { type: "text", value: " " };
}
},
onVisitHighlightedLine(node) {
node.properties.className = ["line--highlighted"];
},
onVisitHighlightedChars(node) {
node.properties.className = ["chars--highlighted"];
},
};
export const mdsxConfig = defineConfig({
extensions: [".md"],
remarkPlugins: [remarkGfm, remarkRemovePrettierIgnore],
rehypePlugins: [
[rehypePrettyCode, prettyCodeOptions],
rehypeHandleMetadata,
rehypeSlug,
],
blueprints: {
default: {
path: resolve(
__dirname,
"./src/lib/docs/components/markdown/blueprint.svelte",
),
},
},
});
/**
* Removes `<!-- prettier-ignore -->` and `// prettier-ignore` from code blocks
* before they are converted to HTML for syntax highlighting.
*
* We do this because sometimes we want to force a line break in code blocks, but
* prettier removes them, however, we don't want to include the ignore statement
* in the final code block.
*
* One caveat is that if you did want to include the ignore statement in the final
* code block, you'd have to do some hacky stuff like including it in the comment
* itself and checking for it in the code block, but that's not something we need
* at the moment.
*
* @returns {MdastTransformer}
*
*/
function remarkRemovePrettierIgnore() {
return async (tree) => {
visit(tree, "code", (node) => {
node.value = node.value
// @ts-expect-error - not dealing with this rn
.replaceAll("<!-- prettier-ignore -->\n", "")
.replaceAll("// prettier-ignore\n", "");
});
};
}
/**
* Adds `data-metadata` to `<figure>` elements that contain a `<figcaption>`.
* We use this to style elements within the `<figure>` differently if a `<figcaption>`
* is present.
*
* @returns {HastTransformer}
*/
function rehypeHandleMetadata() {
return async (tree) => {
visit(tree, (node) => {
if (node?.type === "element" && node?.tagName === "figure") {
if (!("data-rehype-pretty-code-figure" in node.properties)) {
return;
}
const preElement = node.children.at(-1);
if (
preElement &&
"tagName" in preElement &&
preElement.tagName !== "pre"
) {
return;
}
const firstChild = node.children.at(0);
if (
firstChild &&
"tagName" in firstChild &&
firstChild.tagName === "figcaption"
) {
node.properties["data-metadata"] = "";
const lastChild = node.children.at(-1);
if (lastChild && "properties" in lastChild) {
lastChild.properties["data-metadata"] = "";
}
}
}
});
};
}