Skip to content

Commit 11f2063

Browse files
authored
[Docs Site] Decode HTML entities and strip Markdown from page descriptions (#19350)
1 parent fa11541 commit 11f2063

File tree

5 files changed

+61
-14
lines changed

5 files changed

+61
-14
lines changed

package-lock.json

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,13 @@
8888
"rehype-external-links": "3.0.0",
8989
"rehype-mermaid": "3.0.0",
9090
"rehype-title-figure": "0.1.2",
91+
"remark": "15.0.1",
9192
"sharp": "0.33.5",
9293
"solarflare-theme": "0.0.2",
9394
"starlight-image-zoom": "0.9.0",
9495
"starlight-links-validator": "0.14.1",
9596
"starlight-package-managers": "0.9.0",
97+
"strip-markdown": "6.0.0",
9698
"svgo": "3.3.2",
9799
"tailwindcss": "3.4.17",
98100
"tippy.js": "6.3.7",

src/components/overrides/Head.astro

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ if (currentSection) {
139139
}
140140
}
141141
142-
Astro.props.entry.data.description ??= await getPageDescription(
142+
Astro.props.entry.data.description = await getPageDescription(
143143
// @ts-expect-error TODO: improve types
144144
Astro.props.entry,
145145
Astro.locals,

src/content/docs/workers/languages/rust/index.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ sidebar:
88
head:
99
- tag: title
1010
content: Cloudflare Workers — Rust language support
11-
description: Write Workers in 100% Rust using the [`workers-rs`crate](https://github.com/cloudflare/workers-rs)
11+
description: Write Workers in 100% Rust using the [`workers-rs` crate](https://github.com/cloudflare/workers-rs)
1212
---
1313

1414
Cloudflare Workers provides support for Rust via the [`workers-rs` crate](https://github.com/cloudflare/workers-rs), which makes [Runtime APIs](/workers/runtime-apis) and [bindings](/workers/runtime-apis/bindings/) to developer platform products, such as [Workers KV](/kv/concepts/how-kv-works/), [R2](/r2/), and [Queues](/queues/), available directly from your Rust code.

src/util/description.ts

+24-12
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
import type { CollectionEntry } from "astro:content";
22
import { parse } from "node-html-parser";
33
import { entryToString } from "./container";
4-
/*
5-
1. If there is a `description` property in the frontmatter, return that.
6-
2. If there is a `<p>...</p>` element in the HTML, return that.
7-
3. Return `undefined` to signal to consumers there is no suitable description.
8-
*/
4+
import { remark } from "remark";
5+
import strip from "strip-markdown";
6+
import he from "he";
7+
8+
/**
9+
* Generates a plain-text description for use in the `description` and `og:description` meta tags.
10+
*
11+
* 1. If there is a `description` property in the frontmatter, strip any Markdown tokens and return.
12+
* 2. If there is a `<p>...</p>` element in the HTML, decode any HTML entities and return that.
13+
* 3. Return `undefined` to signal to consumers there is no suitable description.
14+
*/
915
export async function getPageDescription(
1016
entry: CollectionEntry<"docs">,
1117
locals: any,
1218
) {
13-
if (entry.data.description) return entry.data.description;
19+
let description = undefined;
20+
21+
if (entry.data.description) {
22+
const file = await remark().use(strip).process(entry.data.description);
1423

15-
const html = await entryToString(entry, locals);
24+
description = file.toString();
25+
} else {
26+
const html = await entryToString(entry, locals);
1627

17-
if (!html) return undefined;
28+
if (!html) return undefined;
1829

19-
const dom = parse(html);
20-
const description = dom.querySelector(":root > p");
30+
const dom = parse(html);
31+
const paragraph = dom.querySelector(":root > p");
2132

22-
if (description) return description.innerText;
33+
if (paragraph) description = he.decode(paragraph.innerText);
34+
}
2335

24-
return undefined;
36+
return description?.replaceAll(" ↗", "").trim();
2537
}

0 commit comments

Comments
 (0)