Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with links and images in substreams docs #382

Merged
merged 2 commits into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]}/>`)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally didn't use AI for this. 😄

// fix gitbook image srcs
.replaceAll(/src="[^>"]*\.gitbook\/(.*)"/g, (...m) => {
console.log({ m })
return `src="${baseURL}.gitbook/${m[1]}"`
})
// fixes http://localhost:3000/en/substreams/reference-and-specs/authentication/
.replaceAll('<pre class="language-bash" data-overflow="wrap"><code class="lang-bash">', '```bash\n')
// fixes http://localhost:3000/en/substreams/developers-guide/modules/inputs/
Expand All @@ -52,15 +58,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