Skip to Content
🎉 Nextra 4.0 已发布。Dima Machina 正在 寻找新的工作或咨询机会
Documentation博客主题获取文章和标签

获取文章和标签

以下代码片段演示了如何获取所有文章及其关联的标签。

app/posts/get-posts.js
import { normalizePages } from 'nextra/normalize-pages' import { getPageMap } from 'nextra/page-map' export async function getPosts() { const { directories } = normalizePages({ list: await getPageMap('/posts'), route: '/posts' }) return directories .filter(post => post.name !== 'index') .sort((a, b) => new Date(b.frontMatter.date) - new Date(a.frontMatter.date)) } export async function getTags() { const posts = await getPosts() const tags = posts.flatMap(post => post.frontMatter.tags) return tags }
Last updated on