-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvelte.config.js
205 lines (173 loc) · 5.81 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// import adapter from '@sveltejs/adapter-auto';
import adapter from '@sveltejs/adapter-static';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
import { mdsvex } from 'mdsvex';
import remarkMath from 'remark-math';
import rehypeKatexSvelte from "rehype-katex-svelte";
import { visit } from 'unist-util-visit';
import { toString as hast_toString } from 'hast-util-to-string';
import slugify from 'slugify';
import yaml from 'yaml';
// diabolical.
// import { get_projects_and_blog_data } from './src/lib/utils/markdown';
function createTocPlugin() {
return function(tree) {
const headings = [];
visit(tree, 'element', (node) => {
if (node.tagName.match(/^h[1-6]$/)) {
const level = parseInt(node.tagName.charAt(1));
const text = hast_toString(node);
const id = slugify(text, { lower: true, strict: true });
node.properties = node.properties || {};
node.properties.id = id;
headings.push({ level, text, id });
}
});
let foundScript = false;
visit(tree, 'raw', (node) => {
if (node.value.includes('<script') && !node.value.includes('context="module"') && !foundScript) {
foundScript = true;
node.value = node.value.replace(
'<script>',
'<script>\n\timport TOC_MDSVEX_INTERNAL from "$lib/components/TOC.svelte";'
);
}
if (node.value === '<nav id="toc"></nav>') {
node.value = `<TOC_MDSVEX_INTERNAL headings={${JSON.stringify(headings)}} />`;
}
});
if (!foundScript) {
visit(tree, 'root', (node) => {
node.children.unshift({
type: 'raw',
value: '<script>\n\timport TOC_MDSVEX_INTERNAL from "$lib/components/TOC.svelte";\n</script>'
});
});
}
return tree;
}
}
// function debugRehypePlugin() {
// return function(tree) {
// let scriptCount = 0;
// visit(tree, 'raw', (node) => {
// if (node.value.includes('<script')) {
// scriptCount++;
// console.log(`\nScript #${scriptCount}:`);
// console.log(node.value);
// }
// });
// console.log(`\nTotal script tags found: ${scriptCount}`);
// return tree;
// }
// }
// function debugRemarkPlugin() {
// return function(tree) {
// console.log('\nRemark AST:');
// console.dir(tree, { depth: null });
// return tree;
// }
// }
const hackyBlogPlugin = () => {
return (tree) => {
let injectedProp = false;
visit(tree, 'raw', (node) => {
if (node.value.includes('<script') && !node.value.includes('context="module"') && !injectedProp) {
node.value = node.value.replace(
'<script>',
'<script>\n\tlet __props = $props();'
);
injectedProp = true;
}
if (node.value.includes('<script') && !node.value.includes('context="module"')) {
//
// const { some_new_value, title, description, tags, datePublished, dateModified, author } = metadata;
}
// Replace $$props with __props
node.value = node.value.replace(/\$\$props/g, '__props');
// Replace deprecated context="module" with module attribute
node.value = node.value.replace('<script context="module">', '<script module>');
});
return tree;
};
};
const mdsvex_config = mdsvex({
extensions: ['.md', '.svx'],
// https://mdsvex.com/docs#layouts
// layout: {
// blog: './src/lib/blogpost_layout.svelte',
// project: './src/lib/blogpost_layout.svelte'
// }
layout: './src/lib/blogpost_layout.svelte',
remarkPlugins: [
// debugRemarkPlugin,
remarkMath,
],
rehypePlugins: [
rehypeKatexSvelte,
createTocPlugin,
hackyBlogPlugin,
// debugRehypePlugin,
],
});
const m_old = mdsvex_config.markup;
mdsvex_config.markup = async (param) => {
if (param.filename.endsWith('.md') || param.filename.endsWith('.svx')) {
// const { projects, blogPosts } = await get_projects_and_blog_data();
const { projects, blogPosts } = { projects: [], blogPosts: [] };
const allEntries = [...projects, ...blogPosts];
const frontmatterRegex = /^---\n([\s\S]*?)\n---/;
const match = param.content.match(frontmatterRegex);
if (match) {
const parts = param.filename.split('/');
const routeIndex = parts.indexOf('routes');
if (routeIndex === -1) {
throw new Error('File must be in routes directory');
}
const url = parts.slice(routeIndex + 1, -1).join('/');
// Find matching entry by comparing with url
const matchingEntry = allEntries.find(entry => entry.url === url);
// Debug logging
// console.log('filename:', param.filename);
// console.log('url:', url);
// console.log('all entries:', allEntries.map(entry => ({
// url: entry.url
// })));
// console.log('matchingEntry:', matchingEntry);
if (matchingEntry) {
const newFrontmatter = yaml.stringify(matchingEntry);
param.content = param.content.replace(frontmatterRegex, `---\n${newFrontmatter}---`);
}
}
}
const result = await m_old(param);
if (result) {
result.code = result.code.replaceAll('</script><script>', '');
// console.log("result:", result.code);
}
return result;
};
console.log(" [[[ process.env.NODE_ENV ]]] : ", process.env.NODE_ENV);
/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
// for more information about preprocessors
preprocess: [vitePreprocess(),
mdsvex_config
],
extensions: ['.svelte', '.md', '.svx'],
kit: {
// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
// adapter: adapter()
adapter: adapter({
pages: 'build',
assets: 'build',
fallback: undefined,
precompress: false,
strict: true
})
}
};
export default config;