Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@
"rehype-external-links": "3.0.0",
"rehype-mermaid": "3.0.0",
"rehype-title-figure": "0.1.2",
"remark": "15.0.1",
"sharp": "0.33.5",
"solarflare-theme": "0.0.2",
"starlight-image-zoom": "0.9.0",
"starlight-links-validator": "0.14.1",
"starlight-package-managers": "0.9.0",
"strip-markdown": "6.0.0",
"svgo": "3.3.2",
"tailwindcss": "3.4.17",
"tippy.js": "6.3.7",
Expand Down
2 changes: 1 addition & 1 deletion src/components/overrides/Head.astro
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ if (currentSection) {
}
}

Astro.props.entry.data.description ??= await getPageDescription(
Astro.props.entry.data.description = await getPageDescription(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used to do if (entry.data.description) return entry.data.description; in getPageDescription but that didn't do anything - because we never used this function if that property existed.

That was a bug, but also fine since we never would have changed it. Now we are transforming it so removed ??.

// @ts-expect-error TODO: improve types
Astro.props.entry,
Astro.locals,
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/workers/languages/rust/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sidebar:
head:
- tag: title
content: Cloudflare Workers — Rust language support
description: Write Workers in 100% Rust using the [`workers-rs`crate](https://github.com/cloudflare/workers-rs)
description: Write Workers in 100% Rust using the [`workers-rs` crate](https://github.com/cloudflare/workers-rs)
---

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.
Expand Down
36 changes: 24 additions & 12 deletions src/util/description.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
import type { CollectionEntry } from "astro:content";
import { parse } from "node-html-parser";
import { entryToString } from "./container";
/*
1. If there is a `description` property in the frontmatter, return that.
2. If there is a `<p>...</p>` element in the HTML, return that.
3. Return `undefined` to signal to consumers there is no suitable description.
*/
import { remark } from "remark";
import strip from "strip-markdown";
Comment on lines +4 to +5
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could technically be done in a lot more lines with marked but most of their discussions recommend using other libraries, like strip-markdown.

import he from "he";

/**
* Generates a plain-text description for use in the `description` and `og:description` meta tags.
*
* 1. If there is a `description` property in the frontmatter, strip any Markdown tokens and return.
* 2. If there is a `<p>...</p>` element in the HTML, decode any HTML entities and return that.
* 3. Return `undefined` to signal to consumers there is no suitable description.
*/
export async function getPageDescription(
entry: CollectionEntry<"docs">,
locals: any,
) {
if (entry.data.description) return entry.data.description;
let description = undefined;

if (entry.data.description) {
const file = await remark().use(strip).process(entry.data.description);
Copy link
Member Author

@KianNH KianNH Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[`workers-rs` crate](https://github.com/cloudflare/workers-rs)` -> `workers-rs crate`


const html = await entryToString(entry, locals);
description = file.toString();
} else {
const html = await entryToString(entry, locals);

if (!html) return undefined;
if (!html) return undefined;

const dom = parse(html);
const description = dom.querySelector(":root > p");
const dom = parse(html);
const paragraph = dom.querySelector(":root > p");

if (description) return description.innerText;
if (paragraph) description = he.decode(paragraph.innerText);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&#x3C;body> -> <body>

}

return undefined;
return description?.replaceAll(" ↗", "").trim();
}
Loading