-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
astro.config.mjs
68 lines (56 loc) · 1.85 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
import fs from 'fs';
import dayjs from 'dayjs';
import tailwind from '@astrojs/tailwind';
import { defineConfig } from 'astro/config';
import { parse } from 'node-html-parser';
import { SITE } from './src/config';
import rehypeCustomizeImageSrc from './rehype-customize-image-src.js';
const DEFAULT_FORMAT = 'YYYY/MM/DD';
const WEEKLY_REPO_NAME = 'tw93/weekly';
function getCreateDateFormat(filePath) {
return dayjs(fs.statSync(filePath).birthtime).format(DEFAULT_FORMAT);
}
function getWeeklyDateFormat(num) {
if (num < 100) {
return dayjs('2022-10-10')
.subtract(100 - num, 'week')
.format(DEFAULT_FORMAT);
}
return getCreateDateFormat(filePath);
}
function getTwitterImg(num) {
return num >= 110 ? `https://weekly.tw93.fun/assets/${num}.jpg` : undefined;
}
function defaultLayoutPlugin() {
return function (tree, file) {
const filePath = file.history[0];
const { frontmatter } = file.data.astro;
frontmatter.layout = '@layouts/post.astro';
if (tree.children[0]?.value && !frontmatter.pic) {
const imageElement = parse(tree.children[0].value).querySelector('img');
frontmatter.pic = imageElement.getAttribute('src');
}
if (tree.children[1]?.children[1]?.value) {
frontmatter.desc = tree.children[1].children[1].value;
}
frontmatter.desc = frontmatter.desc || SITE.description;
frontmatter.pic = frontmatter.pic || SITE.pic;
if (!frontmatter.date) {
frontmatter.date =
SITE.repo === WEEKLY_REPO_NAME
? getWeeklyDateFormat(filePath.split('/posts/')[1].split('-')[0])
: getCreateDateFormat(filePath);
}
if (SITE.repo === WEEKLY_REPO_NAME) {
frontmatter.twitterImg = getTwitterImg(filePath.split('/posts/')[1].split('-')[0]);
}
};
}
export default defineConfig({
prefetch: true,
integrations: [tailwind()],
markdown: {
remarkPlugins: [defaultLayoutPlugin],
rehypePlugins: [rehypeCustomizeImageSrc],
},
});