Skip to content
Shaoheng Nie
Go back

如何配置 AstroPaper 主题

Updated:

AstroPaper 是一个高度可定制的 Astro 博客主题。使用 AstroPaper,你可以根据个人喜好自定义一切。本文将介绍如何通过配置文件轻松进行一些自定义设置。

目录

配置 SITE

重要的配置位于 src/config.ts 文件中。在该文件中,你会看到 SITE 对象,你可以在其中指定网站的主要配置。

在开发过程中,SITE.website 留空是可以的。但在生产模式下,你应该在 SITE.website 选项中指定你部署的 URL,因为这将用于规范 URL、社交卡片 URL 等,对 SEO 很重要。

export const SITE = {
  website: "https://astro-paper.pages.dev/", // replace this with your deployed domain
  author: "Sat Naing",
  profile: "https://satnaing.dev/",
  desc: "A minimal, responsive and SEO-friendly Astro blog theme.",
  title: "AstroPaper",
  ogImage: "astropaper-og.jpg",
  lightAndDarkMode: true,
  postPerIndex: 4,
  postPerPage: 4,
  scheduledPostMargin: 15 * 60 * 1000, // 15 minutes
  showArchives: true,
  showBackButton: true, // show back button in post detail
  editPost: {
    enabled: true,
    text: "Suggest Changes",
    url: "https://github.com/satnaing/astro-paper/edit/main/",
  },
  dynamicOgImage: true, // enable automatic dynamic og-image generation
  dir: "ltr", // "rtl" | "auto"
  lang: "en", // html lang code. Set this empty and default will be "en"
  timezone: "Asia/Bangkok", // Default global timezone (IANA format) https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
} as const;src/config.ts

以下是 SITE 配置选项

选项描述
website你部署的网站 URL
author你的名字
profile你的个人/作品集网站 URL,用于更好的 SEO。如果没有,请设为 null 或空字符串 ""
desc你的网站描述。对 SEO 和社交媒体分享很有用。
title你的网站名称
ogImage网站的默认 OG 图片。对社交媒体分享很有用。OG 图片可以是外部图片 URL,也可以放在 /public 目录下。
lightAndDarkMode启用或禁用网站的 亮色与暗色模式。如果禁用,将使用主配色方案。此选项默认启用。
postPerIndex首页 Recent 部分下显示的文章数量。
postPerPage你可以指定每页文章列表显示多少篇文章。(例如:如果你将 SITE.postPerPage 设为 3,每页将只显示 3 篇文章)
scheduledPostMargin在生产模式下,pubDatetime 为未来的文章将不可见。但是,如果某篇文章的 pubDatetime 在未来 15 分钟内,它将会显示。如果你不喜欢默认的 15 分钟边距,可以设置 scheduledPostMargin
showArchives决定是否显示 Archives 菜单(位于 AboutSearch 菜单之间)及其对应页面。此选项默认设为 true
showBackButton决定是否在每篇博客文章中显示 返回 按钮。
editPost此选项允许用户通过在博客文章标题下方提供编辑链接来建议修改。可以通过将 SITE.editPost.enabled 设为 false 来禁用此功能。
dynamicOgImage此选项控制在博客文章 frontmatter 中未指定 ogImage 时是否生成动态 og-image。如果你有很多博客文章,可能希望禁用此功能。详情请参阅权衡说明
dir指定整个博客的文本方向。在 <html dir="ltr"> 中用作 HTML dir 属性。支持的值:ltr | rtl | auto
lang用作 <html lang"en"> 中的 HTML ISO 语言代码。默认为 en
timezone此选项允许你使用 IANA 格式指定时区。设置此项可确保本地环境和部署站点之间时间戳一致,消除时间差异。

更新布局宽度

整个博客的默认 max-width768pxmax-w-3xl)。如果你想更改它,可以在 global.css 中轻松更新 max-w-app 工具类。例如:

@utility max-w-app {
  @apply max-w-3xl;
  @apply max-w-4xl xl:max-w-5xl;
}src/styles/global.css

你可以在 Tailwind CSS 文档中探索更多的 max-width 值。

配置 Logo 或标题

在 AstroPaper v5 之前,你可以在 src/config.ts 文件的 LOGO_IMAGE 对象中更新网站名称/logo。然而,在 AstroPaper v5 中,此选项已被移除,取而代之的是 Astro 内置的 SVG 和 Image 组件。

指向网站 logo 的箭头

你有 3 种选择:

选项 1:SITE title 文本

这是最简单的选项。你只需要更新 src/config.ts 文件中的 SITE.title

选项 2:Astro 的 SVG 组件

如果你想使用 SVG logo,可能希望选择此选项。

这种方法最大的优点是可以根据需求自定义 SVG 样式。在上面的示例中,你可以看到如何在暗色模式下反转 SVG logo 的颜色。

选项 3:Astro 的 Image 组件

如果你的 logo 是图片而非 SVG,可以使用 Astro 的 Image 组件。

使用这种方法,你仍然可以通过 CSS 类调整图片的外观。然而,这可能并不总是符合你的需求。如果你需要根据亮色或暗色模式显示不同的 logo 图片,请查看 Header.astro 组件中如何处理亮色/暗色图标。

配置社交链接

指向社交链接图标的箭头

你可以在 constants.tsSOCIALS 对象中配置社交链接。

export const SOCIALS = [
  {
    name: "GitHub",
    href: "https://github.com/satnaing/astro-paper",
    linkTitle: ` ${SITE.title} on GitHub`,
    icon: IconGitHub,
  },
  {
    name: "X",
    href: "https://x.com/username",
    linkTitle: `${SITE.title} on X`,
    icon: IconBrandX,
  },
  {
    name: "LinkedIn",
    href: "https://www.linkedin.com/in/username/",
    linkTitle: `${SITE.title} on LinkedIn`,
    icon: IconLinkedin,
  },
  {
    name: "Mail",
    href: "mailto:yourmail@gmail.com",
    linkTitle: `Send an email to ${SITE.title}`,
    icon: IconMail,
  },
] as const;src/constants.ts

配置分享链接

你可以在 src/constants.tsSHARE_LINKS 对象中配置分享链接。

指向分享链接图标的箭头

配置字体

AstroPaper 使用 Astro 的实验性字体 API,默认字体为 Google Sans Code。这提供了在所有平台上一致的排版效果,并具有自动字体优化功能,包括预加载和缓存。

使用默认字体

字体已在 astro.config.ts 中自动配置并在 Layout.astro 中加载。使用默认的 Google Sans Code 字体无需额外配置。

自定义字体

要使用不同的字体,你需要更新三个地方:

  1. 更新 astro.config.ts 中的字体配置:
import { defineConfig, fontProviders } from "astro/config";

export default defineConfig({
  // ...
  experimental: {
    fonts: [
      {
        name: "Your Font Name",
        cssVariable: "--font-your-font",
        provider: fontProviders.google(),
        fallbacks: ["monospace"],
        weights: [300, 400, 500, 600, 700],
        styles: ["normal", "italic"],
      },
    ],
  },
});astro.config.ts
  1. 更新 Layout.astro 中的 Font 组件:
---
import { Font } from "astro:assets";
// ...
---

<head>
  <!-- ... -->
  <Font
    cssVariable="--font-your-font"
    preload={[{ subset: "latin", weight: 400, style: "normal" }]}
  />
  <!-- ... -->
</head>src/layouts/Layout.astro
  1. 更新 global.css 中的 CSS 变量映射:
@theme inline {
  --font-app: var(--font-your-font); 
  --color-background: var(--background);
  --color-foreground: var(--foreground);
  --color-accent: var(--accent);
  --color-muted: var(--muted);
  --color-border: var(--border);
}src/styles/global.css

--font-app 变量通过 font-app Tailwind 工具类在主题中全局使用,因此更新这一个变量即可将自定义字体应用到所有地方。

注意:确保字体名称与 Google Fonts 上显示的名称完全一致。对于其他字体提供商或本地字体,请参阅 Astro 实验性字体 API 文档

结语

这是关于如何自定义此主题的简要说明。如果你懂一些编程,还可以进行更多自定义。关于自定义样式,请阅读这篇文章。感谢阅读。✌🏻


Share this post on:

Next Post
自定义 AstroPaper 主题配色方案