GitHub Pages 的 llms.txt

GitHub Pages 的三种做法:提交到 docs/ 或 gh-pages 的静态文件、自动生成内容的 Jekyll 模板,或实现完全自动化生成的 GitHub Actions 工作流。

最近更新:

选项 1:docs/ 或 gh-pages 中的静态文件

最简单的方法是提交 llms.txt 作为纯文件添加到您的代码仓库。GitHub Pages 会按原样提供该文件,并使用正确的 MIME 类型。

GitHub Pages directory structure
# GitHub Pages, static file approach
#
# Serving from docs/ branch (most common):
#   your-repo/
#   ├── docs/
#   │   ├── index.html
#   │   └── llms.txt   ← add this file here
#   └── README.md
#
# Serving from gh-pages branch:
#   Create or switch to the gh-pages branch, then add:
#   llms.txt   ← in the branch root
#
# In repository Settings → Pages:
#   Source: Deploy from a branch
#   Branch: main (or gh-pages), Folder: /docs (or /)
#
# GitHub Pages serves it at: https://username.github.io/repo/llms.txt
# With a custom domain: https://yourdomain.com/llms.txt

GitHub Pages 会自动识别纯文本文件,并使用以下类型提供: Content-Type: text/plain; charset=utf-8。无需配置。

选项 2:带 layout: null 的 Jekyll

如果你的 GitHub Pages 网站使用 Jekyll(许多仓库的默认选择),普通的 .txt 文件会原样通过,无需 front matter。不过,如果 你想让 Jekyll 将该文件作为模板处理(例如注入站点变量),请添加一个 front matter 块,其中包含 layout: null 以防止 Jekyll 将其包裹进 HTML 布局中。

llms.txt, with Jekyll front matter
---
layout: null
permalink: /llms.txt
---
# My Project

> One-sentence description of what my project does.

## Documentation

- [Getting Started](https://myproject.com/docs/start/): Install and configure.
- [API Reference](https://myproject.com/docs/api/): Full endpoint catalog.

## Optional

- [Changelog](https://myproject.com/changelog/): Release history.

你也可以从一个 YAML 数据文件中驱动链接列表,位于 _data/,将内容与模板分离:

_data/llms_links.yml
# _data/llms_links.yml
docs:
  - title: "Getting Started"
    url: "https://myproject.com/docs/start/"
    desc: "Install and configure in minutes."
  - title: "API Reference"
    url: "https://myproject.com/docs/api/"
    desc: "Full endpoint catalog with examples."
optional:
  - title: "Changelog"
    url: "https://myproject.com/changelog/"
    desc: "Release history."
llms.txt, Jekyll template with _data
---
layout: null
permalink: /llms.txt
---
# {{ site.title }}

> {{ site.description }}

## Documentation
{% for link in site.data.llms_links.docs %}
- [{{ link.title }}]({{ link.url }}): {{ link.desc }}
{% endfor %}

## Optional
{% for link in site.data.llms_links.optional %}
- [{{ link.title }}]({{ link.url }}): {{ link.desc }}
{% endfor %}

选项 3:使用 GitHub Actions 动态生成

对于需要根据内容(Markdown 文件、frontmatter、CMS)自动生成链接列表的网站,可以使用 GitHub Actions 工作流运行脚本,并将结果提交回仓库。

.github/workflows/generate-llms-txt.yml
# .github/workflows/generate-llms-txt.yml
# Generates llms.txt from your content and commits it to the repo.
name: Generate llms.txt

on:
  push:
    branches: [main]
    paths:
      - 'docs/**'
      - 'content/**'
  workflow_dispatch:

jobs:
  generate:
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Generate llms.txt
        run: node scripts/generate-llms-txt.js

      - name: Commit and push if changed
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add docs/llms.txt
          git diff --staged --quiet || git commit -m "chore: regenerate llms.txt"
          git push
scripts/generate-llms-txt.js
// scripts/generate-llms-txt.js
// Run by GitHub Actions to generate docs/llms.txt from your content
const fs = require('fs');
const path = require('path');

const SITE_URL = 'https://myproject.com';

// Example: build link list from your markdown files in docs/
const docsDir = path.join(__dirname, '..', 'docs');
const mdFiles = fs.readdirSync(docsDir)
  .filter(f => f.endsWith('.md') && f !== 'index.md');

const links = mdFiles.map(file => {
  const content = fs.readFileSync(path.join(docsDir, file), 'utf-8');
  const titleMatch = content.match(/^#\s+(.+)/m);
  const descMatch = content.match(/^>\s+(.+)/m);
  const slug = file.replace('.md', '');
  const title = titleMatch ? titleMatch[1] : slug;
  const desc = descMatch ? descMatch[1] : '';
  return `- [${title}](${SITE_URL}/docs/${slug}/): ${desc}`;
}).join('\n');

const output = [
  '# My Project',
  '',
  '> My project description.',
  '',
  '## Documentation',
  '',
  links,
  '',
  '## Optional',
  '',
  `- [Changelog](${SITE_URL}/changelog/): Release history.`,
].join('\n');

fs.writeFileSync(path.join(docsDir, 'llms.txt'), output, 'utf-8');
console.log('Generated docs/llms.txt');

该工作流会在推送到以下分支时触发: main 会修改你的内容目录,而且也可以通过以下方式手动运行: workflow_dispatch。仅当生成的文件确实发生变化时,它才会创建提交。

自定义域名和 CNAME

如果你使用自定义域名(例如 myproject.com),请添加一个 CNAME 文件,其中包含你的域名。GitHub Pages 会提供你的网站,包括 llms.txt,通过该域名访问。

docs/CNAME
myproject.com

在您的 llms.txt 应与你的自定义域名匹配,而不是默认的 username.github.io/repo URL。

验证

Verification
curl -I https://myproject.com/llms.txt
# Expected:
# HTTP/2 200
# content-type: text/plain; charset=utf-8

curl https://myproject.com/llms.txt | head -5

相关指南

来源