11import { ComponentProps , ReactElement } from 'react' ;
2- import { useRouter } from 'next/router' ;
2+ import { addBasePath } from 'next/dist/client/add-base-path' ;
3+ import NextImage from 'next/image' ;
34import clsx from 'clsx' ;
45
56export const mdxComponents : {
@@ -13,7 +14,7 @@ export const mdxComponents: {
1314 if ( ext === 'mov' ) {
1415 ext = 'quicktime' ;
1516 }
16- return < source { ...props } src = { src } type = { type || `video/${ ext } ` } /> ;
17+ return < source { ...props } src = { addBasePath ( src ) } type = { type || `video/${ ext } ` } /> ;
1718 } ,
1819 video : ( { className, children, ...props } : ComponentProps < 'video' > ) => (
1920 < video className = { clsx ( 'mt-6 w-full' , className ) } autoPlay loop muted { ...props } >
@@ -30,13 +31,13 @@ export const mdxComponents: {
3031 { ...props }
3132 />
3233 ) ,
33- img ( { src = '' , alt, ...props } : ComponentProps < 'img' > ) {
34- // eslint-disable-next-line react-hooks/rules-of-hooks -- false positive
35- const { basePath } = useRouter ( ) ;
36- if ( ! src . startsWith ( 'http' ) ) {
34+ img ( props : ComponentProps < 'img' > ) {
35+ const ComponentToUse = typeof props . src === 'object' ? NextImage : 'img' ;
36+ if ( typeof props . src === 'string' && ! props . src . startsWith ( 'http' ) ) {
3737 // eslint-disable-next-line no-console -- just for debug to notice that NextImage was not used
38- console . warn ( 'Image' , src , "doesn't use NextImage" ) ;
38+ console . warn ( 'Image' , props . src , "doesn't use NextImage" ) ;
3939 }
40- return < img { ...props } src = { src . startsWith ( '/' ) ? basePath + src : src } alt = { alt } /> ;
40+ // @ts -expect-error -- fixme
41+ return < ComponentToUse { ...props } /> ;
4142 } ,
4243} ;
0 commit comments