@rspress/plugin-llms

为 Rspress 站点生成 llms.txt 相关文件,使大模型可以更好地理解你的文档站。

安装

npm
yarn
pnpm
bun
npm add @rspress/plugin-llms -D

使用

在配置文件中写入以下的配置:

// rspress.config.ts
import { defineConfig } from 'rspress';
import { pluginLlms } from '@rspress/plugin-llms';

export default defineConfig({
  plugins: [pluginLlms()],
});

之后执行 rspress build 命令,在生成产物的同时,会在产物目录下根据导航栏生成 llms.txt llms-full.txt 和对应路由的 markdown 文件。

选项

这个插件接受一个对象参数,类型如下:

export interface Options {
  llmsTxt?: boolean | LlmsTxt;
  mdFiles?: boolean;
  llmsFullTxt?: boolean;
  include?: (context: { page: PageIndexInfo }) => boolean;
  exclude?: (context: { page: PageIndexInfo }) => boolean;
}

llmsTxt

  • 类型: false | LlmsTxt
import type { PageIndexInfo } from '@rspress/shared';

export interface LlmsTxt {
  name: string;
  onTitleGenerate?: (context: {
    title: string | undefined;
    description: string | undefined;
  }) => string;
  onLineGenerate?: (page: PageIndexInfo) => string;
  onAfterLlmsTxtGenerate?: (llmsTxtContent: string) => string;
}
  • 默认值: { name: 'llms.txt' }

是否生成 llms.txt 文件,或者通过 hooks 自定义生成 llms.txt 文件。

一个 llms.txt 文件的默认格式如下:

# {title}

> {description}

## {nav1.title}

- [{page.title}]({ page.routePath }): {page.frontmatter.description}

## {nav2.title}

- [{page.title}]({ page.routePath }): {page.frontmatter.description}

你可以通过 hook 对指定部分进行修改。

  • onTitleGenerate: 自定义生成 title 和 description 部分。
  • onLineGenerate: 自定义生成 md 文件的每一行。
  • onAfterLlmsTxtGenerate: 最后修改 llms.txt 文件的内容。

例如:

pluginLlms({
  llmsTxt: {
    onTitleGenerate: ({ title, description }) => {
      return `# ${title} - llms.txt

> ${description}

Rspress is a static site generator based on Rsbuild and it can generate llms.txt with @rspress/plugin-llms.
`;
    },
  },
});

对应的生成结果为:

# Rspress - llms.txt

> Rsbuild based static site generator

Rspress is a static site generator based on Rsbuild and it can generate llms.txt with @rspress/plugin-llms.

## guide

- [foo](/foo.md)

mdFiles

  • 类型: false | MdFiles
export interface MdFiles {
  mdxToMd: boolean;
}
  • 默认值: { mdxToMd: true }

是否生成对应路由的 markdown 文件,当设置为 false 时,不会生成对应路由的 markdown 文件。

mdxToMd

  • 类型: boolean
  • 默认值: true

是否将 mdx 文件转换为 md 文件,默认会将 mdx 文件通过一组默认策略转换为 md 文件。

llmsFullTxt

  • 类型: false | LlmsFullTxt
export interface LlmsFullTxt {
  name: string;
}
  • 默认值: { name: 'llms-full.txt' }

是否生成 llms-full.txt 文件,当设置为 false 时,不会生成 llms-full.txt 文件。

include

  • 类型: (context: { page: PageIndexInfo }) => boolean
  • 默认值: (context) => context.page.lang === config.lang

是否生成时包含某些页面,默认只会包含默认语言对应的页面,一般用于精简 llms.txt。

  • 示例:

只为语言为英文的页面生成 llms.txt 等相关文件:

pluginLlms({
  include: ({ page }) => {
    return page.lang === 'en';
  },
});

exclude

  • 类型: (context: { page: PageIndexInfo }) => boolean
  • 默认值: undefined

是否生成时排除某些页面,会在 include 之后执行。

  • 示例:

排除 /foo 路由下的单个页面:

pluginLlms({
  exclude: ({ page }) => {
    return page.routePath === '/foo';
  },
});

同时生成多组 llms.txt

由于大模型只需要一组 llms.txt 即可理解你网站,所以默认只会包含默认语言对应的页面。但某些情况下,你可能生成多组 llms.txt,例如多语言站点。

此时,你可以通过传入一个数组来实现:

  • 示例:
// rspress.config.ts
import { defineConfig } from 'rspress/config';
defineConfig({
  lang: 'en',
  plugins: [
    pluginLlms([
      {
        llmsTxt: {
          name: 'llms.txt',
        },
        llmsFullTxt: {
          name: 'llms-full.txt',
        },
        include: ({ page }) => page.lang === 'en',
      },
      {
        llmsTxt: {
          name: 'zh/llms.txt',
        },
        llmsFullTxt: {
          name: 'zh/llms-full.txt',
        },
        include: ({ page }) => page.lang === 'zh',
      },
    ]),
  ],
});