Skip to content

Commit ca77acb

Browse files
authored
[Docs Site] Build markdown.zip and upload to R2 (#22111)
1 parent a2c00de commit ca77acb

File tree

10 files changed

+68
-8
lines changed

10 files changed

+68
-8
lines changed

.github/workflows/publish-production.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ jobs:
3232
name: Deploy to Cloudflare Workers
3333
env:
3434
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
35+
- name: Build vendored Markdown archive
36+
env:
37+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
38+
run: |
39+
npx tsx bin/generate-index-md.ts
40+
cd distmd && zip -r markdown.zip .
41+
npx wrangler r2 object put vendored-markdown/markdown.zip --file=markdown.zip --remote
3542
- uses: actions/cache/save@v4
3643
if: always()
3744
with:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# build output
22
dist/
3+
distmd/
34
# generated types
45
.astro/
56

bin/generate-index-md.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { readFileSync, writeFileSync, mkdirSync } from "node:fs";
2+
3+
import glob from "fast-glob";
4+
import { parse } from "node-html-parser";
5+
import { htmlToMarkdown } from "~/util/markdown";
6+
7+
const files = await glob("dist/**/*.html");
8+
9+
for (const file of files) {
10+
const html = readFileSync(file, "utf-8");
11+
const dom = parse(html);
12+
13+
const url = dom
14+
.querySelector("link[rel='alternate'][type='text/markdown']")
15+
?.getAttribute("href");
16+
17+
if (!url) {
18+
continue;
19+
}
20+
21+
const markdown = await htmlToMarkdown(html, url);
22+
23+
if (!markdown) {
24+
continue;
25+
}
26+
27+
const path = file.replace("dist/", "distmd/").replace(".html", ".md");
28+
29+
mkdirSync(path.split("/").slice(0, -1).join("/"), { recursive: true });
30+
31+
writeFileSync(path, markdown);
32+
}

src/components/overrides/Head.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ head.push({
182182
attrs: {
183183
rel: "alternate",
184184
type: "text/markdown",
185-
href: Astro.url.pathname + "index.md",
185+
href: Astro.site + Astro.url.pathname.slice(1) + "index.md",
186186
},
187187
});
188188

src/pages/llms.txt.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@ export const GET: APIRoute = async () => {
3131
Easily build and deploy full-stack applications everywhere,
3232
thanks to integrated compute, storage, and networking.
3333
34+
> [!TIP]
35+
> An archive of Markdown files is available at https://developers.cloudflare.com/markdown.zip
36+
3437
${grouped
3538
.map(([product, entries]) => {
3639
return dedent(`
3740
## ${product}
3841
3942
${entries
4043
?.map((e) => {
41-
const line = `- [${e.data.title}](https://developers.cloudflare.com/${e.id}/)`;
44+
const line = `- [${e.data.title}](https://developers.cloudflare.com/${e.id}/index.md)`;
4245
4346
const description = e.data.description;
4447

src/util/markdown.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ export async function htmlToMarkdown(
3939
description ? `description: ${description}` : [],
4040
lastUpdated ? `lastUpdated: ${lastUpdated}` : [],
4141
`source_url:`,
42-
` html: ${url}`,
43-
` md: ${url.replace("index.md", "")}`,
42+
` html: ${url.replace("index.md", "")}`,
43+
` md: ${url}`,
4444
"---\n",
4545
markdown,
4646
]

worker/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ const redirectsEvaluator = generateRedirectsEvaluator(redirectsFileContents, {
1212

1313
export default class extends WorkerEntrypoint<Env> {
1414
override async fetch(request: Request) {
15+
if (request.url.endsWith("/markdown.zip")) {
16+
const res = await this.env.VENDORED_MARKDOWN.get("markdown.zip");
17+
18+
return new Response(res?.body, {
19+
headers: {
20+
"Content-Type": "application/zip",
21+
},
22+
});
23+
}
24+
1525
if (request.url.endsWith("/index.md")) {
1626
const htmlUrl = request.url.replace("index.md", "");
1727
const res = await this.env.ASSETS.fetch(htmlUrl, request);

worker/index.worker.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ describe("Cloudflare Docs", () => {
273273
description: The HTML generated by this file is used as a test fixture for our Markdown generation.
274274
lastUpdated: 2025-01-01T00:00:00.000Z
275275
source_url:
276-
html: http://fakehost/style-guide/fixtures/markdown/index.md
277-
md: http://fakehost/style-guide/fixtures/markdown/
276+
html: http://fakehost/style-guide/fixtures/markdown/
277+
md: http://fakehost/style-guide/fixtures/markdown/index.md
278278
---
279279
280280
The HTML generated by this file is used as a test fixture for our Markdown generation.
@@ -327,7 +327,9 @@ describe("Cloudflare Docs", () => {
327327
"link[rel='alternate'][type='text/markdown']",
328328
)?.attributes.href;
329329

330-
expect(markdown).toBe("/workers/index.md");
330+
expect(markdown).toBe(
331+
"https://developers.cloudflare.com/workers/index.md",
332+
);
331333
});
332334

333335
it("og:image tag", () => {

worker/worker-configuration.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
interface Env {
44
ASSETS: Fetcher;
5+
VENDORED_MARKDOWN: R2Bucket;
56
}
67
declare module "*/__redirects" {
78
const value: string;

wrangler.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ rules = [
1616
directory = "./dist"
1717
binding = "ASSETS"
1818
not_found_handling = "404-page"
19-
run_worker_first = true
19+
run_worker_first = true
20+
21+
[[r2_buckets]]
22+
binding = "VENDORED_MARKDOWN"
23+
bucket_name = "vendored-markdown"

0 commit comments

Comments
 (0)