Skip to content

Commit 59e0a4a

Browse files
committed
allow img, add base url to a and href, fix mermaid
1 parent 18ed4df commit 59e0a4a

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/pages/cloudflare-one/[...entry]/index.md.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { entryToString } from "~/util/container";
66

77
import { process } from "~/util/rehype";
88
import rehypeParse from "rehype-parse";
9+
import rehypeBaseUrl from "~/plugins/rehype/base-url";
910
import rehypeFilterElements from "~/plugins/rehype/filter-elements";
1011
import remarkGfm from "remark-gfm";
1112
import rehypeRemark from "rehype-remark";
@@ -35,6 +36,7 @@ export const GET: APIRoute<Props> = async (context) => {
3536

3637
const md = await process(html, [
3738
rehypeParse,
39+
rehypeBaseUrl,
3840
rehypeFilterElements,
3941
remarkGfm,
4042
rehypeRemark,

src/plugins/rehype/base-url.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import { visit } from "unist-util-visit";
22
import type { Root } from "hast";
33

4+
const REWRITE_ELEMENTS = ["a", "img"];
5+
46
export default function () {
57
return function (tree: Root) {
68
visit(tree, "element", function (element) {
7-
if (element.tagName === "a") {
8-
const href = element.properties.href as string | undefined;
9+
if (REWRITE_ELEMENTS.includes(element.tagName)) {
10+
const property = element.tagName === "a" ? "href" : "src";
11+
const href = element.properties[property] as string | undefined;
912

1013
if (href) {
1114
if (href.startsWith("/")) {
1215
const url = new URL(href, "https://developers.cloudflare.com/");
1316

14-
element.properties.href = url.href;
17+
element.properties[property] = url.href;
1518
}
1619
}
1720
}

src/plugins/rehype/filter-elements.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,19 @@ const ALLOWED_ELEMENTS = [
9191
"th",
9292
"thead",
9393
"tr",
94+
// Images
95+
"img",
9496
// Custom elements
9597
"rule-id",
9698
"starlight-tabs",
99+
"starlight-image-zoom-zoomable",
97100
];
98101

99102
const ALLOWED_ATTRIBUTES: Record<string, string[]> = {
100103
a: ["href", "id", "target"],
101104
pre: ["dataLanguage"],
102105
code: ["className"],
106+
img: ["src", "alt"],
103107
"rule-id": ["id"],
104108
};
105109

@@ -133,6 +137,31 @@ export default function () {
133137
}
134138

135139
if (tag === "pre") {
140+
if (classNames.includes("mermaid")) {
141+
const definition = element.children.find(
142+
(child) => child.type === "text",
143+
);
144+
if (!definition) return;
145+
146+
element.children = [
147+
{
148+
type: "element",
149+
tagName: "code",
150+
properties: {
151+
className: ["language-mermaid"],
152+
},
153+
children: [
154+
{
155+
type: "text",
156+
value: definition.value,
157+
},
158+
],
159+
},
160+
];
161+
162+
return;
163+
}
164+
136165
const language = element.properties.dataLanguage;
137166
if (!language) return;
138167

0 commit comments

Comments
 (0)