-
-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathastro.config.mjs
More file actions
151 lines (135 loc) · 4.1 KB
/
astro.config.mjs
File metadata and controls
151 lines (135 loc) · 4.1 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import fs from "fs";
import dayjs from "dayjs";
import tailwind from "@astrojs/tailwind";
import remarkBreaks from "remark-breaks";
import sitemap from "@astrojs/sitemap";
import { defineConfig } from "astro/config";
import { parse } from "node-html-parser";
import { SITE } from "./src/config";
import rehypeImage from "./rehype-image.js";
// Markdown configuration - controls line break behavior
const markdownConfig = {
hardBreaks: true, // Set to true to enable hard breaks (similar to CMARK_OPT_HARDBREAKS)
gfm: true,
smartypants: true,
allowDangerousHtml: true,
};
const DEFAULT_FORMAT = "YYYY/MM/DD";
const WEEKLY_REPO_NAME = "tw93/weekly";
function formatDate(date) {
return dayjs(date).format(DEFAULT_FORMAT);
}
function getFileCreateDate(filePath) {
return formatDate(fs.statSync(filePath).birthtime);
}
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";
const isEn = filePath.includes("/en/posts/");
const postsDir = isEn ? "/en/posts/" : "/posts/";
const relativePath = filePath
.split(/[\/\\]posts[\/\\]/)[1]
?.replace(/\.md$/, "");
if (relativePath) {
frontmatter.legacySlug = relativePath;
const [numberPart, ...nameParts] = relativePath.split("-");
if (numberPart) {
const numericIndex = Number.parseInt(numberPart, 10);
if (!Number.isNaN(numericIndex)) {
frontmatter.issueNumber = numericIndex;
frontmatter.numericUrl = `${postsDir}${numericIndex}`;
} else {
frontmatter.numericUrl = `${postsDir}${numberPart}`;
}
}
if (nameParts.length > 0) {
frontmatter.issueTitle = decodeURIComponent(nameParts.join("-"));
}
}
if (tree.children[0]?.value && !frontmatter.image) {
const imageElement = parse(tree.children[0].value).querySelector("img");
frontmatter.image = imageElement.getAttribute("src");
}
if (tree.children[1]?.children[1]?.value) {
frontmatter.description = tree.children[1].children[1].value;
}
frontmatter.description = frontmatter.description || SITE.description;
frontmatter.image = frontmatter.image || SITE.siteImage;
// Fallback to file creation time if no date is specified
if (!frontmatter.date) {
frontmatter.date = getFileCreateDate(filePath);
}
if (SITE.repo === WEEKLY_REPO_NAME) {
const postNumber = filePath.split(/[\/\\]posts[\/\\]/)[1]?.split("-")[0];
frontmatter.socialImage = getTwitterImage(postNumber);
}
};
}
export default defineConfig({
site: SITE.homePage,
prefetch: true,
trailingSlash: "never",
server: {
headers: {
"Access-Control-Allow-Origin": "*",
},
},
integrations: [
tailwind(),
sitemap({
changefreq: "weekly",
priority: 0.7,
lastmod: new Date(),
filter: (page) => {
const url = new URL(page);
const postPathMatch = url.pathname.match(/\/posts\/(\d+)-/);
return !postPathMatch;
},
}),
],
i18n: {
defaultLocale: "zh",
locales: ["zh", "en"],
routing: {
prefixDefaultLocale: false,
},
},
markdown: {
remarkPlugins: [
defaultLayoutPlugin,
// Enable hard breaks based on configuration
...(markdownConfig.hardBreaks ? [remarkBreaks] : []),
],
rehypePlugins: [rehypeImage],
remarkRehype: {
handlers: {},
allowDangerousHtml: markdownConfig.allowDangerousHtml,
},
gfm: markdownConfig.gfm,
smartypants: markdownConfig.smartypants,
},
vite: {
server: {
cors: true,
},
build: {
rollupOptions: {
onwarn(warning, warn) {
// Suppress harmless internal Astro import warnings
if (
warning.code === "UNUSED_EXTERNAL_IMPORT" &&
warning.exporter === "@astrojs/internal-helpers/remote"
) {
return;
}
warn(warning);
},
},
},
},
});