npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cogita/plugin-posts-frontmatter

v0.1.6

Published

Core plugin that automatically scans posts and extracts frontmatter data for Cogita themes.

Downloads

1,127

Readme

@cogita/plugin-posts-frontmatter

npm version TypeScript

中文 | English

自动扫描文章并为 Cogita 主题提取 frontmatter 数据的核心插件

这是什么?

此插件自动扫描您的 posts/ 目录,从 Markdown 文件中提取 frontmatter,并通过虚拟模块向主题提供数据。它是博客文章列表、归档和其他内容驱动功能的基础。

安装

pnpm add @cogita/plugin-posts-frontmatter

注意: 在 Cogita 框架中,此插件由需要它的主题(如 @cogita/theme-lucid)自动包含。通常不需要手动安装。

工作原理

  1. 扫描 posts 目录中的所有 .md 和 .mdx 文件
  2. 提取 每个文件的 frontmatter 元数据
  3. 生成 每篇文章的路由
  4. 创建 包含所有文章数据的虚拟模块
  5. 提供 通过 virtual-posts-data 向主题提供数据

使用方法

自动使用(推荐)

简单使用兼容的 Cogita 主题:

// cogita.config.ts
import { defineConfig } from '@cogita/core';

export default defineConfig({
  theme: 'lucid', // 主题自动加载此插件
});

手动配置(高级用法)

用于自定义设置或 Rspress 项目:

import { defineConfig } from '@rspress/core';
import { pluginPostsFrontmatter } from '@cogita/plugin-posts-frontmatter';

export default defineConfig({
  plugins: [
    (config) => pluginPostsFrontmatter({
      ...config,
      postsDir: 'posts',
      routePrefix: 'posts',
      cwd: process.cwd(),
    })
  ],
});

虚拟模块

插件创建 virtual-posts-data 模块:

// 在主题组件中可用
import { allPosts } from 'virtual-posts-data';

function BlogHome() {
  const recentPosts = allPosts.slice(0, 5);
  
  return (
    <div>
      {recentPosts.map(post => (
        <article key={post.url}>
          <h2>{post.title}</h2>
          <time>{post.createDate}</time>
          <p>{post.description}</p>
        </article>
      ))}
    </div>
  );
}

文章格式

Frontmatter 示例

---
title: "您的文章标题"
description: "SEO 和分享用的简要描述"
createDate: "2024-01-01"
updateDate: "2024-01-15"
tags: ["标签1", "标签2"]
categories: ["分类1"]
author: "作者姓名"
draft: false
featured: true
---

# 您的文章内容从这里开始

编写您的 Markdown 内容...

支持的字段

| 字段 | 类型 | 描述 | 默认值 | |------|------|------|-------| | title | string | 文章标题 | 文件名 | | description | string | 文章描述 | - | | createDate | string | 创建日期 (YYYY-MM-DD) | 文件创建时间 | | updateDate | string | 最后更新日期 | 文件修改时间 | | tags | string[] | 文章标签 | - | | categories | string[] | 文章分类 | - | | author | string | 作者姓名 | - | | draft | boolean | 草稿状态 | false | | featured | boolean | 推荐文章 | false |

日期格式

支持多种日期格式:

# 推荐
createDate: "2024-01-01"
createDate: "2024-01-01T10:30:00Z"

# 也支持
date: "2024-01-01"          # createDate 的别名
createDate: "Jan 1, 2024"
createDate: "2024/01/01"

配置

插件选项

interface PluginConfig {
  postsDir?: string;        // 文章目录(默认:'posts')
  routePrefix?: string;     // 路由前缀(默认:'posts')
  cwd?: string;            // 项目根目录
  sortBy?: string;         // 排序字段(默认:'createDate')
  sortOrder?: string;      // 排序顺序(默认:'desc')
}

目录结构

posts/
├── hello-world.md        → /posts/hello-world
├── 2024/
│   └── new-year.md      → /posts/2024/new-year
└── tech/
    └── react-tips.md    → /posts/tech/react-tips

数据结构

PostFrontmatter 接口

interface PostFrontmatter {
  title: string;
  description?: string;
  filePath: string;         // 绝对文件路径
  route: string;           // 路由路径(如:'/posts/hello-world')
  url: string;             // 与 route 相同(兼容性)
  createDate: string;      // ISO 日期字符串
  updateDate: string;      // ISO 日期字符串
  tags?: string[];
  categories?: string[];
  [key: string]: any;      // 额外的 frontmatter 字段
}

TypeScript 支持

添加客户端类型定义:

/// <reference types="@cogita/plugin-posts-frontmatter/client" />

或在 tsconfig.json 中:

{
  "compilerOptions": {
    "types": ["@cogita/plugin-posts-frontmatter/client"]
  }
}

自定义

自定义排序顺序

// 按更新日期排序
pluginPostsFrontmatter({
  sortBy: 'updateDate',
  sortOrder: 'desc'
});

过滤文章

// 自定义插件包装器
export const myPostsPlugin = (config) => {
  const plugin = pluginPostsFrontmatter(config);
  const originalBeforeBuild = plugin.beforeBuild;
  
  plugin.beforeBuild = async function() {
    await originalBeforeBuild?.call(this);
    // 在生产环境中过滤掉草稿文章
    if (process.env.NODE_ENV === 'production') {
      allPosts = allPosts.filter(post => !post.draft);
    }
  };
  
  return plugin;
};

故障排除

文章不显示

检查这些常见问题:

  1. 文件位置:确保文件在 posts/ 目录中
  2. 文件扩展名:使用 .md 或 .mdx 扩展名
  3. Frontmatter 格式:有效的 YAML frontmatter
  4. 草稿状态:检查是否设置了 draft: true
# 调试:检查 posts 目录
ls -la posts/

# 调试:验证 frontmatter
head -10 posts/your-post.md

路由问题

  • 避免文件名中的特殊字符
  • 使用 kebab-case 获得最佳 SEO
  • 检查冲突的路由

性能

对于大量文章:

// 启用缓存
export default defineConfig({
  builderConfig: {
    cache: { type: 'filesystem' }
  }
});

开发

# 克隆仓库
git clone https://github.com/wu9o/cogita.git
cd cogita/plugins/posts-frontmatter

# 安装依赖
pnpm install

# 构建插件
pnpm build

# 运行测试
pnpm test

了解更多

相关包

许可证

MIT © wu9o