@rspress/plugin-llms

Generate llms.txt related files for your Rspress site, allowing large language models to better understand your documentation site.

Installation

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

Usage

Add the following configuration to your configuration file:

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

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

Then execute the rspress build command. While generating the output, the plugin will also generate llms.txt, llms-full.txt, and corresponding markdown files for each route in the output directory based on the navigation bar.

Configuration

This plugin accepts an object parameter with the following type:

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

llmsTxt

  • Type: 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;
}
  • Default: { name: 'llms.txt' }

Whether to generate the llms.txt file, or to customize the llms.txt file through hooks.

The default format of an llms.txt file is as follows:

# {title}

> {description}

## {nav1.title}

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

## {nav2.title}

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

You can modify the specified part through hook.

  • onTitleGenerate: Customize the generated title and description sections.
  • onLineGenerate: Customize each line of the md file.
  • onAfterLlmsTxtGenerate: Finally modify the contents of the llms.txt file.

For example:

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.
`;
    },
  },
});

The corresponding generation results are:

# 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

  • Type: false | MdFiles
export interface MdFiles {
  mdxToMd: boolean;
}
  • Default: { mdxToMd: true }

Whether to generate a markdown file for the corresponding route, when set to false, the markdown file for the corresponding route will not be generated.

mdxToMd

  • Type: boolean
  • Default: true

Whether to convert mdx files to md files, by default, mdx files will be converted to md files through a set of default strategies.

llmsFullTxt

  • Type: false | LlmsFullTxt
export interface LlmsFullTxt {
  name: string;
}
  • Default: { name: 'llms-full.txt' }

Whether to generate the llms-full.txt file, the llms-full.txt file will not be generated when set to false.

include

  • Type: (context: { page: PageIndexInfo }) => boolean
  • Default: (context) => context.page.lang === config.lang

Whether to include certain pages when generated will only include pages corresponding to the default language by default, which is generally used to simplify llms.txt.

  • Example:

Generate llms.txt and other related files for pages whose language is English only:

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

exclude

  • Type: (context: { page: PageIndexInfo }) => boolean
  • Default: undefined

Whether to exclude certain pages, it will be executed after include.

  • Example:

Exclude a single page under the /foo route:

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

Generate multiple groups of llms.txt at the same time

Since the llms only requires a set of llms.txt to understand your website, it will only contain pages corresponding to the default language by default. But in some cases, you may generate multiple groups of llms.txt, such as multilingual sites.

At this point, you can do it by passing in an array:

  • Example:
// 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',
      },
    ]),
  ],
});