@@ -3,41 +3,39 @@ import {
3
3
Divider as ChakraDivider ,
4
4
Flex ,
5
5
Heading ,
6
- Image ,
7
- Link as ChakraLink ,
8
6
Text ,
9
7
chakra ,
10
8
} from "@chakra-ui/react"
11
9
import { ParsedUrlQuery } from "querystring"
12
- import { MDXRemote } from "next-mdx-remote"
13
- import { useRouter } from "next/router"
10
+ import { MDXRemote , type MDXRemoteSerializeResult } from "next-mdx-remote"
14
11
import { serialize } from "next-mdx-remote/serialize"
15
12
import remarkGfm from "remark-gfm"
13
+ import path from "path"
16
14
17
- import ButtonLink from ".. /components/ButtonLink"
18
- import DocLink from ".. /components/DocLink"
19
- import Emoji from ".. /components/Emoji"
20
- import EnergyConsumptionChart from ".. /components/EnergyConsumptionChart"
21
- import ExpandableCard from ".. /components/ExpandableCard"
15
+ import ButtonLink from "@ /components/ButtonLink"
16
+ import DocLink from "@ /components/DocLink"
17
+ import Emoji from "@ /components/Emoji"
18
+ import EnergyConsumptionChart from "@ /components/EnergyConsumptionChart"
19
+ import ExpandableCard from "@ /components/ExpandableCard"
22
20
import InfoBanner from "@/components/InfoBanner"
23
21
import Link from "@/components/Link"
24
22
import MarkdownTable from "@/components/MarkdownTable"
25
- import NetworkUpgradeSummary from "../components/History/NetworkUpgradeSummary"
26
- import YouTube from "../components/YouTube"
23
+ import MarkdownImage from "@/components/MarkdownImage"
24
+ import NetworkUpgradeSummary from "@/components/History/NetworkUpgradeSummary"
25
+ import YouTube from "@/components/YouTube"
27
26
28
27
import { getContent , getContentBySlug } from "@/lib/utils/md"
29
- import { getRelativePath } from "@/lib/utils/relativePath "
28
+ import rehypeImgSize from "@/lib/rehype/rehypeImgSize "
30
29
31
- import { GetStaticPaths , GetStaticProps , NextPage } from "next/types"
32
- import { ChildOnlyProp } from "@/lib/types"
33
- import { CONTENT_IMAGES_MAX_WIDTH } from "@/lib/constants"
30
+ import type { GetStaticPaths , GetStaticProps , NextPage } from "next/types"
31
+ import type { ChildOnlyProp } from "@/lib/types"
34
32
35
33
interface Params extends ParsedUrlQuery {
36
34
slug : string [ ]
37
35
}
38
36
39
37
interface Props {
40
- content : string
38
+ mdxSource : MDXRemoteSerializeResult
41
39
}
42
40
43
41
export const getStaticPaths : GetStaticPaths = ( ) => {
@@ -61,17 +59,21 @@ export const getStaticProps: GetStaticProps<Props, Params> = async (
61
59
) => {
62
60
const params = context . params !
63
61
const markdown = getContentBySlug ( params . slug . join ( "/" ) , [ "slug" , "content" ] )
64
- // TODO: check if content type can be fixed
65
- const content = ( await serialize ( markdown . content , {
62
+
63
+ const mdPath = path . join ( "/content" , ...params . slug )
64
+ const mdDir = path . join ( "public" , mdPath )
65
+
66
+ const mdxSource = await serialize ( markdown . content , {
66
67
mdxOptions : {
67
68
// Required since MDX v2 to compile tables (see https://mdxjs.com/migrating/v2/#gfm)
68
69
remarkPlugins : [ remarkGfm ] ,
70
+ rehypePlugins : [ [ rehypeImgSize , { dir : mdDir , srcPath : mdPath } ] ] ,
69
71
} ,
70
- } ) ) as any
72
+ } )
71
73
72
74
return {
73
75
props : {
74
- content ,
76
+ mdxSource ,
75
77
} ,
76
78
}
77
79
}
@@ -185,22 +187,6 @@ const ListItem = (props: ChildOnlyProp) => (
185
187
< chakra . li color = "text300" { ...props } />
186
188
)
187
189
188
- const Img = ( img : any ) => {
189
- // use router to get correct image relative path inside /public/content/ dynamically
190
- const router = useRouter ( )
191
- // TODO: update how `imgRelativePath` is computed for translated assets inside /translations, will depend on value of locale after setting up i18n
192
- const imgRelativePath = getRelativePath ( router . asPath , img . src )
193
-
194
- return (
195
- < ChakraLink href = { imgRelativePath } isExternal >
196
- < Image
197
- src = { imgRelativePath }
198
- alt = { img . alt }
199
- maxW = { CONTENT_IMAGES_MAX_WIDTH }
200
- />
201
- </ ChakraLink >
202
- )
203
- }
204
190
// code
205
191
const components = {
206
192
a : Link ,
@@ -209,7 +195,7 @@ const components = {
209
195
h3 : Header3 ,
210
196
h4 : Header4 ,
211
197
hr : HR ,
212
- img : Img ,
198
+ img : MarkdownImage ,
213
199
li : ListItem ,
214
200
p : Paragraph ,
215
201
pre : Pre ,
@@ -226,7 +212,7 @@ const components = {
226
212
YouTube,
227
213
}
228
214
229
- const ContentPage : NextPage < Props > = ( { content } ) => {
215
+ const ContentPage : NextPage < Props > = ( { mdxSource } ) => {
230
216
return (
231
217
< Box w = "full" >
232
218
< Flex
@@ -259,7 +245,9 @@ const ContentPage: NextPage<Props> = ({ content }) => {
259
245
} ,
260
246
} }
261
247
>
262
- < MDXRemote { ...( content as any ) } components = { components } />
248
+ { /* // TODO: fix components types, for some reason MDXRemote doesn't like some of them */ }
249
+ { /* @ts -ignore */ }
250
+ < MDXRemote { ...mdxSource } components = { components } />
263
251
</ Box >
264
252
</ Flex >
265
253
</ Box >
0 commit comments