From 070b94aabc4ca63c8e3da56ddbeb6ef20256558d Mon Sep 17 00:00:00 2001 From: Shubham Padia Date: Thu, 3 Jul 2025 16:21:09 +0000 Subject: [PATCH] help-beta: Set first paragraph of content as meta.description. Fixes #35115. --- help-beta/astro.config.mjs | 1 + help-beta/package.json | 7 +++++++ help-beta/src/route_data.ts | 36 ++++++++++++++++++++++++++++++++++++ pnpm-lock.yaml | 28 ++++++++++++++++++++++++++++ version.py | 2 +- 5 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 help-beta/src/route_data.ts diff --git a/help-beta/astro.config.mjs b/help-beta/astro.config.mjs index fb33a425d0..5ecfeec138 100644 --- a/help-beta/astro.config.mjs +++ b/help-beta/astro.config.mjs @@ -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: [ { diff --git a/help-beta/package.json b/help-beta/package.json index 76da7db7ff..82485664c0 100644 --- a/help-beta/package.json +++ b/help-beta/package.json @@ -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" } } diff --git a/help-beta/src/route_data.ts b/help-beta/src/route_data.ts new file mode 100644 index 0000000000..544109a187 --- /dev/null +++ b/help-beta/src/route_data.ts @@ -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), + }, + }); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ddd8163fde..f922e41552 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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 diff --git a/version.py b/version.py index b483c112b1..7944fa03a9 100644 --- a/version.py +++ b/version.py @@ -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