Skip to content

Commit fc22f64

Browse files
committed
fix: centralized Img comp for both html and mdx
1 parent 727e45a commit fc22f64

File tree

8 files changed

+67
-141
lines changed

8 files changed

+67
-141
lines changed

package-lock.json

Lines changed: 32 additions & 130 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"build": "bin/build.mjs"
88
},
99
"devDependencies": {
10+
"@types/hast": "^3.0.4",
1011
"@types/minimist": "^1.2.5",
1112
"@types/node": "^18.19.44",
1213
"@types/react": "^18.3.3",

src/app/globals.css

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ body {
2121
@tailwind utilities;
2222
/* Stop purging. */
2323

24-
@layer utilities {
25-
img,
26-
.img {
27-
@apply bg-surface-container inline-block rounded-lg;
28-
}
29-
}
30-
3124
/* Your own custom utilities */
3225
@supports (position: sticky) {
3326
@media (min-width: theme('screens.lg')) {

src/components/mdx/Img/Img.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import cn from '@/lib/cn'
2+
import { ComponentProps } from 'react'
3+
4+
export function Img({ src, alt = '', className, ...props }: ComponentProps<'img'>) {
5+
return (
6+
// eslint-disable-next-line @next/next/no-img-element
7+
<img
8+
src={src}
9+
decoding="async"
10+
loading="lazy"
11+
alt={alt}
12+
className={cn('bg-surface-container inline-block rounded-lg', className)}
13+
{...props}
14+
/>
15+
)
16+
}

src/components/mdx/Img/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './Img'

src/components/mdx/Img/rehypeImg.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { Root } from 'hast'
2+
import { visit } from 'unist-util-visit'
3+
4+
// https://unifiedjs.com/learn/guide/create-a-rehype-plugin/
5+
export function rehypeImg() {
6+
return (tree: Root) => {
7+
visit(tree, null, function (node) {
8+
// console.log(node)
9+
if (node.type === 'mdxJsxFlowElement' && node.name === 'img') node.name = 'Img' // map HTML <img> to <Img> React component
10+
})
11+
}
12+
}

src/components/mdx/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ export * from './Codesandbox'
22
export * from './Gha'
33
export * from './Grid'
44
export * from './Hint'
5+
export * from './Img'
56
export * from './Toc'
67

78
import cn from '@/lib/cn'
89
import { MARKDOWN_REGEX } from '@/utils/docs'
910
import { Sandpack as SP } from '@codesandbox/sandpack-react'
1011
import { ComponentProps } from 'react'
12+
import { Img } from './Img'
1113

1214
type Hn = 'h2' | 'h3' | 'h4' | 'h5' | 'h6'
1315
function Heading({ id, Tag, ...props }: { id?: string; Tag: Hn } & ComponentProps<Hn>) {
@@ -84,10 +86,7 @@ export const a = ({ href, target, rel, ...props }: ComponentProps<'a'>) => {
8486
return <a {...props} href={href} target={target} rel={rel} className="text-primary" />
8587
}
8688

87-
export const img = ({ src, alt = '', ...props }: ComponentProps<'img'>) => (
88-
// eslint-disable-next-line @next/next/no-img-element
89-
<img src={src} decoding="async" loading="lazy" alt={alt} {...props} />
90-
)
89+
export const img = Img
9190

9291
export const code = (props: ComponentProps<'code'>) => (
9392
<code

src/utils/docs.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Codesandbox } from '@/components/mdx/Codesandbox'
44
import { fetchCSB } from '@/components/mdx/Codesandbox/fetchCSB'
55
import { rehypeCodesandbox } from '@/components/mdx/Codesandbox/rehypeCodesandbox'
66
import { rehypeGha } from '@/components/mdx/Gha/rehypeGha'
7+
import { rehypeImg } from '@/components/mdx/Img/rehypeImg'
78
import { rehypeToc } from '@/components/mdx/Toc/rehypeToc'
89
import resolveMdxUrl from '@/utils/resolveMdxUrl'
910
import matter from 'gray-matter'
@@ -156,6 +157,7 @@ async function _getDocs(
156157
mdxOptions: {
157158
remarkPlugins: [remarkGFM],
158159
rehypePlugins: [
160+
rehypeImg,
159161
rehypeGha,
160162
rehypePrismPlus,
161163
rehypeCodesandbox(boxes), // 1. put all Codesandbox[id] into `doc.boxes`

0 commit comments

Comments
 (0)