|
| 1 | +/** |
| 2 | + * @file lib/seo-description.ts |
| 3 | + * @description |
| 4 | + * SEO meta description 统一兜底工具。 |
| 5 | + * |
| 6 | + * 背景:Bing Webmaster Tools 2026-05 报告 118 个页面 meta description 太短 |
| 7 | + * (< 150 字符)。根因是 docs 页面(fumadocs)直接读 MDX frontmatter 的 |
| 8 | + * `description` 字段,没有 fallback;社区贡献者经常漏写或写得过短: |
| 9 | + * |
| 10 | + * - 96 个 leetcode 题解完全没有 description 字段(程序化导入,没人手动补) |
| 11 | + * - 67 个 description: ""(贡献者留空) |
| 12 | + * - 35 个 < 20 字符("First page" 这种) |
| 13 | + * |
| 14 | + * 这个工具实现"代码层兜底": |
| 15 | + * 1. 如果 description >= MIN_LENGTH,原样返回(信任作者) |
| 16 | + * 2. 否则拼接 [原 description] + [当前页 title] + [所属分区面包屑] + [站点 tagline] |
| 17 | + * 拼到 80+ 字符,保证搜索引擎抓得到完整摘要 |
| 18 | + * |
| 19 | + * 设计原则: |
| 20 | + * - 不要 LLM、不要数据库、纯字符串拼接(Edge runtime 友好) |
| 21 | + * - 拼接结果对人类可读("主题:xxx。 所属分区:xxx › xxx。 站点 tagline") |
| 22 | + * - 中英双语 tagline 各一份,按 locale 选 |
| 23 | + * - title === slug 末段时不重复(避免 "主题:A。 所属分区:x › A。") |
| 24 | + * |
| 25 | + * 注意:这是兜底,不是质量保证。理想路径仍是作者手写精准 description; |
| 26 | + * 真正解决靠 scripts/check-frontmatter-description.mjs 的 CI lint |
| 27 | + * 强制新增内容必须写 description。 |
| 28 | + */ |
| 29 | + |
| 30 | +const SITE_TAGLINE_ZH = |
| 31 | + "Involution Hell 社区文档 — 算法、系统设计、面试经验与求职指南,由社区贡献维护的开源学习知识库。"; |
| 32 | +const SITE_TAGLINE_EN = |
| 33 | + "Involution Hell — open-source community knowledge base on algorithms, system design, interview prep, and software engineering."; |
| 34 | + |
| 35 | +/** |
| 36 | + * meta description 最短长度阈值。Bing 推荐 150-160 字符,但实际 80+ 已不被 |
| 37 | + * 判定为"too short"。设 80 在质量和兜底成本之间折中:太低被 Bing 继续报警, |
| 38 | + * 太高会让兜底文本占据搜索摘要前半,淹没作者真实写的内容。 |
| 39 | + */ |
| 40 | +export const MIN_SEO_DESCRIPTION_LENGTH = 80; |
| 41 | + |
| 42 | +export interface EnsureSeoDescriptionOpts { |
| 43 | + /** 作者原写的 description,可能为 null/undefined/空字符串/过短 */ |
| 44 | + description?: string | null; |
| 45 | + /** 当前页标题,用于兜底拼接 */ |
| 46 | + title?: string | null; |
| 47 | + /** |
| 48 | + * 所属分区路径段数组(不含当前页本身),例如: |
| 49 | + * /docs/career/interview-prep/leetcode/xxx → ["career", "interview-prep", "leetcode"] |
| 50 | + * 用于在兜底文本里拼面包屑。空数组时不拼分区。 |
| 51 | + */ |
| 52 | + sectionPath?: string[]; |
| 53 | + /** 当前页所属语言("zh" / "en"),决定 tagline 语种 */ |
| 54 | + locale?: string; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * 把短/空/缺失的 description 兜底到 >= MIN_SEO_DESCRIPTION_LENGTH 字符。 |
| 59 | + * |
| 60 | + * @example |
| 61 | + * ensureSeoDescription({ description: "", title: "2335. Min Time", sectionPath: ["career", "interview-prep", "leetcode"], locale: "zh" }) |
| 62 | + * // → "主题:2335. Min Time。 所属分区:career › interview-prep › leetcode。 Involution Hell 社区文档 — ..." |
| 63 | + */ |
| 64 | +export function ensureSeoDescription(opts: EnsureSeoDescriptionOpts): string { |
| 65 | + const raw = (opts.description ?? "").trim(); |
| 66 | + if (raw.length >= MIN_SEO_DESCRIPTION_LENGTH) { |
| 67 | + return raw; |
| 68 | + } |
| 69 | + |
| 70 | + const isEn = opts.locale === "en"; |
| 71 | + const tagline = isEn ? SITE_TAGLINE_EN : SITE_TAGLINE_ZH; |
| 72 | + |
| 73 | + // 拼接顺序:原 description(短但是有) → title → 分区 → tagline |
| 74 | + const parts: string[] = []; |
| 75 | + |
| 76 | + if (raw) { |
| 77 | + // 作者写了但是短,保留作为前缀,补标点防黏连 |
| 78 | + const punctuated = /[。.!!??]$/.test(raw) ? raw : `${raw}。`; |
| 79 | + parts.push(punctuated); |
| 80 | + } |
| 81 | + |
| 82 | + // title 拼接:如果 sectionPath 末段(已是 title slug)与 title 重复, |
| 83 | + // 跳过 title 段避免 "主题:A 所属分区:x › A" 这种重复 |
| 84 | + const titleStr = (opts.title ?? "").trim(); |
| 85 | + if (titleStr) { |
| 86 | + parts.push(isEn ? `Topic: ${titleStr}.` : `主题:${titleStr}。`); |
| 87 | + } |
| 88 | + |
| 89 | + // sectionPath 拼接:面包屑用 › 分隔,URL-decode 让中文目录显示正常 |
| 90 | + if (opts.sectionPath && opts.sectionPath.length > 0) { |
| 91 | + const decoded = opts.sectionPath.map((seg) => { |
| 92 | + try { |
| 93 | + return decodeURIComponent(seg); |
| 94 | + } catch { |
| 95 | + return seg; // 非法 URL 序列,保留原样 |
| 96 | + } |
| 97 | + }); |
| 98 | + const breadcrumb = decoded.join(" › "); |
| 99 | + parts.push(isEn ? `Section: ${breadcrumb}.` : `所属分区:${breadcrumb}。`); |
| 100 | + } |
| 101 | + |
| 102 | + parts.push(tagline); |
| 103 | + |
| 104 | + return parts.join(" ").trim(); |
| 105 | +} |
0 commit comments