-
Notifications
You must be signed in to change notification settings - Fork 0
/
astro.config.ts
158 lines (150 loc) · 3.54 KB
/
astro.config.ts
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
import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';
import mdx from '@astrojs/mdx';
import robotsTxt from 'astro-robots-txt';
// plugin in separate file don't work 🤷
import type { Plugin } from 'unified';
import type { Root } from 'mdast';
import { createCssVariablesTheme, createHighlighter, getHighlighter } from 'shiki';
import type { Highlighter, BuiltinLanguage } from 'shiki';
import { visit } from 'unist-util-visit';
import playformCompress from '@playform/compress';
const SUPPORTED_LANGS = [
'javascript',
'typescript',
'tsx',
'jsx',
'rust',
'json',
'toml',
'md',
'mdx',
'astro',
'sh',
] as const satisfies BuiltinLanguage[];
type BlockParams = {
file?: string;
noBadge?: boolean;
};
const myTheme = createCssVariablesTheme({
name: 'css-variables',
variablePrefix: '--shiki-',
variableDefaults: {},
fontStyle: true,
});
let highlighterCache: Promise<Highlighter>;
const parseLangRe = /(?=(?:file=(?<file>\S*))?)(?=(?:noBadge=(?<noBadge>true|false))?)/g;
export const remarkCustomCodeBlock: () => Promise<Plugin<any[], Root>> = async () => {
if (!highlighterCache)
highlighterCache = createHighlighter({
themes: [myTheme],
langs: SUPPORTED_LANGS,
});
const highlighter = await highlighterCache;
return () => (tree) => {
visit(tree, 'code', (node) => {
const rawLang = node.lang ?? 'plaintext';
const metaMatch = parseLangRe.exec(node.meta ?? '');
if (!metaMatch) {
return console.warn(
'Meta section of code fence should match format <file (optional)> <noBadge (true or false, optional)',
);
}
const { file, noBadge = false } = metaMatch.groups as unknown as BlockParams;
const langColorMap: {
[K in BuiltinLanguage]?: {
bg: string;
fg: string;
};
} = {
css: {
bg: '#264de4',
fg: '#fff',
},
ts: {
bg: '#007acc',
fg: '#fff',
},
tsx: {
bg: '#007acc',
fg: '#fff',
},
java: {
bg: '#ed8b00',
fg: '#fff',
},
rust: {
bg: '#dea584',
fg: '#fff',
},
astro: {
bg: '#ff5a03',
fg: '#fff',
},
toml: {
bg: '#9c4221',
fg: '#fff',
},
};
const lang: BuiltinLanguage = (() => {
const lang = rawLang as BuiltinLanguage;
if (lang === 'typescript') return 'ts';
if (lang === 'javascript') return 'js';
if (lang === 'rs') return 'rust';
return lang;
})();
let codeHtml: string = highlighter.codeToHtml(node.value, {
lang,
theme: 'css-variables',
});
if (file) {
codeHtml = codeHtml.replace(`shiki`, `shiki shiki-with-file-name`);
}
const html = `
<div class='shiki-wrapper'>
${file ? `<div class='code-file-name'>${file}</div>` : ''}
<div style='position:relative'>
${
!noBadge
? `<div
class='language-badge'
style='background-color:${langColorMap[lang]?.bg ?? '#888'};color:${langColorMap[lang]?.fg ?? '#fff'}'
>
${lang.toUpperCase()}
</div>`
: ''
}
${codeHtml}
</div>
</div>
`.trim();
node.value = html;
(node as any).type = 'html';
(node as any).children = [];
});
};
};
// https://astro.build/config
export default defineConfig({
site: 'https://blog.igamble.dev',
vite: {
server: {
watch: {
ignored: ['**/node_modules/**', '**/api/**'],
},
},
},
// Enable Preact to support Preact JSX components.
integrations: [
sitemap(),
mdx(),
robotsTxt({
sitemap: true,
}),
playformCompress(),
],
markdown: {
syntaxHighlight: false,
remarkPlugins: [await remarkCustomCodeBlock()],
},
});