File tree Expand file tree Collapse file tree 3 files changed +37
-3
lines changed
pages/cloudflare-one/[...entry] Expand file tree Collapse file tree 3 files changed +37
-3
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import { entryToString } from "~/util/container";
66
77import { process } from "~/util/rehype" ;
88import rehypeParse from "rehype-parse" ;
9+ import rehypeBaseUrl from "~/plugins/rehype/base-url" ;
910import rehypeFilterElements from "~/plugins/rehype/filter-elements" ;
1011import remarkGfm from "remark-gfm" ;
1112import 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 ,
Original file line number Diff line number Diff line change 11import { visit } from "unist-util-visit" ;
22import type { Root } from "hast" ;
33
4+ const REWRITE_ELEMENTS = [ "a" , "img" ] ;
5+
46export 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 }
Original file line number Diff line number Diff 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
99102const 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
You can’t perform that action at this time.
0 commit comments