-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastro.config.mts
122 lines (114 loc) · 2.99 KB
/
astro.config.mts
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
// @ts-check
import { defineConfig } from "astro/config";
import tailwind from "@astrojs/tailwind";
import { config } from "./src/config";
function defaultLayoutPlugin() {
return function (_tree: any, file: any) {
const { frontmatter } = file.data.astro;
// default layout
if (!frontmatter.layout) {
frontmatter.layout = "@layouts/post.astro";
}
};
}
function defaultPicPlugin() {
function getFirstImageSrc(node: any): any {
if (node.type === "image" && node.url) {
return node.url;
}
if (node.children) {
for (const child of node.children) {
const src = getFirstImageSrc(child);
if (src) {
return src;
}
}
}
if (node.type === "html") {
const lines = node.value.split("\n");
for (const line of lines) {
const match = line.match(/<img[^>]*src="([^"]*)"/);
if (match) {
return match[1];
}
}
}
return null;
}
return function (tree: any, file: any) {
const { frontmatter } = file.data.astro;
const pic = getFirstImageSrc(tree);
// starts with /images/d && webp using thumb
if (pic && pic.startsWith("/images/d") && pic.endsWith(".webp")) {
frontmatter.pic = pic.replace(".webp", "-thumb.webp");
} else if (pic) {
frontmatter.pic = pic;
} else {
frontmatter.pic = config.site.pic;
}
};
}
function defaultDatePlugin() {
return function (tree: any, file: any) {
const { frontmatter } = file.data.astro;
if (!frontmatter.date) {
frontmatter.date = new Date().toISOString();
}
};
}
function defaultSummaryPlugin() {
return function (tree: any, file: any) {
const { frontmatter } = file.data.astro;
if (!frontmatter.summary) {
// find first text paragraph
for (const node of tree.children) {
if (node.type === "paragraph") {
for (const child of node.children) {
if (child.type === "text") {
frontmatter.summary = child.value;
return;
}
}
}
}
}
};
}
function applyCustomCommentsPlugin() {
return function (tree: any) {
let doPatchNext = ''
for (const node of tree.children) {
if (node.type === "raw" && node.value.startsWith("<!--")) {
doPatchNext = node.value.replace("<!--", "").replace("-->", "").trim()
}
if (node.type === "element" && node.tagName === "p") {
if (doPatchNext.startsWith("+")) {
const addedClass = doPatchNext.replace("+", "")
node.properties = {
...node.properties,
class: addedClass
}
}
doPatchNext = ''
}
}
};
}
// https://astro.build/config
export default defineConfig({
prefetch: true,
markdown: {
remarkPlugins: [
defaultLayoutPlugin,
defaultPicPlugin,
defaultSummaryPlugin,
],
rehypePlugins: [
applyCustomCommentsPlugin,
]
},
vite: {
assetsInclude: ["**/*.HEIC"],
},
integrations: [tailwind()],
});