Skip to content

Commit 936de7e

Browse files
authored
[Docs Site] Generate index.md for all index.html files (#20988)
* [Docs Site] Generate index.md for all index.html files * remove redundant head config * charset
1 parent a9edcbf commit 936de7e

File tree

12 files changed

+151
-92
lines changed

12 files changed

+151
-92
lines changed

astro.config.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,6 @@ export default defineConfig({
9090
src: "./src/assets/logo.svg",
9191
},
9292
favicon: "/favicon.png",
93-
head: ["image", "og:image", "twitter:image"].map((name) => {
94-
return {
95-
tag: "meta",
96-
attrs: {
97-
name,
98-
content: "https://developers.cloudflare.com/cf-twitter-card.png",
99-
},
100-
};
101-
}),
10293
social: {
10394
github: "https://github.com/cloudflare/cloudflare-docs",
10495
"x.com": "https://x.com/cloudflare",

public/__redirects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,7 @@
17521752
/cloudflare-one/tutorials/zsh-env-var/ /cloudflare-one/tutorials/cli/ 301
17531753

17541754
### DYNAMIC REDIRECTS ###
1755+
/*/index.html.md /:splat/index.md 301
17551756
/api-next/* /api/:splat 301
17561757
/changelog-next/* /changelog/:splat 301
17571758
/browser-rendering/quick-actions-rest-api/* /browser-rendering/rest-api/:splat 301

public/_headers

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@
33

44
/_astro/*
55
Cache-Control: public, max-age=604800, immutable
6+
7+
/*/llms-full.txt:
8+
Content-Type: text/markdown; charset=utf-8
9+
10+
/*/index.md:
11+
Content-Type: text/markdown; charset=utf-8

src/components/overrides/Head.astro

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ const ogImageUrl = new URL(ogImagePath, Astro.url.origin).toString();
150150
});
151151
});
152152
153+
head.push({
154+
tag: "link",
155+
attrs: {
156+
rel: "alternate",
157+
type: "text/markdown",
158+
href: Astro.url.pathname + "index.md",
159+
},
160+
content: "",
161+
});
162+
153163
metaTags.map((attrs) => {
154164
head.push({
155165
tag: "meta",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: Fixtures
3+
noindex: true
4+
sidebar:
5+
hidden: true
6+
---
7+
8+
This folder stores test fixtures to be used in CI.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: Markdown
3+
noindex: true
4+
sidebar:
5+
hidden: true
6+
---
7+
8+
import { Tabs, TabItem } from "~/components";
9+
10+
The HTML generated by this file is used as a test fixture for our Markdown generation.
11+
12+
<Tabs>
13+
<TabItem label="mdx">
14+
```mdx
15+
test
16+
```
17+
</TabItem>
18+
<TabItem label="md">
19+
```md
20+
test
21+
```
22+
</TabItem>
23+
</Tabs>

src/pages/[...entry]/index.html.md.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/pages/cloudflare-one/[...entry]/index.md.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

vitest.workspace.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ const workspace = defineWorkspace([
88
test: {
99
name: "Workers",
1010
include: ["**/*.worker.test.ts"],
11+
deps: {
12+
optimizer: {
13+
ssr: {
14+
enabled: true,
15+
include: ["node-html-parser"],
16+
},
17+
},
18+
},
1119
poolOptions: {
1220
workers: {
1321
wrangler: { configPath: "./wrangler.toml" },

worker/index.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ import { WorkerEntrypoint } from "cloudflare:workers";
22
import { generateRedirectsEvaluator } from "redirects-in-workers";
33
import redirectsFileContents from "../dist/__redirects";
44

5+
import { parse } from "node-html-parser";
6+
import { process } from "../src/util/rehype";
7+
8+
import rehypeParse from "rehype-parse";
9+
import rehypeBaseUrl from "../src/plugins/rehype/base-url";
10+
import rehypeFilterElements from "../src/plugins/rehype/filter-elements";
11+
import remarkGfm from "remark-gfm";
12+
import rehypeRemark from "rehype-remark";
13+
import remarkStringify from "remark-stringify";
14+
515
const redirectsEvaluator = generateRedirectsEvaluator(redirectsFileContents, {
616
maxLineLength: 10_000, // Usually 2_000
717
maxStaticRules: 10_000, // Usually 2_000
@@ -10,6 +20,45 @@ const redirectsEvaluator = generateRedirectsEvaluator(redirectsFileContents, {
1020

1121
export default class extends WorkerEntrypoint<Env> {
1222
override async fetch(request: Request) {
23+
if (request.url.endsWith("/index.md")) {
24+
const res = await this.env.ASSETS.fetch(
25+
request.url.replace("index.md", ""),
26+
request,
27+
);
28+
29+
if (res.status === 404) {
30+
return res;
31+
}
32+
33+
if (
34+
res.status === 200 &&
35+
res.headers.get("content-type")?.startsWith("text/html")
36+
) {
37+
const html = await res.text();
38+
39+
const content = parse(html).querySelector(".sl-markdown-content");
40+
41+
if (!content) {
42+
return new Response("Not Found", { status: 404 });
43+
}
44+
45+
const markdown = await process(content.toString(), [
46+
rehypeParse,
47+
rehypeBaseUrl,
48+
rehypeFilterElements,
49+
remarkGfm,
50+
rehypeRemark,
51+
remarkStringify,
52+
]);
53+
54+
return new Response(markdown, {
55+
headers: {
56+
"content-type": "text/markdown; charset=utf-8",
57+
},
58+
});
59+
}
60+
}
61+
1362
try {
1463
try {
1564
const redirect = await redirectsEvaluator(request, this.env.ASSETS);

0 commit comments

Comments
 (0)