Skip to content

Commit 596e792

Browse files
committed
docs: proper page description
1 parent 887a18c commit 596e792

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+175
-2
lines changed

.vitepress/config.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {Resvg, initWasm as initResvgWasm, type ResvgRenderOptions} from "@resvg/
1616
import {BlogPageInfoPlugin} from "./config/BlogPageInfoPlugin.js";
1717
import {getApiReferenceSidebar} from "./config/apiReferenceSidebar.js";
1818
import {ensureLocalImage} from "./utils/ensureLocalImage.js";
19+
import {getExcerptFromMarkdownFile} from "./utils/getExcerptFromMarkdownFile.js";
1920
import type {Element as HastElement, Parent} from "hast";
2021

2122
import type {Node as UnistNode} from "unist";
@@ -28,6 +29,7 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
2829
const packageJson: typeof import("../package.json") = fs.readJsonSync(path.join(__dirname, "..", "package.json"));
2930
const env = envVar.from(process.env);
3031

32+
const docsDir = path.join(__dirname, "..", "docs");
3133
const urlBase = env.get("DOCS_URL_BASE")
3234
.asString();
3335
const packageVersion = env.get("DOCS_PACKAGE_VERSION")
@@ -249,6 +251,27 @@ export default defineConfig({
249251
}
250252
}
251253

254+
if ((description == null || description === "") && pageData.filePath && !pageData.filePath.startsWith("api/")) {
255+
const excerpt = await getExcerptFromMarkdownFile(await fs.readFile(path.join(docsDir, pageData.filePath), "utf8"));
256+
if (excerpt != null && excerpt !== "")
257+
description = excerpt.replaceAll('"', "'").replaceAll("\n", " ");
258+
}
259+
260+
pageData.description = description;
261+
262+
if (description != null && description !== "" &&
263+
(pageData.frontmatter.description == null || pageData.frontmatter.description === "")
264+
) {
265+
pageData.frontmatter.description = description;
266+
for (let i = 0; i < head.length; i++) {
267+
const header = head[i]!;
268+
if (header[0] === "meta" && header[1]?.name === "description") {
269+
head[i] = ["meta", {name: "description", content: description}];
270+
break;
271+
}
272+
}
273+
}
274+
252275
head.push(["meta", {name: "og:title", content: title}]);
253276
if (description != null && description !== "")
254277
head.push(["meta", {name: "og:description", content: description}]);
@@ -311,7 +334,7 @@ export default defineConfig({
311334
plugins: [
312335
GitChangelog({
313336
repoURL: () => "https://github.com/withcatai/node-llama-cpp",
314-
cwd: path.join(__dirname, "..", "docs")
337+
cwd: docsDir
315338
}) as VitepressPlugin,
316339
GitChangelogMarkdownSection({
317340
exclude: (id) => (
@@ -708,7 +731,7 @@ export default defineConfig({
708731
});
709732

710733
for (const {url, excerpt, frontmatter, html} of blogPosts) {
711-
const ogImageElement = findElementInHtml(html, (element) => element.tagName === "meta" && element.properties?.name === "og:imag");
734+
const ogImageElement = findElementInHtml(html, (element) => element.tagName === "meta" && element.properties?.name === "og:image");
712735
const date = new Date(frontmatter.date);
713736
if (Number.isNaN(date.getTime()))
714737
throw new Error(`Invalid date for blog post: ${url}`);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import {getMarkdownRenderer} from "./getMarkdownRenderer.js";
2+
3+
export async function getExcerptFromMarkdownFile(
4+
markdownContent: string,
5+
removeTitle: boolean = true,
6+
maxLength: number = 80
7+
) {
8+
const renderer = await getMarkdownRenderer();
9+
let content = markdownContent.trim().replaceAll("\r\n", "\n");
10+
11+
if (content.startsWith("---")) {
12+
const frontMatterEndIndex = content.indexOf("\n---", "---".length);
13+
const nextNewLine = content.indexOf("\n", frontMatterEndIndex + "\n---".length);
14+
if (frontMatterEndIndex >= 0 && nextNewLine >= 0)
15+
content = content.slice(nextNewLine + 1).trim();
16+
}
17+
18+
if (removeTitle && content.startsWith("# ")) {
19+
const nextNewLine = content.indexOf("\n");
20+
if (nextNewLine >= 0)
21+
content = content.slice(nextNewLine + "\n".length).trim();
22+
}
23+
24+
const renderedText = markdownToPlainText(renderer, content).trim();
25+
26+
if (renderedText.length > maxLength) {
27+
if (renderedText[maxLength] === " ")
28+
return renderedText.slice(0, maxLength);
29+
30+
const lastSpaceIndex = renderedText.lastIndexOf(" ", maxLength);
31+
if (lastSpaceIndex >= 0)
32+
return renderedText.slice(0, lastSpaceIndex);
33+
34+
return renderedText.slice(0, maxLength);
35+
}
36+
37+
return renderedText;
38+
}
39+
40+
function markdownToPlainText(
41+
markdownIt: Awaited<ReturnType<typeof getMarkdownRenderer>>,
42+
markdown: string,
43+
includeNotes: boolean = false,
44+
includeCode: boolean = false
45+
) {
46+
const env = {};
47+
const pageTokens = markdownIt.parse(markdown, env);
48+
49+
function toText(tokens: typeof pageTokens) {
50+
let text = "";
51+
let addedParagraphSpace = false;
52+
53+
for (const token of tokens) {
54+
if (!includeNotes && token.type === "inline" && token.level === 2)
55+
continue;
56+
57+
if (token.children != null) {
58+
const childrenText = toText(token.children);
59+
if (addedParagraphSpace && childrenText.startsWith(" "))
60+
text += childrenText.slice(" ".length);
61+
else
62+
text += childrenText;
63+
} else if (
64+
["text", "code_block", "code_inline", "emoji"].includes(token.type) ||
65+
(includeCode && ["fence"].includes(token.type))
66+
) {
67+
if (addedParagraphSpace && token.content.startsWith(" "))
68+
text += token.content.slice(" ".length);
69+
else
70+
text += token.content;
71+
72+
addedParagraphSpace = false;
73+
} else if (token.type.endsWith("_close")) {
74+
text += " ";
75+
addedParagraphSpace = true;
76+
}
77+
}
78+
79+
return text;
80+
}
81+
82+
return toText(pageTokens);
83+
}

docs/cli/chat.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
outline: deep
3+
description: "'chat' command reference"
34
---
45
# `chat` command
56

docs/cli/complete.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
outline: deep
3+
description: "'complete' command reference"
34
---
45
# `complete` command
56

docs/cli/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
outline: deep
3+
description: CLI command reference
34
---
45
# CLI
56

docs/cli/infill.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
outline: deep
3+
description: "'infill' command reference"
34
---
45
# `infill` command
56

docs/cli/init.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
outline: deep
3+
description: "'init' command reference"
34
---
45
# `init` command
56

docs/cli/inspect.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
outline: deep
3+
description: "'inspect' command reference"
34
---
45
# `inspect` command
56

docs/cli/inspect/estimate.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
outline: deep
3+
description: "'inspect estimate' command reference"
34
---
45
# `inspect estimate` command
56

docs/cli/inspect/gguf.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
outline: deep
3+
description: "'inspect gguf' command reference"
34
---
45
# `inspect gguf` command
56

0 commit comments

Comments
 (0)