Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/publish-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ jobs:
name: Deploy to Cloudflare Workers
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
- name: Build vendored Markdown archive
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
run: |
npx tsx bin/generate-index-md.ts
cd distmd && zip -r markdown.zip .
npx wrangler r2 object put vendored-markdown/markdown.zip --file=markdown.zip --remote
- uses: actions/cache/save@v4
if: always()
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# build output
dist/
distmd/
# generated types
.astro/

Expand Down
32 changes: 32 additions & 0 deletions bin/generate-index-md.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { readFileSync, writeFileSync, mkdirSync } from "node:fs";

import glob from "fast-glob";
import { parse } from "node-html-parser";
import { htmlToMarkdown } from "~/util/markdown";

const files = await glob("dist/**/*.html");

for (const file of files) {
const html = readFileSync(file, "utf-8");
const dom = parse(html);

const url = dom
.querySelector("link[rel='alternate'][type='text/markdown']")
?.getAttribute("href");

if (!url) {
continue;
}

const markdown = await htmlToMarkdown(html, url);

if (!markdown) {
continue;
}

const path = file.replace("dist/", "distmd/").replace(".html", ".md");

mkdirSync(path.split("/").slice(0, -1).join("/"), { recursive: true });

writeFileSync(path, markdown);
}
2 changes: 1 addition & 1 deletion src/components/overrides/Head.astro
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ head.push({
attrs: {
rel: "alternate",
type: "text/markdown",
href: Astro.url.pathname + "index.md",
href: Astro.site + Astro.url.pathname.slice(1) + "index.md",
},
});

Expand Down
5 changes: 4 additions & 1 deletion src/pages/llms.txt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ export const GET: APIRoute = async () => {
Easily build and deploy full-stack applications everywhere,
thanks to integrated compute, storage, and networking.

> [!TIP]
> An archive of Markdown files is available at https://developers.cloudflare.com/markdown.zip

${grouped
.map(([product, entries]) => {
return dedent(`
## ${product}

${entries
?.map((e) => {
const line = `- [${e.data.title}](https://developers.cloudflare.com/${e.id}/)`;
const line = `- [${e.data.title}](https://developers.cloudflare.com/${e.id}/index.md)`;

const description = e.data.description;

Expand Down
4 changes: 2 additions & 2 deletions src/util/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export async function htmlToMarkdown(
description ? `description: ${description}` : [],
lastUpdated ? `lastUpdated: ${lastUpdated}` : [],
`source_url:`,
` html: ${url}`,
` md: ${url.replace("index.md", "")}`,
` html: ${url.replace("index.md", "")}`,
` md: ${url}`,
"---\n",
markdown,
]
Expand Down
10 changes: 10 additions & 0 deletions worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ const redirectsEvaluator = generateRedirectsEvaluator(redirectsFileContents, {

export default class extends WorkerEntrypoint<Env> {
override async fetch(request: Request) {
if (request.url.endsWith("/markdown.zip")) {
const res = await this.env.VENDORED_MARKDOWN.get("markdown.zip");

return new Response(res?.body, {
headers: {
"Content-Type": "application/zip",
},
});
}

if (request.url.endsWith("/index.md")) {
const htmlUrl = request.url.replace("index.md", "");
const res = await this.env.ASSETS.fetch(htmlUrl, request);
Expand Down
8 changes: 5 additions & 3 deletions worker/index.worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ describe("Cloudflare Docs", () => {
description: The HTML generated by this file is used as a test fixture for our Markdown generation.
lastUpdated: 2025-01-01T00:00:00.000Z
source_url:
html: http://fakehost/style-guide/fixtures/markdown/index.md
md: http://fakehost/style-guide/fixtures/markdown/
html: http://fakehost/style-guide/fixtures/markdown/
md: http://fakehost/style-guide/fixtures/markdown/index.md
---

The HTML generated by this file is used as a test fixture for our Markdown generation.
Expand Down Expand Up @@ -327,7 +327,9 @@ describe("Cloudflare Docs", () => {
"link[rel='alternate'][type='text/markdown']",
)?.attributes.href;

expect(markdown).toBe("/workers/index.md");
expect(markdown).toBe(
"https://developers.cloudflare.com/workers/index.md",
);
});

it("og:image tag", () => {
Expand Down
1 change: 1 addition & 0 deletions worker/worker-configuration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

interface Env {
ASSETS: Fetcher;
VENDORED_MARKDOWN: R2Bucket;
}
declare module "*/__redirects" {
const value: string;
Expand Down
6 changes: 5 additions & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ rules = [
directory = "./dist"
binding = "ASSETS"
not_found_handling = "404-page"
run_worker_first = true
run_worker_first = true

[[r2_buckets]]
binding = "VENDORED_MARKDOWN"
bucket_name = "vendored-markdown"
Loading