Skip to content

Commit 7ee4776

Browse files
authored
[Docs Site] Add frontmatter to index.md output (#21980)
* [Docs Site] Add frontmatter to index.md output * rebase + update test * fix title and desc * hardcode lastUpdated
1 parent 5ab1a24 commit 7ee4776

File tree

6 files changed

+62
-10
lines changed

6 files changed

+62
-10
lines changed

src/components/overrides/Head.astro

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getEntry } from "astro:content";
77
import { getOgImage } from "~/util/og";
88
import type { CollectionEntry } from "astro:content";
99
10+
const DEFAULT_TITLE_DELIMITER = "|";
1011
const NOINDEX_PRODUCTS = ["email-security", "style-guide", "security"];
1112
1213
const currentSection = Astro.url.pathname.split("/")[1].replaceAll(".", "");
@@ -31,7 +32,9 @@ if (currentSection) {
3132
let title: string;
3233
3334
if (titleIdx !== -1) {
34-
const existingTitle = head[titleIdx].content;
35+
const existingTitle = head[titleIdx].content?.split(
36+
` ${DEFAULT_TITLE_DELIMITER} `,
37+
)[0];
3538
title = `${existingTitle} · ${product.data.meta.title}`;
3639
3740
head[titleIdx] = {
@@ -80,6 +83,26 @@ if (shouldNoIndex) {
8083
});
8184
}
8285
86+
if (
87+
frontmatter.description &&
88+
head.findIndex(
89+
({ tag, attrs }) => tag === "meta" && attrs?.name === "description",
90+
) === -1
91+
) {
92+
const existingOpenGraphTag = head.findIndex(
93+
({ tag, attrs }) => tag === "meta" && attrs?.property === "og:description",
94+
);
95+
96+
if (existingOpenGraphTag !== -1) {
97+
head[existingOpenGraphTag].attrs!.content = frontmatter.description;
98+
}
99+
100+
metaTags.push({
101+
name: "description",
102+
content: frontmatter.description as string,
103+
});
104+
}
105+
83106
if (frontmatter.pcx_content_type) {
84107
["pcx_content_type", "algolia_content_type"].map((name) => {
85108
metaTags.push({

src/content/docs/style-guide/fixtures/markdown.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: Markdown
33
noindex: true
4+
lastUpdated: 2025-01-01T00:00:00Z
45
sidebar:
56
hidden: true
67
---

src/middleware/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const onRequest = defineMiddleware(async (context, next) => {
1818
const htmlUrl = new URL(pathname.replace("index.md", ""), context.url);
1919
const html = await (await fetch(htmlUrl)).text();
2020

21-
const markdown = await htmlToMarkdown(html);
21+
const markdown = await htmlToMarkdown(html, context.url.toString());
2222

2323
if (!markdown) {
2424
return new Response("Not Found", { status: 404 });

src/util/markdown.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import remarkStringify from "remark-stringify";
1010

1111
export async function htmlToMarkdown(
1212
html: string,
13+
url: string,
1314
): Promise<string | undefined> {
14-
const content = parse(html).querySelector(".sl-markdown-content");
15+
const dom = parse(html);
16+
const content = dom.querySelector(".sl-markdown-content");
1517

1618
if (!content) {
1719
return;
@@ -26,5 +28,24 @@ export async function htmlToMarkdown(
2628
remarkStringify,
2729
]);
2830

29-
return markdown;
31+
const title = dom.querySelector("title")?.textContent;
32+
const description = dom.querySelector("meta[name='description']")?.attributes
33+
.content;
34+
const lastUpdated = dom.querySelector(".meta time")?.attributes.datetime;
35+
36+
const withFrontmatter = [
37+
"---",
38+
`title: ${title}`,
39+
description ? `description: ${description}` : [],
40+
lastUpdated ? `lastUpdated: ${lastUpdated}` : [],
41+
`source_url:`,
42+
` html: ${url}`,
43+
` md: ${url.replace("index.md", "")}`,
44+
"---\n",
45+
markdown,
46+
]
47+
.flat()
48+
.join("\n");
49+
50+
return withFrontmatter;
3051
}

worker/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ const redirectsEvaluator = generateRedirectsEvaluator(redirectsFileContents, {
1313
export default class extends WorkerEntrypoint<Env> {
1414
override async fetch(request: Request) {
1515
if (request.url.endsWith("/index.md")) {
16-
const res = await this.env.ASSETS.fetch(
17-
request.url.replace("index.md", ""),
18-
request,
19-
);
16+
const htmlUrl = request.url.replace("index.md", "");
17+
const res = await this.env.ASSETS.fetch(htmlUrl, request);
2018

2119
if (res.status === 404) {
2220
return res;
@@ -28,7 +26,7 @@ export default class extends WorkerEntrypoint<Env> {
2826
) {
2927
const html = await res.text();
3028

31-
const markdown = await htmlToMarkdown(html);
29+
const markdown = await htmlToMarkdown(html, request.url);
3230

3331
if (!markdown) {
3432
return new Response("Not Found", { status: 404 });

worker/index.worker.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,16 @@ describe("Cloudflare Docs", () => {
267267

268268
const text = await response.text();
269269
expect(text).toMatchInlineSnapshot(`
270-
"The HTML generated by this file is used as a test fixture for our Markdown generation.
270+
"---
271+
title: Markdown · Cloudflare Style Guide
272+
description: The HTML generated by this file is used as a test fixture for our Markdown generation.
273+
lastUpdated: 2025-01-01T00:00:00.000Z
274+
source_url:
275+
html: http://fakehost/style-guide/fixtures/markdown/index.md
276+
md: http://fakehost/style-guide/fixtures/markdown/
277+
---
278+
279+
The HTML generated by this file is used as a test fixture for our Markdown generation.
271280
272281
* mdx
273282

0 commit comments

Comments
 (0)