Skip to content
Donghai's Blog
Go back

AstroPaper主题自定义记录

修改RSS订阅链接

之前的博客RSS订阅链接是 /index.xml ,但 AstroPaper RSS订阅链接是 /rss.xml,为了不影响订阅源,需要做点修改

1.重命名文件

src\pages\rss.xml.ts 重命名为 src\pages\index.xml.ts

2.修改其他页面的路由配置

全局搜索 rss.xml ,检查有哪些页面使用了它,然后修改为 index.xml

设置 slug

因为有一些文章迁移过来,我不想影响原因的文章链接,所以需要设置 slus ,但是我发现虽然设置了,却没有效果,例如:

---
pubDatetime: 2022-09-23T15:22:00Z
modDatetime: 2025-06-13T16:52:45.934Z
title: 在AstroPaper主题中添加新帖子(译)
slug: doc/adding-new-posts-in-astropaper-theme
featured: true
draft: false
tags:
  - astro-paper
  - docs
description: 在使用 AstroPaper 主题创建或添加新帖子时的一些规则和建议
---

<!-- 文章的其余部分 -->

文章路径是 src\data\blog\astro-paper\adding-new-post.md,在浏览器预览这篇文章的链接却是 http://localhost:4321/posts/astro-paper/adding-new-posts-in-astropaper-theme,并不是预期的 http://localhost:4321/doc/adding-new-posts-in-astropaper-theme

看了 src\utils\getPath.ts 源码后,发现问题出在这里:id 被当作了文件名处理,而不是完整路径,所以需要再点逻辑:

export function getPath(
  id: string,
  filePath: string | undefined,
  includeBase = true
) {
  // 检查 id 是否包含自定义路径
  if (id.includes('/')) {
    // 如果 id 包含 '/',直接使用完整 id
    const basePath = includeBase ? "/posts" : "";
    return [basePath, id].join("/");
  }

  // 原有逻辑保持不变...
  const pathSegments = filePath
    ?.replace(BLOG_PATH, "")
    .split("/")
    .filter(path => path !== "")
    .filter(path => !path.startsWith("_"))
    .slice(0, -1)
    .map(segment => slugifyStr(segment));

  const basePath = includeBase ? "/posts" : "";
  const blogId = id.split("/");
  const slug = blogId.length > 0 ? blogId.slice(-1) : blogId;

  if (!pathSegments || pathSegments.length < 1) {
    return [basePath, slug].join("/");
  }

  return [basePath, ...pathSegments, slug].join("/");
}

修改日期格式

将日期格式从 “4 Jan, 2024” 修改为 “2024-01-04”

---


// ...此处代码省略...

const isModified = modDatetime && modDatetime > pubDatetime;

// ...此处代码省略...

const date = datetime.format("D MMM, YYYY"); 
const date = datetime.format("YYYY-MM-DD"); 
---

<div class:list={["flex items-center gap-x-2 opacity-80", className]}>
// ...此处代码省略...
</div>
src/components/Datetime.astro

Share this post on:

Previous Post
AstroPaper主题添加GitHub风格的Markdown警告框
Next Post
我日常收藏的优质网站资源导航(持续更新)
BlogsClub Meo Forever Blog