Skip to content
Discussion options

You must be logged in to vote

实现方案

  1. [手动] blog 目录下建立 2024、2025 等文件夹,再在内部塞入对应文件。

  2. 从 content collection id 下手

    a. [自动] 经测试在 define 文件中没有暴露 id,故的确不可行
    b. [手动] 使用 custom ids 手动定义,但较为复杂麻烦

  3. [自动] 使用 dynamic routes 双层匹配。具体说明见后文。

Dynamic routes

改写匹配规则 [...id].astro 为:

src/pages/[year]/[id].astro

---
import { render, type CollectionEntry } from 'astro:content'

import { getBlogCollection, sortMDByDate } from 'astro-pure/server'
import PostLayout from '@/layouts/BlogPost.astro'

export const prerender = true

export async function getStaticPaths() {
  const posts = sortMDByDate(await getBlogCollection())
  return posts.map((post) => ({
    params: {
      id: post.id,
      // When publishDate exists, add year to the path de…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@Invisibox
Comment options

@cworld1
Comment options

Answer selected by cworld1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants