-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
astro.config.mjs
71 lines (58 loc) · 1.95 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
70
71
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';
const START_DATE = '2022-10-10';
function formatDate(date) {
return dayjs(date).format(DEFAULT_FORMAT);
}
function getFileCreateDate(filePath) {
return formatDate(fs.statSync(filePath).birthtime);
}
function getWeeklyDate(num) {
return num < 100
? formatDate(dayjs(START_DATE).subtract(100 - num, 'week'))
: getFileCreateDate(filePath);
}
function getTwitterImage(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) {
const postNumber = filePath.split('/posts/')[1].split('-')[0];
frontmatter.date = SITE.repo === WEEKLY_REPO_NAME
? getWeeklyDate(postNumber)
: getFileCreateDate(filePath);
}
if (SITE.repo === WEEKLY_REPO_NAME) {
const postNumber = filePath.split('/posts/')[1].split('-')[0];
frontmatter.twitterImg = getTwitterImage(postNumber);
}
};
}
export default defineConfig({
prefetch: true,
integrations: [tailwind()],
markdown: {
remarkPlugins: [defaultLayoutPlugin],
rehypePlugins: [rehypeCustomizeImageSrc],
},
});