Skip to content

Commit 4958088

Browse files
committed
[Docs Site] Fix index.md encoding on /plans/
1 parent 8bff05f commit 4958088

File tree

4 files changed

+73
-42
lines changed

4 files changed

+73
-42
lines changed

src/components/ProductFeatures.astro

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ const entries = Object.entries(plan);
2929
<h3>{value.title}</h3>
3030
{value.link && (
3131
<p>
32-
<strong>Link: </strong>
33-
<a href={value.link}>{value.title}</a>
32+
<!-- prettier-ignore -->
33+
<strong>Link:</strong>{" "}<a href={value.link}>{value.title}</a>
3434
</p>
3535
)}
3636
{Object.values(value.properties).map((value: any) => (
37-
<p>
37+
<div>
3838
<strong
3939
set:html={marked.parseInline(
4040
value.title === "Availability"
@@ -46,27 +46,27 @@ const entries = Object.entries(plan);
4646
<p>{value.summary}</p>
4747
)}
4848
<ul>
49-
{value.free && (
49+
{value.free !== undefined && (
5050
<li>
51-
<strong>Free: </strong>
51+
<strong>Free:</strong>{" "}
5252
<Fragment
5353
set:html={marked.parseInline(value.free.toString())}
5454
/>
5555
</li>
5656
)}
5757
{additional_descriptions && (
5858
<>
59-
{value.lite ? (
59+
{value.lite !== undefined ? (
6060
<li>
61-
<strong>Lite: </strong>
61+
<strong>Lite:</strong>{" "}
6262
<Fragment
6363
set:html={marked.parseInline(value.lite.toString())}
6464
/>
6565
</li>
6666
) : (
67-
value.free && (
67+
value.free !== undefined && (
6868
<li>
69-
<strong>Lite: </strong>
69+
<strong>Lite:</strong>{" "}
7070
<Fragment
7171
set:html={marked.parseInline(value.free.toString())}
7272
/>
@@ -75,27 +75,27 @@ const entries = Object.entries(plan);
7575
)}
7676
</>
7777
)}
78-
{value.pro && (
78+
{value.pro !== undefined && (
7979
<li>
80-
<strong>Pro: </strong>
80+
<strong>Pro:</strong>{" "}
8181
<Fragment
8282
set:html={marked.parseInline(value.pro.toString())}
8383
/>
8484
</li>
8585
)}
8686
{additional_descriptions && (
8787
<>
88-
{value.pro_plus ? (
88+
{value.pro_plus !== undefined ? (
8989
<li>
90-
<strong>Pro Plus: </strong>
90+
<strong>Pro Plus:</strong>{" "}
9191
<Fragment
9292
set:html={marked.parseInline(value.pro_plus.toString())}
9393
/>
9494
</li>
9595
) : (
96-
value.pro && (
96+
value.pro !== undefined && (
9797
<li>
98-
<strong>Pro Plus: </strong>
98+
<strong>Pro Plus:</strong>{" "}
9999
<Fragment
100100
set:html={marked.parseInline(value.pro.toString())}
101101
/>
@@ -104,24 +104,24 @@ const entries = Object.entries(plan);
104104
)}
105105
</>
106106
)}
107-
{value.biz && (
107+
{value.biz !== undefined && (
108108
<li>
109-
<strong>Business: </strong>
109+
<strong>Business:</strong>{" "}
110110
<Fragment
111111
set:html={marked.parseInline(value.biz.toString())}
112112
/>
113113
</li>
114114
)}
115-
{value.ent && (
115+
{value.ent !== undefined && (
116116
<li>
117-
<strong>Enterprise: </strong>
117+
<strong>Enterprise:</strong>{" "}
118118
<Fragment
119119
set:html={marked.parseInline(value.ent.toString())}
120120
/>
121121
</li>
122122
)}
123123
</ul>
124-
</p>
124+
</div>
125125
))}
126126
</div>
127127
);

src/middleware/index.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
import { defineMiddleware } from "astro:middleware";
2+
import { htmlToMarkdown } from "~/util/markdown";
23

34
// `astro dev` only middleware so that `/api/...` links can be viewed.
45
export const onRequest = defineMiddleware(async (context, next) => {
56
if (import.meta.env.DEV) {
6-
if (context.url.pathname.startsWith("/api/")) {
7-
const url = new URL(context.url.pathname, import.meta.env.SITE);
7+
const { pathname } = context.url;
8+
9+
if (pathname.startsWith("/api/")) {
10+
const url = new URL(pathname, import.meta.env.SITE);
811

912
return fetch(url, {
1013
headers: {
1114
"accept-encoding": "identity",
1215
},
1316
});
17+
} else if (pathname.endsWith("/index.md")) {
18+
const htmlUrl = new URL(pathname.replace("index.md", ""), context.url);
19+
const html = await (await fetch(htmlUrl)).text();
20+
21+
const markdown = await htmlToMarkdown(html);
22+
23+
if (!markdown) {
24+
return new Response("Not Found", { status: 404 });
25+
}
26+
27+
return new Response(markdown, {
28+
headers: {
29+
"content-type": "text/markdown; charset=utf-8",
30+
},
31+
});
1432
}
1533
}
1634

src/util/markdown.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { parse } from "node-html-parser";
2+
import { process } from "../util/rehype";
3+
4+
import rehypeParse from "rehype-parse";
5+
import rehypeBaseUrl from "../plugins/rehype/base-url";
6+
import rehypeFilterElements from "../plugins/rehype/filter-elements";
7+
import remarkGfm from "remark-gfm";
8+
import rehypeRemark from "rehype-remark";
9+
import remarkStringify from "remark-stringify";
10+
11+
export async function htmlToMarkdown(
12+
html: string,
13+
): Promise<string | undefined> {
14+
const content = parse(html).querySelector(".sl-markdown-content");
15+
16+
if (!content) {
17+
return;
18+
}
19+
20+
const markdown = await process(content.toString(), [
21+
rehypeParse,
22+
rehypeBaseUrl,
23+
rehypeFilterElements,
24+
[remarkGfm, { tablePipeAlign: false }],
25+
rehypeRemark,
26+
remarkStringify,
27+
]);
28+
29+
return markdown;
30+
}

worker/index.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,7 @@ 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";
5+
import { htmlToMarkdown } from "../src/util/markdown";
146

157
const redirectsEvaluator = generateRedirectsEvaluator(redirectsFileContents, {
168
maxLineLength: 10_000, // Usually 2_000
@@ -36,21 +28,12 @@ export default class extends WorkerEntrypoint<Env> {
3628
) {
3729
const html = await res.text();
3830

39-
const content = parse(html).querySelector(".sl-markdown-content");
31+
const markdown = await htmlToMarkdown(html);
4032

41-
if (!content) {
33+
if (!markdown) {
4234
return new Response("Not Found", { status: 404 });
4335
}
4436

45-
const markdown = await process(content.toString(), [
46-
rehypeParse,
47-
rehypeBaseUrl,
48-
rehypeFilterElements,
49-
[remarkGfm, { tablePipeAlign: false }],
50-
rehypeRemark,
51-
remarkStringify,
52-
]);
53-
5437
return new Response(markdown, {
5538
headers: {
5639
"content-type": "text/markdown; charset=utf-8",

0 commit comments

Comments
 (0)