Commit 4d043eb

HPCesia <me@hpcesia.com>
2026-03-16 12:02:22
Use Astro font API for common fonts
1 parent 3ce5a51
Changed files (3)
src/layouts/GlobalLayout.astro
@@ -4,6 +4,7 @@ import Footer from "@/components/Footer.astro";
 import Header from "@/components/Header.astro";
 import { SITE_CONFIG } from "@/config";
 import { url } from "@/utils/url";
+import { Font } from "astro:assets";
 
 interface Props {
   title?: string;
@@ -52,6 +53,7 @@ const {
     />
     <link rel="sitemap" href={url("/sitemap-index.xml")} />
 
+    <Font cssVariable="--font-libertinus-serif" preload />
     <link
       rel="stylesheet"
       href="https://fred-wang.github.io/MathFonts/NewComputerModern/mathfonts.css"
src/styles/global.css
@@ -2,7 +2,7 @@
 @import "./article/index.css";
 
 @theme {
-  --font-serif: "Libertinus Serif", ui-serif, serif;
+  --font-serif: var(--font-libertinus-serif), ui-serif, serif;
 }
 
 :root {
@@ -14,27 +14,27 @@
   }
   & :lang("zh-CN") {
     --font-serif:
-      "Libertinus Serif", "Noto Serif SC", "Source Han Serif SC",
+      var(--font-libertinus-serif), "Noto Serif SC", "Source Han Serif SC",
       "Source Han Serif", ui-serif, serif;
   }
   & :lang("zh-TW") {
     --font-serif:
-      "Libertinus Serif", "Noto Serif TC", "Source Han Serif TC",
+      var(--font-libertinus-serif), "Noto Serif TC", "Source Han Serif TC",
       "Source Han Serif", ui-serif, serif;
   }
   & :lang("zh-HK") {
     --font-serif:
-      "Libertinus Serif", "Noto Serif TC", "Source Han Serif TC",
+      var(--font-libertinus-serif), "Noto Serif TC", "Source Han Serif TC",
       "Source Han Serif", ui-serif, serif;
   }
   & :lang("jp") {
     --font-serif:
-      "Libertinus Serif", "Noto Serif JP", "Source Han Serif JP",
+      var(--font-libertinus-serif), "Noto Serif JP", "Source Han Serif JP",
       "Source Han Serif", ui-serif, serif;
   }
   & :lang("ko") {
     --font-serif:
-      "Libertinus Serif", "Noto Serif KR", "Source Han Serif KR",
+      var(--font-libertinus-serif), "Noto Serif KR", "Source Han Serif KR",
       "Source Han Serif", ui-serif, serif;
   }
 }
astro.config.ts
@@ -1,6 +1,6 @@
 import sitemap from "@astrojs/sitemap";
 import tailwindcss from "@tailwindcss/vite";
-import { defineConfig } from "astro/config";
+import { defineConfig, fontProviders } from "astro/config";
 import { typst } from "astro-typst";
 import { SITE_CONFIG } from "./src/config";
 import { extractImage } from "./src/integrations/extract-image";
@@ -16,6 +16,13 @@ export default defineConfig({
   site: `${protocol}://${site}`,
   base: base,
   prefetch: true,
+  fonts: [
+    {
+      provider: fontProviders.fontsource(),
+      name: "Libertinus Serif",
+      cssVariable: "--font-libertinus-serif",
+    },
+  ],
   integrations: [
     typst({
       options: {