Skip to content

Commit

Permalink
Fix issues with links and images in substreams docs (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
benface authored May 14, 2023
1 parent d56c00b commit 4f598e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
11 changes: 6 additions & 5 deletions packages/nextra-theme/src/components/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type LinkProps = Pick<NextLinkProps, 'replace' | 'scroll' | 'shallow' | '
locale?: string
}

const externalHrefRegex = /^(https?:)?\/\//

export const Link = ({
href,
replace,
Expand All @@ -33,16 +35,15 @@ export const Link = ({
)
}

const isInternal = finalHref.startsWith('/')

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

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

return (
<NextLink
Expand Down
19 changes: 11 additions & 8 deletions website/pages/en/substreams/[[...slug]].mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RemoteContent } from 'nextra/data'
import { buildDynamicMDX } from 'nextra/remote'
import { listFiles } from './_meta.js'
import { getPageMap } from '@/src/getPageMap'
import path from 'node:path'

export async function getStaticPaths() {
const files = await listFiles()
Expand Down Expand Up @@ -37,8 +38,13 @@ export async function getStaticProps({ params: { slug = ['README'] } }) {
)
// remove gitbook {% ... %} elements
.replaceAll(/{%.*?%}/g, '')
// close img tags only if he doesn't point to .gitbook
.replaceAll(/<img(.*?)>/g, (...m) => (m[1].includes('src=".gitbook') ? '' : `<img${m[1]}/>`))
// close unclosed img tags
.replaceAll(/<img((?:(?!\/>)[^>])*?)>/g, (...m) => `<img${m[1]}/>`)
// fix gitbook image srcs
.replaceAll(/src="[^>"]*\.gitbook\/(.*)"/g, (...m) => {
console.log({ m })
return `src="${baseURL}.gitbook/${m[1]}"`
})
const mdx = await buildDynamicMDX(data, {
mdxOptions: {
// 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
Expand All @@ -47,15 +53,12 @@ export async function getStaticProps({ params: { slug = ['README'] } }) {
() => (tree, _file, done) => {
// enhance links
visit(tree, 'link', (node) => {
if (node.url.startsWith('./')) {
node.url = node.url.slice(2)
}
if (node.url.startsWith('/')) {
// (foo)[/foo/bar]
node.url = node.url.replace('/', '/substreams/')
} else if (!node.url.includes('/') && node.url.endsWith('.md')) {
// (foo)[foo.md]
node.url = [...slug.slice(0, -1), node.url].join('/')
} else {
// (foo)[foo.md] or (foo)[./foo.md] or (foo)[../foo.md]
node.url = path.join(path.dirname(fileURL), node.url)
}
})
done()
Expand Down

0 comments on commit 4f598e6

Please sign in to comment.