forked from tw93/weekly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
astro.config.mjs
69 lines (59 loc) · 2.42 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
58
59
60
61
62
63
64
65
66
67
68
69
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');
//这里特殊处理了下,因为之前的weekly迁移过来后,createDate不对了,通过规律重写了下,100期以后直接读取
if (SITE.repo === 'tw93/weekly') {
const num = filePath.split('/posts/')[1].split('-')[0];
if (num < 100) {
file.data.astro.frontmatter.date = dayjs('2022-10-10')
.subtract(100 - num, 'week')
.format('YYYY/MM/DD');
} else {
file.data.astro.frontmatter.date = createDate;
}
//对于110期以后的,由于原有封面图不支持twitter,这里兼容一下
if (num >= 110) {
file.data.astro.frontmatter.twitterImg = `https://wwww.neijuan.fun/assets/${num}.jpg`;
}
} else {
file.data.astro.frontmatter.date = createDate;
}
}
};
}
// https://astro.build/config
export default defineConfig({
integrations: [react(), tailwind()],
markdown: {
remarkPlugins: [defaultLayoutPlugin],
},
});