-
Notifications
You must be signed in to change notification settings - Fork 0
/
astro.config.mjs
57 lines (48 loc) · 1.78 KB
/
astro.config.mjs
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
import fs from "fs";
import dayjs from "dayjs";
import tailwind from "@astrojs/tailwind";
import react from "@astrojs/react";
import { defineConfig } from "astro/config";
import { parse } from "node-html-parser";
import { SITE } from "./src/config";
function defaultLayoutPlugin() {
return function (tree, file) {
const filePath = file.history[0];
file.data.astro.frontmatter.layout = "@layouts/post.astro";
// 头图放到文档中的第一行,会自动帮你处理,也可以用 frontmatter 方式,赋值给 pic 字段
if (tree.children[0]?.value && !file.data.astro.frontmatter.pic) {
const imageElement = parse(tree.children[0].value).querySelector("img");
file.data.astro.frontmatter.pic = imageElement.getAttribute("src");
}
// 描述放到文档中头图的下一行,会自动帮你处理,也可以用 frontmatter 方式,赋值给 desc 字段
if (tree.children[1]?.children[1]?.value) {
file.data.astro.frontmatter.desc = tree.children[1].children[1].value;
}
const { date, desc, pic } = file.data.astro.frontmatter;
// 兼容没有描述情况
if (!desc) {
file.data.astro.frontmatter.desc = SITE.description;
}
// 兼容没有头图的情况
if (!pic) {
file.data.astro.frontmatter.pic = SITE.pic;
}
//这里也可以直接在 frontmatter,赋值给 date 字段
if (!date) {
const createDate = dayjs(fs.statSync(filePath).birthtime).format(
"YYYY/MM/DD"
);
file.data.astro.frontmatter.date = createDate;
}
};
}
// https://astro.build/config
export default defineConfig({
site: "https://markyun.github.io/weekly/",
base: "/weekly",
output: "static",
integrations: [react(), tailwind()],
markdown: {
remarkPlugins: [defaultLayoutPlugin],
},
});