-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdsvex.config.js
61 lines (56 loc) · 1.84 KB
/
mdsvex.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
import { defineMDSveXConfig as defineConfig, code_highlighter } from 'mdsvex';
import examples from 'mdsvexamples';
import { visit } from 'unist-util-visit';
function escape(src) {
const res = src.replace(/`/g, '\\`').replace(/\$\{/g, '\\$\\{');
return res;
}
const RE_PARSE_META = /(\w+=\d+|\w+="[^"]*"|\w+=\[[^\]]*\]|\w+)/g;
function parseMeta(meta) {
const result = {};
if (!meta) {
return result;
}
const meta_parts = meta.match(RE_PARSE_META) ?? [];
for (let i = 0; i < meta_parts.length; i++) {
const [key, value = 'true'] = meta_parts[i].split('=');
try {
result[key] = JSON.parse(value);
} catch (/** @type {any} */ e) {
console.error(e);
const error = new Error(`Unable to parse meta \`${key}=${value}\` - ${e.message}`);
error.stack = e.stack;
throw error;
}
}
return result;
}
const mdsvexExtensions = ['.svelte.md', '.md', '.svx'];
const config = defineConfig({
extensions: mdsvexExtensions,
layout: './src/lib-doc/components/ContentLayout.svelte',
highlight: {
highlighter: (code, lang, meta) => {
const highlighted = code_highlighter(code, lang, meta);
const escapedCode = `String.raw\`${escape(code)}\``;
const escapedMeta = escape(JSON.stringify({ ...parseMeta(meta), lang }));
return `<Components.pre
src={${escapedCode}}
meta={${escapedMeta}}>${highlighted}</Components.pre>`;
}
},
remarkPlugins: [
[
examples,
{
defaults: {
Wrapper: '/src/lib-doc/components/CodeWrapper.svelte',
hideStyle: true
}
}
]
],
rehypePlugins: []
});
export default config;
export { mdsvexExtensions };