Skip to content

Commit 6604c8c

Browse files
committed
[Docs Site] Add frontmatter to index.md output
1 parent 41ab175 commit 6604c8c

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

worker/index.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ const redirectsEvaluator = generateRedirectsEvaluator(redirectsFileContents, {
2121
export default class extends WorkerEntrypoint<Env> {
2222
override async fetch(request: Request) {
2323
if (request.url.endsWith("/index.md")) {
24-
const res = await this.env.ASSETS.fetch(
25-
request.url.replace("index.md", ""),
26-
request,
27-
);
24+
const htmlUrl = request.url.replace("index.md", "");
25+
const res = await this.env.ASSETS.fetch(htmlUrl, request);
2826

2927
if (res.status === 404) {
3028
return res;
@@ -35,8 +33,9 @@ export default class extends WorkerEntrypoint<Env> {
3533
res.headers.get("content-type")?.startsWith("text/html")
3634
) {
3735
const html = await res.text();
36+
const dom = parse(html);
3837

39-
const content = parse(html).querySelector(".sl-markdown-content");
38+
const content = dom.querySelector(".sl-markdown-content");
4039

4140
if (!content) {
4241
return new Response("Not Found", { status: 404 });
@@ -51,7 +50,27 @@ export default class extends WorkerEntrypoint<Env> {
5150
remarkStringify,
5251
]);
5352

54-
return new Response(markdown, {
53+
const title = dom.querySelector("title")?.textContent;
54+
const description = dom.querySelector("meta[name='description']")
55+
?.attributes.content;
56+
const lastUpdated =
57+
dom.querySelector(".meta time")?.attributes.datetime;
58+
59+
const withFrontmatter = [
60+
"---",
61+
`title: ${title}`,
62+
description ? `description: ${description}` : [],
63+
lastUpdated ? `lastUpdated: ${lastUpdated}` : [],
64+
`source_url:`,
65+
` html: ${htmlUrl}`,
66+
` md: ${request.url}`,
67+
"---\n",
68+
markdown,
69+
]
70+
.flat()
71+
.join("\n");
72+
73+
return new Response(withFrontmatter, {
5574
headers: {
5675
"content-type": "text/markdown; charset=utf-8",
5776
},

0 commit comments

Comments
 (0)