diff --git a/.vitepress/config.mts b/.vitepress/config.mts index b946bc31c..d6ab4730f 100644 --- a/.vitepress/config.mts +++ b/.vitepress/config.mts @@ -10,6 +10,7 @@ import { capabilityCards } from "./capability"; import { mermaidFences } from "./mermaid"; import { literalBraces } from "./braces"; import { pairedAnchors } from "./anchors"; +import { excerptOf } from "./excerpt"; const SITE = "https://docs.clice.io"; const projects = ["clice", "catter", "xclang"]; @@ -55,10 +56,15 @@ export default defineConfigWithTheme]+>/g, "") + .replace(/(\*\*|__)(.+?)\1/g, "$2") + .replace(/(\*|_)(.+?)\1/g, "$2") + .replace(/`([^`]*)`/g, "$1") + .replace(/\s+/g, " ") + .trim(); +} + +/** At most `max` characters, cut at the last sentence end that leaves + * most of it, else at a word, with an ellipsis. */ +function clip(text: string): string { + const cjk = (text.match(/[㐀-鿿]/g)?.length ?? 0) > text.length / 4; + const max = cjk ? CJK_MAX : LATIN_MAX; + // A paragraph leading into a list goes on: say so. + const whole = text.replace(/[::]$/, "…"); + if (whole.length <= max) return whole; + const head = whole.slice(0, max); + const end = Math.max(head.lastIndexOf(". "), head.lastIndexOf("。")); + if (end > max * 0.6) return head.slice(0, end + 1); + // Latin text breaks at a space; CJK anywhere, but not inside a Latin word. + let cut = cjk ? max - 1 : head.lastIndexOf(" "); + if (cjk) while (cut > max * 0.6 && /[A-Za-z0-9]/.test(head[cut - 1]!) && /[A-Za-z0-9]/.test(head[cut]!)) cut -= 1; + return `${head.slice(0, cut > max * 0.6 ? cut : max - 1).replace(/[,,;;::、\s]+$/, "")}…`; +} + +function firstParagraph(body: string): string { + let fence = false; + for (const block of body.split(/\n\s*\n/)) { + const text = block.trim(); + const fences = (text.match(/^(```|~~~)/gm) ?? []).length; + if (fence || fences % 2 === 1) { + if (fences % 2 === 1) fence = !fence; + continue; + } + // Prose only: no heading, list, table, quote, fence, container, + // HTML or component, image. + if (!text || /^(#|[-*+] |\d+\. |\||>|```|~~~|:::|<|!\[|\[\^)/.test(text)) continue; + const prose = plain(text); + if (prose.length >= 20) return prose; + } + return ""; +} + +export function excerptOf(file: string): string { + if (!fs.existsSync(file)) return ""; + const source = fs.readFileSync(file, "utf8").replaceAll("\r\n", "\n"); + const match = /^---\n([\s\S]*?)\n---(\n|$)/.exec(source); + const front = (match ? yaml.load(match[1]!) : null) as { hero?: { text?: string; tagline?: string } } | null; + const hero = front?.hero; + if (hero?.text || hero?.tagline) { + const parts = [hero.text, hero.tagline].filter((part): part is string => !!part).map(plain); + const cjk = /[㐀-鿿]/.test(parts.join("")); + // `text` is a title: it takes a full stop before the tagline. + const joined = parts.map((part, i) => (i < parts.length - 1 && !/[.。!!??]$/.test(part) ? part + (cjk ? "。" : ".") : part)); + return clip(joined.join(cjk ? "" : " ")); + } + const paragraph = firstParagraph(match ? source.slice(match[0].length) : source); + return paragraph ? clip(paragraph) : ""; +} diff --git a/en/index.md b/en/index.md index a325a2c36..d5845b3e7 100644 --- a/en/index.md +++ b/en/index.md @@ -1,6 +1,6 @@ --- title: clice.io -description: Next generation C++ tooling +description: "Next generation C++ tooling: clice, a C/C++ language server redesigned from the ground up; catter, which gets the compile commands out of any build; xclang, a cross-compiling Clang toolchain; and kotatsu, the C++23 library beneath them." layout: page outline: false --- diff --git a/zh/index.md b/zh/index.md index ed704e926..b360d378f 100644 --- a/zh/index.md +++ b/zh/index.md @@ -1,6 +1,6 @@ --- title: clice.io -description: 下一代 C++ 工具链 +description: "下一代 C++ 工具链:从头重新设计的 C/C++ 语言服务器 clice,能从任何构建里拿到编译命令的 catter,开箱即用的交叉编译 Clang 工具链 xclang,以及撑起它们的 C++23 基础库 kotatsu。" layout: page outline: false ---