Hugo 的 llms.txt

两种做法:在 static/ 中放静态文件(零配置,处处可用),或者使用自定义输出格式,在构建时从你的内容自动生成 llms.txt。

最近更新:

选项 1:静态文件(推荐)

Hugo 会复制 static/ 目录直接复制到 public/ 构建输出。请将你的 llms.txt 将其放在那里后,文件会在以下地址提供: /llms.txt 在任何托管平台上提供,无需更改 Hugo 配置。

static/llms.txt, directory structure
# Hugo, zero-config static approach
#
# Place your file at: static/llms.txt
# Hugo copies everything in static/ directly to the public/ output directory.
# Your file will be served at /llms.txt on any host.
#
# Project structure:
# your-hugo-site/
# ├── static/
# │   └── llms.txt   ← add this
# ├── content/
# ├── layouts/
# └── hugo.toml
#
# No config changes needed. Run "hugo" and it appears in public/llms.txt.

运行 hugo,您会找到 public/llms.txt 位于构建输出中。部署 public/ 到 Cloudflare Pages、Vercel、GitHub Pages 或任何 静态主机。

方案 2:自定义输出格式

Hugo 的输出格式系统允许你使用 Go 模板根据内容生成任意文件类型。定义一个新的 LLMSTXT 格式,然后创建模板,Hugo 会构建 public/llms.txt 自动在每个 hugo 运行时。

第 1 步,将输出格式添加到你的配置中

hugo.toml
# hugo.toml, define the llmstxt custom output format

[outputs]
  home = ["HTML", "RSS", "LLMSTXT"]

[outputFormats.LLMSTXT]
  mediaType = "text/plain"
  baseName = "llms"
  isPlainText = true
  notAlternative = true

使用 YAML?配置相同:

hugo.yaml
# hugo.yaml, same config in YAML

outputs:
  home:
    - HTML
    - RSS
    - LLMSTXT

outputFormats:
  LLMSTXT:
    mediaType: text/plain
    baseName: llms
    isPlainText: true
    notAlternative: true

关键设置是 mediaType = "text/plain" (使 Hugo 写入纯文本文件), baseName = "llms" (生成 llms.txt),以及 notAlternative = true (抑制一个不需要的 <link rel="alternate"> 标签)。

编写模板

在以下位置创建模板: layouts/index.llmstxt.txt。Hugo 使用 主页 模板 作为根级输出。这里有一个列出所有常规页面的简洁版本:

layouts/index.llmstxt.txt, simple
{{/* layouts/index.llmstxt.txt */}}
{{/* Hugo home template for the llmstxt output format */}}
# {{ .Site.Title }}

> {{ .Site.Params.description }}

{{ with .Site.Params.llmstxtContext }}{{ . }}

{{ end -}}
## Pages

{{ range .Site.RegularPages -}}
- [{{ .Title }}]({{ .Permalink }}): {{ with .Description }}{{ . }}{{ else }}{{ .Summary | plainify | truncate 120 }}{{ end }}
{{ end }}

对于包含多个内容版块(博客、文档、教程)的站点,请按版块组织:

layouts/index.llmstxt.txt, by section
{{/* layouts/index.llmstxt.txt, organized by section */}}
# {{ .Site.Title }}

> {{ .Site.Params.description }}

{{ range .Site.Sections -}}
## {{ .Title }}

{{ range .Pages -}}
- [{{ .Title }}]({{ .Permalink }}): {{ with .Description }}{{ . }}{{ else }}{{ .Summary | plainify | truncate 100 }}{{ end }}
{{ end }}
{{ end -}}

## Optional

- [Homepage]({{ .Site.BaseURL }}): site entry point.
content/docs/getting-started.md
---
# content/docs/getting-started.md
title: "Getting started"
description: "Install the CLI, run your first command, and deploy in under 5 minutes."
draft: false
---

Your content here...

部署

  • Cloudflare Pages,将构建命令设置为 hugo 并将发布目录设置为 public。Cloudflare Pages 将会提供 public/llms.txt/llms.txt 通过其全球 CDN 提供。请参阅 Cloudflare Pages 指南 用于缓存头建议。
  • Vercel,系统会自动检测 Hugo。将构建命令设置为 hugo 并将输出目录设为 public。请参阅 Vercel 指南 的边缘缓存选项。
  • GitHub Pages,使用运行以下命令的 GitHub Actions 工作流: hugo 并部署 public/gh-pages 分支。该文件将提供于 https://your-org.github.io/llms.txt.
  • 任何静态主机,部署 public/ 目录。文件将位于根目录,并得到正确提供。

验证

先在本地检查:

Local verification
hugo
ls public/llms.txt          # should exist
cat public/llms.txt          # check content
https_proxy="" hugo server
curl http://localhost:1313/llms.txt

部署后:

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

然后将 URL 粘贴到 验证器 以进行完整的规范合规性检查。

相关指南

来源