Skip to content

Commit fad8eb2

Browse files
committed
[Docs Site] Generate index.md for all index.html files
1 parent 44189f0 commit fad8eb2

File tree

9 files changed

+135
-83
lines changed

9 files changed

+135
-83
lines changed

public/__redirects

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

17391739
### DYNAMIC REDIRECTS ###
1740+
/*/index.html.md /:splat/index.md 301
17401741
/api-next/* /api/:splat 301
17411742
/changelog-next/* /changelog/:splat 301
17421743
/browser-rendering/quick-actions-rest-api/* /browser-rendering/rest-api/:splat 301
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);

worker/index.worker.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ describe("Cloudflare Docs", () => {
6363
expect(response.status).toBe(301);
6464
expect(response.headers.get("Location")).toBe("/changelog/rss.xml");
6565
});
66+
67+
it("redirects /workers/index.html.md to /workers/index.md", async () => {
68+
const request = new Request("http://fakehost/workers/index.html.md");
69+
const response = await SELF.fetch(request, { redirect: "manual" });
70+
71+
expect(response.status).toBe(301);
72+
expect(response.headers.get("Location")).toBe("/workers/index.md");
73+
});
6674
});
6775

6876
describe("json endpoints", () => {
@@ -247,4 +255,40 @@ describe("Cloudflare Docs", () => {
247255
expect(text).toContain('from "~/components"');
248256
});
249257
});
258+
259+
describe("index.md handling", () => {
260+
it("style-guide fixture", async () => {
261+
const request = new Request(
262+
"http://fakehost/style-guide/fixtures/markdown/index.md",
263+
);
264+
const response = await SELF.fetch(request);
265+
266+
expect(response.status).toBe(200);
267+
268+
const text = await response.text();
269+
expect(text).toMatchInlineSnapshot(`
270+
"The HTML generated by this file is used as a test fixture for our Markdown generation.
271+
272+
* mdx
273+
274+
\`\`\`mdx
275+
test
276+
\`\`\`
277+
278+
* md
279+
280+
\`\`\`md
281+
test
282+
\`\`\`
283+
"
284+
`);
285+
});
286+
287+
it("responds with 404.html at `/non-existent/index.md`", async () => {
288+
const request = new Request("http://fakehost/non-existent/index.md");
289+
const response = await SELF.fetch(request);
290+
expect(response.status).toBe(404);
291+
expect(await response.text()).toContain("Page not found.");
292+
});
293+
});
250294
});

wrangler.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ compatibility_flags = ["nodejs_compat"]
66
main = "./worker/index.ts"
77

88
workers_dev = true
9-
route = { pattern = "developers.cloudflare.com/*", zone_name = "developers.cloudflare.com"}
9+
route = { pattern = "developers.cloudflare.com/*", zone_name = "developers.cloudflare.com" }
1010

1111
rules = [
1212
{ type = "Text", globs = ["**/__redirects"], fallthrough = true },
@@ -16,4 +16,4 @@ rules = [
1616
directory = "./dist"
1717
binding = "ASSETS"
1818
not_found_handling = "404-page"
19-
run_worker_first = true
19+
run_worker_first = true

0 commit comments

Comments
 (0)