help-beta: Set first paragraph of content as meta.description.

Fixes #35115.
This commit is contained in:
Shubham Padia
2025-07-03 16:21:09 +00:00
committed by Tim Abbott
parent c03328eeee
commit 070b94aabc
5 changed files with 73 additions and 1 deletions

View File

@@ -67,6 +67,7 @@ export default defineConfig({
Head: "./src/components/Head.astro",
},
pagination: false,
routeMiddleware: "./src/route_data.ts",
customCss: ["./src/styles/main.css", "./src/styles/steps.css"],
sidebar: [
{

View File

@@ -17,8 +17,15 @@
"astro": "^5.1.2",
"hast-util-from-html": "^2.0.3",
"hast-util-to-html": "^9.0.5",
"mdast-util-to-string": "^4.0.0",
"remark": "^15.0.1",
"remark-mdx": "^3.1.0",
"sharp": "^0.34.1",
"typescript": "^5.4.5",
"unist-util-visit": "^5.0.0",
"unplugin-icons": "^22.1.0"
},
"devDependencies": {
"@types/mdast": "^4.0.4"
}
}

View File

@@ -0,0 +1,36 @@
import assert from "node:assert";
import {defineRouteMiddleware} from "@astrojs/starlight/route-data";
import type {Paragraph} from "mdast";
import {toString} from "mdast-util-to-string";
import {remark} from "remark";
import remarkMdx from "remark-mdx";
import {visit} from "unist-util-visit";
function extractFirstParagraph(content: string): string | undefined {
const tree = remark().use(remarkMdx).parse(content);
let firstParagraph: string | undefined;
visit(tree, "paragraph", (node: Paragraph) => {
if (!firstParagraph) {
// We need to convert the node to string so that links, emphasis, etc.
// are converted to plain text.
firstParagraph = toString(node);
firstParagraph = firstParagraph.replaceAll(/\s+/g, " ").trim();
}
});
return firstParagraph;
}
export const onRequest = defineRouteMiddleware((context) => {
assert.ok(typeof context.locals.starlightRoute.entry.body === "string");
context.locals.starlightRoute.head.push({
tag: "meta",
attrs: {
name: "description",
content: extractFirstParagraph(context.locals.starlightRoute.entry.body),
},
});
});

28
pnpm-lock.yaml generated
View File

@@ -539,15 +539,31 @@ importers:
hast-util-to-html:
specifier: ^9.0.5
version: 9.0.5
mdast-util-to-string:
specifier: ^4.0.0
version: 4.0.0
remark:
specifier: ^15.0.1
version: 15.0.1
remark-mdx:
specifier: ^3.1.0
version: 3.1.0
sharp:
specifier: ^0.34.1
version: 0.34.2
typescript:
specifier: ^5.4.5
version: 5.8.3
unist-util-visit:
specifier: ^5.0.0
version: 5.0.0
unplugin-icons:
specifier: ^22.1.0
version: 22.1.0
devDependencies:
'@types/mdast':
specifier: ^4.0.4
version: 4.0.4
packages:
@@ -7921,6 +7937,9 @@ packages:
remark-stringify@11.0.0:
resolution: {integrity: sha512-1OSmLd3awB/t8qdoEOMazZkNsfVTeY4fTsgzcQFdXNq8ToTN4ZGwrMnlda4K6smTFKD+GRV6O48i6Z4iKgPPpw==}
remark@15.0.1:
resolution: {integrity: sha512-Eht5w30ruCXgFmxVUSlNWQ9iiimq07URKeFS3hNc8cUWy1llX4KDWfyEDZRycMc+znsN9Ux5/tJ/BFdgdOwA3A==}
remove-bom-buffer@3.0.0:
resolution: {integrity: sha512-8v2rWhaakv18qcvNeli2mZ/TMTL2nEyAKRvzo1WtnZBl15SHyEhrCu2/xKlJyUFKHiHgfXIyuY6g2dObJJycXQ==}
engines: {node: '>=0.10.0'}
@@ -18663,6 +18682,15 @@ snapshots:
mdast-util-to-markdown: 2.1.2
unified: 11.0.5
remark@15.0.1:
dependencies:
'@types/mdast': 4.0.4
remark-parse: 11.0.0
remark-stringify: 11.0.0
unified: 11.0.5
transitivePeerDependencies:
- supports-color
remove-bom-buffer@3.0.0:
dependencies:
is-buffer: 1.1.6

View File

@@ -49,4 +49,4 @@ API_FEATURE_LEVEL = 406
# historical commits sharing the same major version, in which case a
# minor version bump suffices.
PROVISION_VERSION = (334, 0) # bumped 2025-07-14 to upgrade Python requirements
PROVISION_VERSION = (334, 1) # bumped 2025-07-16 to add some remark libraries