-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[Docs Site] Decode HTML entities and strip Markdown from page descriptions #19350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could technically be done in a lot more lines with |
||
| 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); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| 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); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| return undefined; | ||
| return description?.replaceAll(" ↗", "").trim(); | ||
| } | ||
There was a problem hiding this comment.
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;ingetPageDescriptionbut 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
??.