-
Notifications
You must be signed in to change notification settings - Fork 2
/
mdsvex.config.js
90 lines (85 loc) · 2.19 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
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
import { mdsvex, escapeSvelte } from 'mdsvex';
import { visit } from 'unist-util-visit';
import { codeToHtml } from 'shiki';
import { s } from 'hastscript';
import remarkGithubBlockquoteAdmonitions from 'remark-github-beta-blockquote-admonitions';
import remarkUnwrapImages from 'remark-unwrap-images';
import remarkReadingTime from 'mdsvex-reading-time';
import remarkMath from 'remark-math';
import remarkToc from 'remark-toc';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
import rehypeKatex from 'rehype-katex-svelte';
import rehypeSlug from 'rehype-slug';
async function highlighter(code, lang = 'text') {
try {
if (lang === 'mermaid') {
return `<Components.Mermaid>{\`${code.trim().replaceAll('`', '\\`')}\`}</Components.Mermaid>`;
} else {
// Render other codeblocks using Shiki
const html = escapeSvelte(
await codeToHtml(code, {
themes: {
dark: 'catppuccin-mocha',
light: 'catppuccin-latte'
},
defaultColor: false,
lang
})
).replace('shiki shiki-themes', `shiki shiki-themes language-${lang}`);
return `{@html \`${html}\`}`;
}
} catch (e) {
return highlighter(code);
}
}
/** @type {import('mdsvex').MdsvexOptions} */
const mdsvexConfigs = {
extensions: ['.md'],
layout: {
_: './src/lib/components/Blog/Mdsvex.svelte'
},
highlight: { highlighter },
remarkPlugins: [
remarkGithubBlockquoteAdmonitions,
remarkUnwrapImages,
remarkReadingTime,
remarkMath,
remarkToc
],
rehypePlugins: [
rehypeKatex,
rehypeSlug,
[
rehypeAutolinkHeadings,
{
properties: {
class: 'header-link'
},
content: [
s(
'svg.autolink-svg',
{
xmlns: 'http://www.w3.org/2000/svg',
width: 24,
height: 24,
fill: 'none',
stroke: 'currentColor',
'stroke-width': 2,
'stroke-linecap': 'round',
'stroke-linejoin': 'round',
viewBox: '0 0 24 24'
},
s('path', {
d: 'M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71'
}),
s('path', {
d: 'M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71'
})
)
]
}
]
]
};
const mdsvexPreprocess = mdsvex(mdsvexConfigs);
export default mdsvexPreprocess;