Skip to content

Commit 8f38fd5

Browse files
committed
worker?
1 parent 098136f commit 8f38fd5

File tree

6 files changed

+189
-74
lines changed

6 files changed

+189
-74
lines changed

bin/generate-index-md.ts

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

src/pages/[area]/llms-full.txt.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type { APIRoute } from "astro";
2+
import { getCollection } from "astro:content";
3+
import { slug } from "github-slugger";
4+
5+
export async function getStaticPaths() {
6+
const products = await getCollection("products");
7+
8+
const areas = new Set(
9+
products.flatMap((p) => {
10+
if (!p.data.product.group) return [];
11+
12+
return slug(p.data.product.group.toLowerCase());
13+
}),
14+
);
15+
16+
return [...areas].map((area) => {
17+
return {
18+
params: {
19+
area,
20+
},
21+
};
22+
});
23+
}
24+
25+
export const GET: APIRoute = async ({ params }) => {
26+
const products = await getCollection("products", (p) => {
27+
if (!p.data.product.group) return false;
28+
29+
return slug(p.data.product.group.toLowerCase()) === params.area;
30+
});
31+
32+
const markdown = await getCollection("docs", (e) => {
33+
if (!e.body) return false;
34+
35+
if (
36+
e.id === "warp-client/legal/3rdparty" ||
37+
e.id === "magic-wan/legal/3rdparty"
38+
)
39+
return false;
40+
41+
return products.some((p) =>
42+
e.id.startsWith(p.data.product.url.slice(1, -1)),
43+
);
44+
})
45+
.then((entries) =>
46+
entries.map((entry) => {
47+
return [
48+
`# ${entry.data.title}`,
49+
`URL: https://developers.cloudflare.com/${entry.id}/`,
50+
`${entry.body?.trim()}`,
51+
"---",
52+
].join("\n\n");
53+
}),
54+
)
55+
.then((array) => array.join("\n\n"));
56+
57+
return new Response(markdown, {
58+
headers: {
59+
"content-type": "text/plain",
60+
},
61+
});
62+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type { APIRoute } from "astro";
2+
import { getCollection } from "astro:content";
3+
4+
export async function getStaticPaths() {
5+
const products = await getCollection("products", (p) => {
6+
return p.data.product.group?.toLowerCase() === "developer platform";
7+
});
8+
9+
return products.map((entry) => {
10+
return {
11+
params: {
12+
product: entry.id,
13+
},
14+
props: {
15+
product: entry,
16+
},
17+
};
18+
});
19+
}
20+
21+
export const GET: APIRoute = async ({ props }) => {
22+
const markdown = await getCollection("docs", (e) => {
23+
if (
24+
e.id === "warp-client/legal/3rdparty" ||
25+
e.id === "magic-wan/legal/3rdparty"
26+
)
27+
return false;
28+
29+
return (
30+
e.id.startsWith(props.product.data.product.url.slice(1, -1)) && e.body
31+
);
32+
})
33+
.then((entries) =>
34+
entries.map((entry) => {
35+
return [
36+
`# ${entry.data.title}`,
37+
`URL: https://developers.cloudflare.com/${entry.id}/`,
38+
`${entry.body?.trim()}`,
39+
"---",
40+
].join("\n\n");
41+
}),
42+
)
43+
.then((array) => array.join("\n\n"));
44+
45+
return new Response(markdown, {
46+
headers: {
47+
"content-type": "text/plain",
48+
},
49+
});
50+
};

src/pages/llms-full.txt.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { APIRoute } from "astro";
2+
import { getCollection } from "astro:content";
3+
4+
export const GET: APIRoute = async () => {
5+
const markdown = await getCollection("docs", (e) => {
6+
if (!e.body) return false;
7+
8+
if (
9+
e.id === "warp-client/legal/3rdparty" ||
10+
e.id === "magic-wan/legal/3rdparty"
11+
)
12+
return false;
13+
14+
return true;
15+
})
16+
.then((entries) =>
17+
entries.map((entry) => {
18+
return [
19+
`# ${entry.data.title}`,
20+
`URL: https://developers.cloudflare.com/${entry.id}/`,
21+
`${entry.body?.trim()}`,
22+
"---",
23+
].join("\n\n");
24+
}),
25+
)
26+
.then((array) => array.join("\n\n"));
27+
28+
return new Response(markdown, {
29+
headers: {
30+
"content-type": "text/plain",
31+
},
32+
});
33+
};

worker/index.ts

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,58 @@ 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

717
export default class extends WorkerEntrypoint<Env> {
8-
override async fetch(request: Request, env: Env) {
18+
override async fetch(request: Request) {
919
const url = new URL(request.url);
10-
if (request.url.endsWith("index.md")) {
11-
const path = url.pathname.slice(1) + "index.md";
12-
const file = await env.MARKDOWN.get(path);
20+
if (request.url.endsWith("/index.md")) {
21+
const res = await this.env.ASSETS.fetch(
22+
request.url.replace("index.md", ""),
23+
request,
24+
);
25+
26+
if (res.status === 404) {
27+
return res;
28+
}
29+
30+
if (
31+
res.status === 200 &&
32+
res.headers.get("content-type")?.startsWith("text/html")
33+
) {
34+
const html = await res.text();
1335

14-
if (file) {
15-
return new Response(file.body, {
36+
const content = parse(html).querySelector(".sl-markdown-content");
37+
38+
if (!content) {
39+
return new Response("Not Found", { status: 404 });
40+
}
41+
42+
const markdown = await process(content.toString(), [
43+
rehypeParse,
44+
rehypeBaseUrl,
45+
rehypeFilterElements,
46+
remarkGfm,
47+
rehypeRemark,
48+
remarkStringify,
49+
]);
50+
51+
return new Response(markdown, {
1652
headers: {
17-
"content-type": "text/markdown",
53+
"content-type": "text/markdown; charset=utf-8",
1854
},
1955
});
2056
}
21-
22-
return new Response("Not Found", { status: 404 });
2357
}
2458

2559
try {

wrangler.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,4 @@ rules = [{ type = "Text", globs = ["**/_redirects"], fallthrough = true }]
1414
directory = "./dist"
1515
binding = "ASSETS"
1616
not_found_handling = "404-page"
17-
run_worker_first = true
18-
19-
[[r2_buckets]]
20-
binding = "MARKDOWN"
21-
bucket_name = "devdocs-index-md"
17+
run_worker_first = true

0 commit comments

Comments
 (0)