Skip to content

Commit 4f598e6

Browse files
authored
Fix issues with links and images in substreams docs (#382)
1 parent d56c00b commit 4f598e6

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

packages/nextra-theme/src/components/Link.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export type LinkProps = Pick<NextLinkProps, 'replace' | 'scroll' | 'shallow' | '
99
locale?: string
1010
}
1111

12+
const externalHrefRegex = /^(https?:)?\/\//
13+
1214
export const Link = ({
1315
href,
1416
replace,
@@ -33,16 +35,15 @@ export const Link = ({
3335
)
3436
}
3537

36-
const isInternal = finalHref.startsWith('/')
37-
38-
// If the link is internal, automatically prepend the locale to it
39-
if (isInternal) {
38+
// If the link is root-relative, automatically prepend the locale to it
39+
if (finalHref.startsWith('/')) {
4040
const { locale: pathLocale, pathWithoutLocale } = extractLocaleFromPath(finalHref)
4141
finalHref = `/${linkLocale ?? pathLocale ?? currentLocale}${pathWithoutLocale}`
4242
}
4343

4444
// If the link is external, default the target to `_blank`
45-
const finalTarget = target ?? (!isInternal ? '_blank' : undefined)
45+
const isExternal = externalHrefRegex.test(finalHref)
46+
const finalTarget = target ?? (isExternal ? '_blank' : undefined)
4647

4748
return (
4849
<NextLink

website/pages/en/substreams/[[...slug]].mdx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { RemoteContent } from 'nextra/data'
33
import { buildDynamicMDX } from 'nextra/remote'
44
import { listFiles } from './_meta.js'
55
import { getPageMap } from '@/src/getPageMap'
6+
import path from 'node:path'
67

78
export async function getStaticPaths() {
89
const files = await listFiles()
@@ -37,8 +38,13 @@ export async function getStaticProps({ params: { slug = ['README'] } }) {
3738
)
3839
// remove gitbook {% ... %} elements
3940
.replaceAll(/{%.*?%}/g, '')
40-
// close img tags only if he doesn't point to .gitbook
41-
.replaceAll(/<img(.*?)>/g, (...m) => (m[1].includes('src=".gitbook') ? '' : `<img${m[1]}/>`))
41+
// close unclosed img tags
42+
.replaceAll(/<img((?:(?!\/>)[^>])*?)>/g, (...m) => `<img${m[1]}/>`)
43+
// fix gitbook image srcs
44+
.replaceAll(/src="[^>"]*\.gitbook\/(.*)"/g, (...m) => {
45+
console.log({ m })
46+
return `src="${baseURL}.gitbook/${m[1]}"`
47+
})
4248
const mdx = await buildDynamicMDX(data, {
4349
mdxOptions: {
4450
// change-log contains `{variable}` text that is thrown an error by MDX2 parser since he treat it as variable injection, to fix it we parse chang-log with the markdown parser
@@ -47,15 +53,12 @@ export async function getStaticProps({ params: { slug = ['README'] } }) {
4753
() => (tree, _file, done) => {
4854
// enhance links
4955
visit(tree, 'link', (node) => {
50-
if (node.url.startsWith('./')) {
51-
node.url = node.url.slice(2)
52-
}
5356
if (node.url.startsWith('/')) {
5457
// (foo)[/foo/bar]
5558
node.url = node.url.replace('/', '/substreams/')
56-
} else if (!node.url.includes('/') && node.url.endsWith('.md')) {
57-
// (foo)[foo.md]
58-
node.url = [...slug.slice(0, -1), node.url].join('/')
59+
} else {
60+
// (foo)[foo.md] or (foo)[./foo.md] or (foo)[../foo.md]
61+
node.url = path.join(path.dirname(fileURL), node.url)
5962
}
6063
})
6164
done()

0 commit comments

Comments
 (0)