From af780020144846663145960a5a508dc35aad8127 Mon Sep 17 00:00:00 2001 From: Xin <5097752+imfing@users.noreply.github.com> Date: Sun, 10 Aug 2025 23:15:21 +0800 Subject: [PATCH] feat: support llms.txt output (#736) * feat(llms): add llms txt output format - Introduced a new output format 'llms' in the configuration. - Updated the example site to utilize the new 'llms' format for the home output. - Added a new layout file 'llms.txt' for rendering content in the LLMS format. * docs: add llms.txt support documentation - Introduced a new section in the configuration guide detailing how to enable the llms.txt output format for improved accessibility to large language models. - Provided example configuration for adding the 'llms' output format in the site's hugo.yaml. - Explained the content structure of the generated llms.txt file, including site title, section listings, page summaries, and direct links. * Update configuration.md --- .../content/docs/guide/configuration.md | 20 +++++++++++ exampleSite/hugo.yaml | 6 ++-- hugo.toml | 10 ++++++ layouts/llms.txt | 36 +++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 hugo.toml create mode 100644 layouts/llms.txt diff --git a/exampleSite/content/docs/guide/configuration.md b/exampleSite/content/docs/guide/configuration.md index 3ca2a54..9148c64 100644 --- a/exampleSite/content/docs/guide/configuration.md +++ b/exampleSite/content/docs/guide/configuration.md @@ -350,6 +350,26 @@ To exclude an entire directory, use the [`cascade`](https://gohugo.io/configurat > To block search crawlers, you can make a [`robots.txt` template](https://gohugo.io/templates/robots/). > However, `robots.txt` instructions do not necessarily keep a page out of Google search results. +### LLMS.txt Support + +To enable [llms.txt](https://llmstxt.org/) output format for your site, which provides a structured text outline for [large language models](https://en.wikipedia.org/wiki/Large_language_model) and AI agents, add the `llms` output format to your site's `hugo.yaml`: + +```diff {filename="hugo.yaml"} +outputs: +- home: [html] ++ home: [html, llms] + page: [html] + section: [html, rss] +``` + +This will generate an `llms.txt` file at your site's root containing: +- Site title and description +- Hierarchical listing of all sections and pages +- Page summaries and publication dates +- Direct links to all content + +The llms.txt file is automatically generated from your content structure and makes your site more accessible to AI tools and language models for context and reference. + ### Open Graph To add [Open Graph](https://ogp.me/) metadata to a page, add values in the frontmatter params. diff --git a/exampleSite/hugo.yaml b/exampleSite/hugo.yaml index 6d884ef..0263549 100644 --- a/exampleSite/hugo.yaml +++ b/exampleSite/hugo.yaml @@ -13,9 +13,9 @@ hasCJKLanguage: true # ID: G-MEASUREMENT_ID outputs: - home: [HTML] - page: [HTML] - section: [HTML, RSS] + home: [html, llms] + page: [html] + section: [html, rss] defaultContentLanguage: en languages: diff --git a/hugo.toml b/hugo.toml new file mode 100644 index 0000000..1a31083 --- /dev/null +++ b/hugo.toml @@ -0,0 +1,10 @@ +[module] + [module.hugoVersion] + min = '0.146.0' + +[outputFormats] + [outputFormats.llms] + name= 'llms' + baseName = 'llms' + mediaType = 'text/plain' + isPlainText = true diff --git a/layouts/llms.txt b/layouts/llms.txt new file mode 100644 index 0000000..65a9159 --- /dev/null +++ b/layouts/llms.txt @@ -0,0 +1,36 @@ +# {{ .Site.Title }} + +> {{ .Site.Params.description }} + +{{ range $section := site.Sections }} +{{- template "llms-section-tree" dict "context" . "level" 2 }} +{{ end }} + +{{- $rootPages := where site.RegularPages "Section" "" }} +{{- if $rootPages }} + +## Root Pages +{{- range $rootPages }} +- [{{ .Title }}]({{ .Permalink }}): {{ .Summary | plainify | truncate 100 | strings.TrimSpace }}{{ if .Date }} - Published {{ .Date.Format "2006-01-02" }}{{ end }} +{{- end }} +{{- end }} + +--- +Generated on {{ now.Format "2006-01-02 15:04:05 UTC" }} +Site: {{ .Site.BaseURL }} + +{{- define "llms-section-tree" -}} +{{- $context := .context -}} +{{- $level := .level | default 2 -}} +{{- $headerHashes := strings.Repeat $level "#" -}} +{{- "\n" -}} +{{ $headerHashes }} {{ $context.Title }} + +{{- range $context.RegularPages }} +- [{{ .Title }}]({{ .Permalink }}): {{ .Summary | plainify | truncate 100 | strings.TrimSpace }}{{ if .Date }} - Published {{ .Date.Format "2006-01-02" }}{{ end }} +{{- end }} + +{{- range $context.Sections }} +{{ template "llms-section-tree" dict "context" . "level" (add $level 1) }} +{{- end }} +{{- end -}}