Eleventy(11ty)的 llms.txt

Eleventy 的模板系统让 llms.txt 的实现非常直接:使用带 permalink frontmatter 键的纯文本文件,或遍历集合,根据内容自动生成链接。

最近更新:

方案 1:纯文本模板

src/llms.txt (或输入目录中的任意位置)。添加一个 permalink 键,以控制输出路径。Eleventy 将处理该文件并将其写入 _site/llms.txt.

src/llms.txt
---
permalink: /llms.txt
eleventyExcludeFromCollections: true
---
# Your Site

> One-sentence description of what your site or product does.

## Documentation

- [Getting Started](https://yoursite.com/docs/start): Install and configure in minutes.
- [API Reference](https://yoursite.com/docs/api): Full endpoint catalog with examples.

## Product

- [Overview](https://yoursite.com/product): Core features and capabilities.
- [Pricing](https://yoursite.com/pricing): Plans and billing details.

## Optional

- [Changelog](https://yoursite.com/changelog): Release history.
- [GitHub](https://github.com/your-org/your-repo): Source code.
eleventy.config.js, enable txt templates
// eleventy.config.js
module.exports = function (eleventyConfig) {
  // Add passthrough copy for any assets you want served verbatim
  // llms.txt is handled via permalink in the template, no passthrough needed

  // Optional: add a collection for docs
  eleventyConfig.addCollection('docs', function (collectionApi) {
    return collectionApi.getFilteredByGlob('src/docs/**/*.md');
  });

  // Ensure .txt files are processed as templates
  return {
    templateFormats: ['md', 'njk', 'html', 'txt'],
    dir: {
      input: 'src',
      output: '_site',
    },
  };
};

选项 2:从集合中生成

如果你的文档以 Eleventy 集合的形式管理(带有 frontmatter 的 Markdown 文件),可以在模板内遍历集合并自动生成链接列表。使用 ---js 用于基于 JavaScript 的配置的 frontmatter 语法,或用于正文的 Nunjucks/Liquid 模板语法。

src/llms.txt, from collections
---js
{
  permalink: "/llms.txt",
  eleventyExcludeFromCollections: true
}
---
# Your Site

> {{ site.description }}

## Documentation

{% for doc in collections.docs | sort(attribute='data.order') -%}
- [{{ doc.data.title }}]({{ site.url }}{{ doc.url }}): {{ doc.data.description }}
{% endfor %}

site.url 全局数据变量应在你的 _data/site.js_data/site.json 文件。确保所有链接都使用绝对 URL, 相对路径在被 AI 爬虫获取时无法正确解析。

以下是你的关键 frontmatter 选项: llms.txt 模板:

  • permalink: /llms.txt,将文件输出到网站根目录。
  • eleventyExcludeFromCollections: true,可防止该文件出现在你的 站点集合(nav、feeds、sitemaps)中。
  • layout: false,确保不会应用布局包装器(这并不需要,因为 .txt 文件默认不使用 HTML 布局)。

验证

运行 Eleventy 构建并检查输出:

Build and verify
# Build
npx @11ty/eleventy

# Check that the file was generated
cat _site/llms.txt | head -5

# After deploying, verify the live URL
curl -I https://yoursite.com/llms.txt
# Expected: content-type: text/plain; charset=utf-8

相关指南

来源