-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsvelte.config.js
111 lines (96 loc) · 2.96 KB
/
svelte.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
import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
import { codeToHtml } from 'shiki';
import { escapeSvelte, mdsvex } from 'mdsvex';
function returnArrayFromRangeString(rangeString) {
// The range string will be in the format "1-3,5,7-9"
const rangeArray = rangeString.split(',');
const resultArray = [];
rangeArray.forEach((range) => {
const rangeParts = range.split('-');
const start = parseInt(rangeParts[0]);
const end = parseInt(rangeParts[1]) || start;
for (let i = start; i <= end; i++) {
resultArray.push(i);
}
});
return resultArray;
}
async function codeHighlighter(code, langStr) {
let lang = undefined;
let lineHighlight = [];
let diffAdd = [];
let diffRemove = [];
let fileName = 'Terminal';
if (langStr) {
const langArr = langStr?.split('{');
lang = langArr[0];
// args are in the format %h[1,3-4]%ga[5]%gd[6]%f[./hello.html]
if (langArr[1]) {
let args = langArr[1].split('}')[0].split('%');
for (let arg of args) {
if (arg.startsWith('h')) {
const lineRange = arg.split('[')[1].split(']')[0];
lineHighlight = returnArrayFromRangeString(lineRange);
} else if (arg.startsWith('ga')) {
const lineRange = arg.split('[')[1].split(']')[0];
diffAdd = returnArrayFromRangeString(lineRange);
} else if (arg.startsWith('gd')) {
const lineRange = arg.split('[')[1].split(']')[0];
diffRemove = returnArrayFromRangeString(lineRange);
} else if (arg.startsWith('f')) {
fileName = arg.split('[')[1].split(']')[0];
}
}
}
}
const shikiHtml = await codeToHtml(code, {
lang: lang,
themes: {
light: 'one-light',
dark: 'one-dark-pro'
},
transformers: [
{
code(node) {
// add the file name as a property
node.properties['data-file-name'] = fileName;
},
line(node, line) {
node.properties['data-line'] = line;
if (lineHighlight.includes(line)) {
this.addClassToHast(node, 'line-highlight');
}
if (diffAdd.includes(line)) {
this.addClassToHast(node, 'diff-add');
}
if (diffRemove.includes(line)) {
this.addClassToHast(node, 'diff-remove');
}
}
}
]
});
const html = escapeSvelte(shikiHtml);
return `{@html \`${html}\` }`;
}
/** @type {import('mdsvex').MdsvexOptions} */
const mdsvexConfigs = {
highlight: {
highlighter: codeHighlighter
}
};
/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://svelte.dev/docs/kit/integrations
// for more information about preprocessors
preprocess: [vitePreprocess(), mdsvex(mdsvexConfigs)],
kit: {
// adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list.
// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
// See https://svelte.dev/docs/kit/adapters for more information about adapters.
adapter: adapter()
},
extensions: ['.md', '.svx', '.svelte']
};
export default config;