Skip to content

Commit 43fe0d0

Browse files
committed
rebase + update test
1 parent ef00c5c commit 43fe0d0

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

src/middleware/index.ts

+1-1
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

+23-2
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default class extends WorkerEntrypoint<Env> {
2626
) {
2727
const html = await res.text();
2828

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

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

worker/index.worker.test.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,15 @@ 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+
source_url:
274+
html: http://fakehost/style-guide/fixtures/markdown/index.md
275+
md: http://fakehost/style-guide/fixtures/markdown/
276+
---
277+
278+
The HTML generated by this file is used as a test fixture for our Markdown generation.
271279
272280
* mdx
273281

0 commit comments

Comments
 (0)