newechoes/astro.config.mjs

141 lines
3.7 KiB
JavaScript
Raw Normal View History

2025-02-24 16:18:36 +08:00
// @ts-check
import { defineConfig } from "astro/config";
2025-02-24 16:18:36 +08:00
import tailwindcss from "@tailwindcss/vite";
import mdx from "@astrojs/mdx";
import react from "@astrojs/react";
import remarkEmoji from "remark-emoji";
import rehypeExternalLinks from "rehype-external-links";
import sitemap from "@astrojs/sitemap";
import fs from "node:fs";
import path from "node:path";
import swup from "@swup/astro"
import { SITE_URL } from "./src/consts";
import compressor from "astro-compressor";
import vercel from "@astrojs/vercel";
import { articleIndexerIntegration } from "./src/plugins/build-article-index.js";
2025-05-04 03:44:10 +08:00
import { rehypeCodeBlocks } from "./src/plugins/rehype-code-blocks.js";
import { rehypeTables } from "./src/plugins/rehype-tables.js";
2025-03-08 18:16:42 +08:00
function getArticleDate(articleId) {
try {
2025-03-10 21:12:16 +08:00
// 处理多级目录的文章路径
const mdPath = path.join(process.cwd(), "src/content", articleId + ".md");
const mdxPath = path.join(process.cwd(), "src/content", articleId + ".mdx");
2025-03-10 21:12:16 +08:00
let filePath = fs.existsSync(mdPath) ? mdPath : mdxPath;
2025-03-10 21:12:16 +08:00
if (fs.existsSync(filePath)) {
const content = fs.readFileSync(filePath, "utf-8");
2025-03-08 18:16:42 +08:00
const match = content.match(/date:\s*(\d{4}-\d{2}-\d{2})/);
if (match) {
return new Date(match[1]).toISOString();
}
}
} catch (error) {
console.error("Error reading article date:", error);
2025-03-08 18:16:42 +08:00
}
2025-03-10 21:12:16 +08:00
return new Date().toISOString(); // 如果没有日期,返回当前时间
2025-03-08 18:16:42 +08:00
}
2025-03-03 21:16:16 +08:00
2025-02-24 16:18:36 +08:00
// https://astro.build/config
2025-03-03 21:16:16 +08:00
export default defineConfig({
2025-03-09 14:37:44 +08:00
site: SITE_URL,
output: "static",
trailingSlash: "ignore",
2025-03-09 14:37:44 +08:00
2025-03-08 18:16:42 +08:00
build: {
format: "directory",
2025-03-08 18:16:42 +08:00
},
2025-03-09 14:37:44 +08:00
2025-03-03 21:16:16 +08:00
vite: {
plugins: [tailwindcss()],
2025-03-03 21:16:16 +08:00
},
2025-03-08 18:16:42 +08:00
integrations: [
// 使用Astro官方的MDX支持
mdx(),
swup({
cache: true,
preload: true,
}),
2025-03-08 18:16:42 +08:00
react(),
// 使用文章索引生成器
articleIndexerIntegration(),
2025-03-08 18:16:42 +08:00
sitemap({
filter: (page) => !page.includes("/api/"),
2025-03-08 18:16:42 +08:00
serialize(item) {
if (!item) return undefined;
// 文章页面
if (item.url.includes("/articles/")) {
2025-03-08 18:16:42 +08:00
// 从 URL 中提取文章 ID
const articleId = item.url
.replace(SITE_URL + "/articles/", "")
.replace(/\/$/, "");
2025-03-08 18:16:42 +08:00
const publishDate = getArticleDate(articleId);
2025-03-10 21:12:16 +08:00
return {
...item,
priority: 0.8,
lastmod: publishDate,
2025-03-10 21:12:16 +08:00
};
2025-03-08 18:16:42 +08:00
}
// 其他页面
else {
let priority = 0.7; // 默认优先级
2025-03-08 18:16:42 +08:00
// 首页最高优先级
if (item.url === SITE_URL + "/") {
2025-03-08 18:16:42 +08:00
priority = 1.0;
}
// 文章列表页次高优先级
else if (item.url === SITE_URL + "/articles/") {
2025-03-08 18:16:42 +08:00
priority = 0.9;
}
return {
...item,
priority,
2025-03-08 18:16:42 +08:00
};
}
},
}),
// 添加压缩插件 (必须放在最后位置)
compressor()
2025-03-08 18:16:42 +08:00
],
2025-03-09 14:37:44 +08:00
// Markdown 配置 - 使用官方语法高亮
2025-03-03 21:16:16 +08:00
markdown: {
// 配置语法高亮
syntaxHighlight: {
// 使用shiki作为高亮器
type: 'shiki',
// 排除mermaid语言不进行高亮处理
excludeLangs: ['mermaid']
},
// Shiki主题配置
shikiConfig: {
2025-05-04 03:44:10 +08:00
// 默认主题 - 必须设置,但最终会被替换为 light/dark 主题
theme: 'github-light',
2025-05-04 03:44:10 +08:00
// 定义明亮和暗黑主题
themes: {
light: 'github-light',
dark: 'github-dark'
},
2025-05-04 03:44:10 +08:00
// 启用代码换行
wrap: true
},
2025-03-03 21:16:16 +08:00
remarkPlugins: [
2025-03-29 16:09:16 +08:00
[remarkEmoji, { emoticon: false, padded: true }]
2025-03-03 21:16:16 +08:00
],
rehypePlugins: [
2025-05-04 03:44:10 +08:00
[rehypeExternalLinks, { target: '_blank', rel: ['nofollow', 'noopener', 'noreferrer'] }],
rehypeCodeBlocks,
rehypeTables
2025-03-03 21:16:16 +08:00
],
gfm: true,
2025-03-09 14:37:44 +08:00
},
adapter: vercel(),
});