Skip to content

Commit 38ab966

Browse files
author
Dimitri POSTOLOV
authored
fix some broken images in remote mdx, use ast replacing instead regex (#434)
1 parent 99ac2b0 commit 38ab966

File tree

6 files changed

+85
-72
lines changed

6 files changed

+85
-72
lines changed

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { RemoteContent } from 'nextra/data'
22
import { buildDynamicMDX } from 'nextra/remote'
33
import { getPageMap } from '@/src/getPageMap'
44
import { remarkReplaceLinks } from '@/src/remarkReplaceLinks'
5+
import { remarkReplaceImages } from '@/src/remarkReplaceImages'
56
import { replaceGitBookContent } from '@/src/replaceGitBookContent'
67
import json from '@/remote-files/firehose.json'
78

@@ -21,21 +22,15 @@ export async function getStaticProps({ params }) {
2122
const baseURL = `https://raw.githubusercontent.com/${user}/${repo}/${branch}/${docsPath}${foundPath}`
2223
const response = await fetch(baseURL)
2324
const content = await response.text()
24-
const mdx = await buildDynamicMDX(
25-
replaceGitBookContent({
26-
content,
27-
user,
28-
repo,
29-
branch,
30-
docsPath,
31-
}),
32-
{
33-
mdxOptions: {
34-
remarkPlugins: [[remarkReplaceLinks, { foundPath, basePath: '/firehose/' }]],
35-
},
36-
codeHighlight: false,
37-
}
38-
)
25+
const mdx = await buildDynamicMDX(replaceGitBookContent({ content }), {
26+
mdxOptions: {
27+
remarkPlugins: [
28+
[remarkReplaceLinks, { foundPath, basePath: '/firehose/' }],
29+
[remarkReplaceImages, { assetsBasePath: `${user}/${repo}/${branch}/${docsPath}` }],
30+
],
31+
},
32+
codeHighlight: false,
33+
})
3934
const parsedData = JSON.parse(mdx.__nextra_dynamic_opts)
4035
// remove title from frontMatter if it's the same as the title of the page
4136
if (parsedData.title === parsedData.frontMatter.title) {

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { RemoteContent } from 'nextra/data'
22
import { buildDynamicMDX } from 'nextra/remote'
33
import { getPageMap } from '@/src/getPageMap'
44
import { remarkReplaceLinks } from '@/src/remarkReplaceLinks'
5+
import { remarkReplaceImages } from '@/src/remarkReplaceImages'
56
import { replaceGitBookContent } from '@/src/replaceGitBookContent'
67
import json from '@/remote-files/substreams.json'
78

@@ -21,24 +22,18 @@ export async function getStaticProps({ params }) {
2122
const baseURL = `https://raw.githubusercontent.com/${user}/${repo}/${branch}/${docsPath}${foundPath}`
2223
const response = await fetch(baseURL)
2324
const content = await response.text()
24-
const mdx = await buildDynamicMDX(
25-
replaceGitBookContent({
26-
content,
27-
user,
28-
repo,
29-
branch,
30-
docsPath,
31-
}),
32-
{
33-
mdxOptions: {
34-
// change-log contains `{variable}` text that is thrown an error by MDX2 parser since he treats
35-
// it as variable injection, to fix it we parse chang-log with the Markdown parser
36-
format: paths.endsWith('/change-log') ? 'md' : 'mdx',
37-
remarkPlugins: [[remarkReplaceLinks, { foundPath, basePath: '/substreams/' }]],
38-
},
39-
codeHighlight: false,
40-
}
41-
)
25+
const mdx = await buildDynamicMDX(replaceGitBookContent({ content }), {
26+
mdxOptions: {
27+
// change-log contains `{variable}` text that is thrown an error by MDX2 parser since he treats
28+
// it as variable injection, to fix it we parse chang-log with the Markdown parser
29+
format: paths.endsWith('/change-log') ? 'md' : 'mdx',
30+
remarkPlugins: [
31+
[remarkReplaceLinks, { foundPath, basePath: '/substreams/' }],
32+
[remarkReplaceImages, { assetsBasePath: `${user}/${repo}/${branch}/${docsPath}` }],
33+
],
34+
},
35+
codeHighlight: false,
36+
})
4237
return {
4338
props: {
4439
...mdx,

website/remote-files/firehose.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
"firehose-setup/ethereum/synchronization.md",
2626
"firehose-setup/near/installation.md",
2727
"firehose-setup/system-requirements.md",
28+
"integrate-new-chains/benefits.md",
2829
"integrate-new-chains/design-principles.md",
2930
"integrate-new-chains/firehose-starter.md",
30-
"integrate-new-chains/new-blockchains.md",
31-
"integrate-new-chains/why-integrate-the-firehose.md",
31+
"integrate-new-chains/integration-overview.md",
3232
"intro/firehose-overview.md",
3333
"intro/prerequisites.md",
3434
"references/faq.md",

website/route-lockfile.txt

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/ar/
33
/ar/404/
44
/ar/about/
5-
/ar/arbitrum-faq/
5+
/ar/arbitrum/arbitrum-faq/
66
/ar/billing/
77
/ar/cookbook/arweave/
88
/ar/cookbook/base-testnet/
@@ -54,7 +54,7 @@
5454
/ar/substreams/
5555
/ar/tokenomics/
5656
/de/about/
57-
/de/arbitrum-faq/
57+
/de/arbitrum/arbitrum-faq/
5858
/de/billing/
5959
/de/cookbook/arweave/
6060
/de/cookbook/base-testnet/
@@ -105,7 +105,10 @@
105105
/en/
106106
/en/404/
107107
/en/about/
108-
/en/arbitrum-faq/
108+
/en/arbitrum-faq/ -> /en/arbitrum/arbitrum-faq/
109+
/en/arbitrum/arbitrum-faq/
110+
/en/arbitrum/l2-transfer-tools-faq/
111+
/en/arbitrum/l2-transfer-tools-guide/
109112
/en/billing/
110113
/en/cookbook/arweave/
111114
/en/cookbook/base-testnet/
@@ -149,10 +152,10 @@
149152
/en/firehose/firehose-setup/ethereum/synchronization/
150153
/en/firehose/firehose-setup/near/installation/
151154
/en/firehose/firehose-setup/system-requirements/
155+
/en/firehose/integrate-new-chains/benefits/
152156
/en/firehose/integrate-new-chains/design-principles/
153157
/en/firehose/integrate-new-chains/firehose-starter/
154-
/en/firehose/integrate-new-chains/new-blockchains/
155-
/en/firehose/integrate-new-chains/why-integrate-the-firehose/
158+
/en/firehose/integrate-new-chains/integration-overview/
156159
/en/firehose/intro/firehose-overview/
157160
/en/firehose/intro/prerequisites/
158161
/en/firehose/references/faq/
@@ -204,6 +207,7 @@
204207
/en/substreams/developers-guide/parallel-execution/
205208
/en/substreams/developers-guide/running-substreams/
206209
/en/substreams/developers-guide/sink-targets/README/
210+
/en/substreams/developers-guide/sink-targets/substreams-sink-clickhouse/
207211
/en/substreams/developers-guide/sink-targets/substreams-sink-files/
208212
/en/substreams/developers-guide/sink-targets/substreams-sink-kv/
209213
/en/substreams/developers-guide/sink-targets/substreams-sink-mongodb/
@@ -224,7 +228,7 @@
224228
/es/
225229
/es/404/
226230
/es/about/
227-
/es/arbitrum-faq/
231+
/es/arbitrum/arbitrum-faq/
228232
/es/billing/
229233
/es/cookbook/arweave/
230234
/es/cookbook/base-testnet/
@@ -276,7 +280,7 @@
276280
/es/substreams/
277281
/es/tokenomics/
278282
/fr/about/
279-
/fr/arbitrum-faq/
283+
/fr/arbitrum/arbitrum-faq/
280284
/fr/billing/
281285
/fr/cookbook/arweave/
282286
/fr/cookbook/base-testnet/
@@ -327,7 +331,7 @@
327331
/hi/
328332
/hi/404/
329333
/hi/about/
330-
/hi/arbitrum-faq/
334+
/hi/arbitrum/arbitrum-faq/
331335
/hi/billing/
332336
/hi/cookbook/arweave/
333337
/hi/cookbook/base-testnet/
@@ -376,7 +380,7 @@
376380
/hi/substreams/
377381
/hi/tokenomics/
378382
/it/about/
379-
/it/arbitrum-faq/
383+
/it/arbitrum/arbitrum-faq/
380384
/it/billing/
381385
/it/cookbook/arweave/
382386
/it/cookbook/base-testnet/
@@ -427,7 +431,7 @@
427431
/ja/
428432
/ja/404/
429433
/ja/about/
430-
/ja/arbitrum-faq/
434+
/ja/arbitrum/arbitrum-faq/
431435
/ja/billing/
432436
/ja/cookbook/arweave/
433437
/ja/cookbook/base-testnet/
@@ -481,7 +485,7 @@
481485
/ko/
482486
/ko/404/
483487
/ko/about/
484-
/ko/arbitrum-faq/
488+
/ko/arbitrum/arbitrum-faq/
485489
/ko/billing/
486490
/ko/cookbook/arweave/
487491
/ko/cookbook/base-testnet/
@@ -533,7 +537,7 @@
533537
/ko/substreams/
534538
/ko/tokenomics/
535539
/mr/about/
536-
/mr/arbitrum-faq/
540+
/mr/arbitrum/arbitrum-faq/
537541
/mr/billing/
538542
/mr/cookbook/arweave/
539543
/mr/cookbook/base-testnet/
@@ -582,7 +586,7 @@
582586
/mr/substreams/
583587
/mr/tokenomics/
584588
/nl/about/
585-
/nl/arbitrum-faq/
589+
/nl/arbitrum/arbitrum-faq/
586590
/nl/billing/
587591
/nl/cookbook/arweave/
588592
/nl/cookbook/base-testnet/
@@ -631,7 +635,7 @@
631635
/nl/substreams/
632636
/nl/tokenomics/
633637
/pl/about/
634-
/pl/arbitrum-faq/
638+
/pl/arbitrum/arbitrum-faq/
635639
/pl/billing/
636640
/pl/cookbook/arweave/
637641
/pl/cookbook/base-testnet/
@@ -680,7 +684,7 @@
680684
/pl/substreams/
681685
/pl/tokenomics/
682686
/pt/about/
683-
/pt/arbitrum-faq/
687+
/pt/arbitrum/arbitrum-faq/
684688
/pt/billing/
685689
/pt/cookbook/arweave/
686690
/pt/cookbook/base-testnet/
@@ -729,7 +733,7 @@
729733
/pt/substreams/
730734
/pt/tokenomics/
731735
/ru/about/
732-
/ru/arbitrum-faq/
736+
/ru/arbitrum/arbitrum-faq/
733737
/ru/billing/
734738
/ru/cookbook/arweave/
735739
/ru/cookbook/base-testnet/
@@ -778,7 +782,7 @@
778782
/ru/substreams/
779783
/ru/tokenomics/
780784
/sv/about/
781-
/sv/arbitrum-faq/
785+
/sv/arbitrum/arbitrum-faq/
782786
/sv/billing/
783787
/sv/cookbook/arweave/
784788
/sv/cookbook/base-testnet/
@@ -827,7 +831,7 @@
827831
/sv/substreams/
828832
/sv/tokenomics/
829833
/tr/about/
830-
/tr/arbitrum-faq/
834+
/tr/arbitrum/arbitrum-faq/
831835
/tr/billing/
832836
/tr/cookbook/arweave/
833837
/tr/cookbook/base-testnet/
@@ -876,7 +880,7 @@
876880
/tr/substreams/
877881
/tr/tokenomics/
878882
/uk/about/
879-
/uk/arbitrum-faq/
883+
/uk/arbitrum/arbitrum-faq/
880884
/uk/billing/
881885
/uk/cookbook/arweave/
882886
/uk/cookbook/base-testnet/
@@ -927,7 +931,7 @@
927931
/ur/
928932
/ur/404/
929933
/ur/about/
930-
/ur/arbitrum-faq/
934+
/ur/arbitrum/arbitrum-faq/
931935
/ur/billing/
932936
/ur/cookbook/arweave/
933937
/ur/cookbook/base-testnet/
@@ -979,7 +983,7 @@
979983
/vi/
980984
/vi/404/
981985
/vi/about/
982-
/vi/arbitrum-faq/
986+
/vi/arbitrum/arbitrum-faq/
983987
/vi/billing/
984988
/vi/cookbook/arweave/
985989
/vi/cookbook/base-testnet/
@@ -1033,7 +1037,7 @@
10331037
/zh/
10341038
/zh/404/
10351039
/zh/about/
1036-
/zh/arbitrum-faq/
1040+
/zh/arbitrum/arbitrum-faq/
10371041
/zh/billing/
10381042
/zh/cookbook/arweave/
10391043
/zh/cookbook/base-testnet/

website/src/remarkReplaceImages.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Root } from 'mdast'
2+
import { Plugin } from 'unified'
3+
import { visit } from 'unist-util-visit'
4+
5+
export const remarkReplaceImages: Plugin<[{ assetsBasePath: string }], Root> = ({ assetsBasePath }) => {
6+
if (!assetsBasePath) throw new Error('remarkReplaceLinks: assetsBasePath is required')
7+
8+
return (tree, _file, done) => {
9+
visit(
10+
tree,
11+
[
12+
{ type: 'mdxJsxTextElement', name: 'img' },
13+
{ type: 'mdxJsxFlowElement', name: 'img' },
14+
],
15+
(node: any) => {
16+
const attrs = node.attributes as { name: string; value: string }[]
17+
const srcAttr = attrs.find((attr) => attr.name === 'src')!
18+
if (srcAttr.value.includes('.gitbook/assets/')) {
19+
srcAttr.value = srcAttr.value.replace(
20+
/.*\.gitbook\/assets\//,
21+
`https://raw.githubusercontent.com/${assetsBasePath}.gitbook/assets/`
22+
)
23+
}
24+
}
25+
)
26+
visit(tree, 'image', (node) => {
27+
if (node.url.includes('../assets/')) {
28+
node.url = node.url.replace(/.*\.\.\/assets\//, `https://raw.githubusercontent.com/${assetsBasePath}assets/`)
29+
}
30+
})
31+
done()
32+
}
33+
}

website/src/replaceGitBookContent.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
type ReplaceGitBookContentParams = {
2-
content: string
3-
user: string
4-
repo: string
5-
branch: string
6-
docsPath: string
7-
}
8-
9-
export function replaceGitBookContent({ content, user, repo, branch, docsPath }: ReplaceGitBookContentParams): string {
1+
export function replaceGitBookContent({ content }: { content: string }): string {
102
return (
113
content
124
// replace {% embed ... %} with <iframe />
@@ -23,11 +15,5 @@ export function replaceGitBookContent({ content, user, repo, branch, docsPath }:
2315
.replaceAll(/{%.*?%}/g, '')
2416
// close unclosed img tags
2517
.replaceAll(/<img((?:(?!\/>)[^>])*?)>/g, (...m) => `<img${m[1]}/>`)
26-
// Replaces all the relative paths of images to absolute paths to the repo
27-
.replaceAll('../assets', `https://raw.githubusercontent.com/${user}/${repo}/${branch}/assets/`)
28-
.replaceAll(
29-
'.gitbook/assets/',
30-
`https://raw.githubusercontent.com/${user}/${repo}/${branch}/${docsPath}.gitbook/assets/`
31-
)
3218
)
3319
}

0 commit comments

Comments
 (0)