Skip to content

Commit

Permalink
Fix links in remote content (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
benface authored Aug 2, 2023
1 parent 1437d84 commit 23749f5
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
10 changes: 6 additions & 4 deletions website/pages/en/querying/graph-client/[[...slug]].mdx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { RemoteContent } from 'nextra/data'
import { buildDynamicMDX } from 'nextra/remote'
import { getPageMap } from '@/src/getPageMap'
import { remarkReplaceLinks } from '@/src/remarkReplaceLinks'
import { Mermaid } from 'nextra/components'
import { visit } from 'unist-util-visit'
import { getPageMap } from '@/src/getPageMap'
import json from '@/remote-files/graph-client.json'

export const getStaticPaths = () => ({
Expand All @@ -20,9 +21,8 @@ export async function getStaticProps({ params }) {
const foundPath = filePaths.find((filePath) => filePath.startsWith(paths))
const baseURL = `https://raw.githubusercontent.com/${user}/${repo}/${branch}/${docsPath}${foundPath}`
const response = await fetch(baseURL)
const data = await response.text()
const mdx = await buildDynamicMDX(data, {
codeHighlight: false,
const content = await response.text()
const mdx = await buildDynamicMDX(content, {
mdxOptions: {
remarkPlugins: [
() => (tree, _file, done) => {
Expand All @@ -33,8 +33,10 @@ export async function getStaticProps({ params }) {
})
done()
},
[remarkReplaceLinks, { foundPath, basePath: '/querying/graph-client/' }],
],
},
codeHighlight: false,
})
return {
props: {
Expand Down
4 changes: 2 additions & 2 deletions website/pages/en/substreams/[[...slug]].mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export async function getStaticProps({ params }) {
const content = await response.text()
const mdx = await buildDynamicMDX(replaceGitBookContent({ content }), {
mdxOptions: {
// change-log contains `{variable}` text that is thrown an error by MDX2 parser since he treats
// it as variable injection, to fix it we parse chang-log with the Markdown parser
// change-log contains `{variable}` text that is thrown an error by MDX2 parser since it treats
// it as variable injection, to fix it we parse `change-log` with the Markdown parser
format: paths.endsWith('/change-log') ? 'md' : 'mdx',
remarkPlugins: [
[remarkReplaceLinks, { foundPath, basePath: '/substreams/' }],
Expand Down
2 changes: 1 addition & 1 deletion website/src/remarkReplaceImages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Plugin } from 'unified'
import { visit } from 'unist-util-visit'

export const remarkReplaceImages: Plugin<[{ assetsBasePath: string }], Root> = ({ assetsBasePath }) => {
if (!assetsBasePath) throw new Error('remarkReplaceLinks: assetsBasePath is required')
if (!assetsBasePath) throw new Error('remarkReplaceImages: assetsBasePath is required')

return (tree, _file, done) => {
visit(
Expand Down
5 changes: 3 additions & 2 deletions website/src/remarkReplaceLinks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path'

import { Root } from 'mdast'
import path from 'node:path'
import { Plugin } from 'unified'
import { visit } from 'unist-util-visit'

Expand All @@ -22,7 +23,7 @@ export const remarkReplaceLinks: Plugin<[{ foundPath: string; basePath: string }
node.url = node.url.replace('/', basePath)
} else {
// Relative to the current path, e.g. (foo)[bar] or (foo)[./bar] or (foo)[../bar]
node.url = path.join(path.dirname(foundPath), node.url)
node.url = path.join(basePath + path.dirname(foundPath), node.url)
}
})
done()
Expand Down

0 comments on commit 23749f5

Please sign in to comment.