Skip to content

Commit f96922b

Browse files
authored
docs: fix wrappers assets (images) (supabase#36365)
* Load wrappers assets with external url * Refactor assets url transformer as rehype plugin
1 parent 7c3f795 commit f96922b

File tree

1 file changed

+29
-2
lines changed
  • apps/docs/app/guides/database/extensions/wrappers/[[...slug]]

1 file changed

+29
-2
lines changed

apps/docs/app/guides/database/extensions/wrappers/[[...slug]]/page.tsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,20 @@ interface Params {
211211

212212
const WrappersDocs = async (props: { params: Promise<Params> }) => {
213213
const params = await props.params
214-
const { isExternal, meta, ...data } = await getContent(params)
214+
const { isExternal, meta, assetsBaseUrl, ...data } = await getContent(params)
215+
216+
// Create a combined URL transformer that handles both regular URLs and asset URLs
217+
const combinedUrlTransformer: UrlTransformFunction = (url, node) => {
218+
// First try assets URL transformation (starts with ../assets/)
219+
const transformedUrl = assetUrlTransform(url, assetsBaseUrl)
220+
221+
// If URL wasn't changed proceed with regular URL transformation
222+
if (transformedUrl === url) {
223+
return urlTransform(url, node)
224+
}
225+
226+
return transformedUrl
227+
}
215228

216229
const options = isExternal
217230
? ({
@@ -222,7 +235,7 @@ const WrappersDocs = async (props: { params: Promise<Params> }) => {
222235
remarkPyMdownTabs,
223236
[removeTitle, meta.title],
224237
],
225-
rehypePlugins: [[linkTransform, urlTransform], rehypeSlug],
238+
rehypePlugins: [[linkTransform, combinedUrlTransformer], rehypeSlug],
226239
},
227240
} as SerializeOptions)
228241
: undefined
@@ -242,6 +255,7 @@ const getContent = async (params: Params) => {
242255
let meta: any
243256
let content: string
244257
let editLink: string
258+
let assetsBaseUrl: string = ''
245259

246260
if (!federatedPage) {
247261
isExternal = false
@@ -278,6 +292,8 @@ const getContent = async (params: Params) => {
278292
})
279293
const rawContent = await response.text()
280294

295+
assetsBaseUrl = `https://raw.githubusercontent.com/${org}/${repo}/${tag}/docs/assets/`
296+
281297
const { content: contentWithoutFrontmatter } = matter(rawContent)
282298
content = removeRedundantH1(contentWithoutFrontmatter)
283299
}
@@ -289,9 +305,20 @@ const getContent = async (params: Params) => {
289305
editLink: newEditLink(editLink),
290306
meta,
291307
content,
308+
assetsBaseUrl,
292309
}
293310
}
294311

312+
const assetUrlTransform = (url: string, baseUrl: string): string => {
313+
const assetPattern = /(\.\.\/)+assets\//
314+
315+
if (assetPattern.test(url)) {
316+
return url.replace(assetPattern, baseUrl)
317+
}
318+
319+
return url
320+
}
321+
295322
const urlTransform: UrlTransformFunction = (url) => {
296323
try {
297324
const externalSiteUrl = new URL(externalSite)

0 commit comments

Comments
 (0)