|
| 1 | +// lib/llms-txt.ts |
| 2 | + |
| 3 | +/** |
| 4 | + * @file lib/llms-txt.ts |
| 5 | + * @description |
| 6 | + * `/llms.txt` 正文生成器(llmstxt.org 约定)。 |
| 7 | + * |
| 8 | + * 干什么用:AI 引擎(ChatGPT / Perplexity / Claude 等)抓站时先看这个文件, |
| 9 | + * 拿到一份无导航噪音的全站索引,再决定深入读哪几篇。sitemap.xml 只有 URL, |
| 10 | + * 模型得逐个抓才知道讲什么;llms.txt 一次给全 标题 + 描述 + 链接。 |
| 11 | + * |
| 12 | + * 只做索引不做全文:站内 152 篇中文 + 152 篇英文,全文拼进去是几 MB, |
| 13 | + * 反而挤爆上下文。约定本身也是「索引 + 链接」。 |
| 14 | + * |
| 15 | + * 纯函数,不 import `@/lib/source`(那条链会拖进 fumadocs-mdx 管线, |
| 16 | + * vitest 起不来)。枚举文档的活儿在 `app/llms.txt/route.ts` 里干。 |
| 17 | + */ |
| 18 | + |
| 19 | +export interface LlmsTxtEntry { |
| 20 | + /** 站内绝对路径,如 `/zh/docs/career/xxx`,不含域名 */ |
| 21 | + pathname: string; |
| 22 | + title: string; |
| 23 | + /** frontmatter description,可能缺失或为空 */ |
| 24 | + description?: string; |
| 25 | + /** 分组小标题,同一个值的条目会归到一起,按首次出现顺序排列 */ |
| 26 | + section: string; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * 单条描述的长度上限。个别 frontmatter 描述写得极长(有的贡献者把整段 |
| 31 | + * 摘要塞进去),不截断的话少数几篇就能把索引撑肥一倍,挤掉别的条目 |
| 32 | + * 被读到的机会。300 够表达一篇文档讲什么了。 |
| 33 | + */ |
| 34 | +const MAX_DESCRIPTION = 300; |
| 35 | + |
| 36 | +const HEADER = `# Involution Hell(内卷地狱) |
| 37 | +
|
| 38 | +> 面向留学生与求职者的开源社区知识库:算法题解、系统设计、面试经验与求职指南。内容由社区贡献者共同维护。 |
| 39 | +
|
| 40 | +本文件是全站文档索引,供 AI 引擎检索与引用。中文文档在 \`/zh/docs/\`,英文在 \`/en/docs/\`; |
| 41 | +某篇没有英文版时,\`/en/\` 路径会回退渲染中文原文。 |
| 42 | +
|
| 43 | +内容采用 CC BY-NC-SA 4.0 许可:可以引用和转述,请保留出处链接并注明来源;禁止商业化再分发。 |
| 44 | +`; |
| 45 | + |
| 46 | +/** 折叠空白 + 去掉会破坏 markdown 链接的方括号。 */ |
| 47 | +function clean(text: string): string { |
| 48 | + return text.replace(/\s+/g, " ").replace(/[[\]]/g, "").trim(); |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * 把文档条目渲染成 llms.txt 正文。 |
| 53 | + * |
| 54 | + * @param entries 全部文档条目,分组顺序由 `section` 首次出现的顺序决定 |
| 55 | + * @param siteUrl 站点根 URL(不带尾斜杠),拼成绝对链接 —— 相对链接对 |
| 56 | + * 抓取方没用,它们不一定知道自己是从哪个域名拿到这个文件的 |
| 57 | + */ |
| 58 | +export function buildLlmsTxt(entries: LlmsTxtEntry[], siteUrl: string): string { |
| 59 | + const groups = new Map<string, LlmsTxtEntry[]>(); |
| 60 | + for (const entry of entries) { |
| 61 | + const bucket = groups.get(entry.section); |
| 62 | + if (bucket) { |
| 63 | + bucket.push(entry); |
| 64 | + } else { |
| 65 | + groups.set(entry.section, [entry]); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + const lines: string[] = [HEADER]; |
| 70 | + for (const [section, items] of groups) { |
| 71 | + lines.push(`## ${section}`, ""); |
| 72 | + for (const item of items) { |
| 73 | + const title = clean(item.title) || item.pathname; |
| 74 | + const raw = item.description ? clean(item.description) : ""; |
| 75 | + const description = |
| 76 | + raw.length > MAX_DESCRIPTION |
| 77 | + ? `${raw.slice(0, MAX_DESCRIPTION).trimEnd()}…` |
| 78 | + : raw; |
| 79 | + const link = `- [${title}](${siteUrl}${item.pathname})`; |
| 80 | + lines.push(description ? `${link}: ${description}` : link); |
| 81 | + } |
| 82 | + lines.push(""); |
| 83 | + } |
| 84 | + |
| 85 | + return `${lines.join("\n").trimEnd()}\n`; |
| 86 | +} |
0 commit comments