Skip to content

Commit

Permalink
fix: centralized Img comp for both html and mdx
Browse files Browse the repository at this point in the history
  • Loading branch information
abernier committed Aug 27, 2024
1 parent 727e45a commit fc22f64
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 141 deletions.
162 changes: 32 additions & 130 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"build": "bin/build.mjs"
},
"devDependencies": {
"@types/hast": "^3.0.4",
"@types/minimist": "^1.2.5",
"@types/node": "^18.19.44",
"@types/react": "^18.3.3",
Expand Down
7 changes: 0 additions & 7 deletions src/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ body {
@tailwind utilities;
/* Stop purging. */

@layer utilities {
img,
.img {
@apply bg-surface-container inline-block rounded-lg;
}
}

/* Your own custom utilities */
@supports (position: sticky) {
@media (min-width: theme('screens.lg')) {
Expand Down
16 changes: 16 additions & 0 deletions src/components/mdx/Img/Img.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import cn from '@/lib/cn'
import { ComponentProps } from 'react'

export function Img({ src, alt = '', className, ...props }: ComponentProps<'img'>) {
return (
// eslint-disable-next-line @next/next/no-img-element
<img
src={src}
decoding="async"
loading="lazy"
alt={alt}
className={cn('bg-surface-container inline-block rounded-lg', className)}
{...props}
/>
)
}
1 change: 1 addition & 0 deletions src/components/mdx/Img/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Img'
12 changes: 12 additions & 0 deletions src/components/mdx/Img/rehypeImg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { Root } from 'hast'
import { visit } from 'unist-util-visit'

// https://unifiedjs.com/learn/guide/create-a-rehype-plugin/
export function rehypeImg() {
return (tree: Root) => {
visit(tree, null, function (node) {
// console.log(node)
if (node.type === 'mdxJsxFlowElement' && node.name === 'img') node.name = 'Img' // map HTML <img> to <Img> React component
})
}
}
7 changes: 3 additions & 4 deletions src/components/mdx/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ export * from './Codesandbox'
export * from './Gha'
export * from './Grid'
export * from './Hint'
export * from './Img'
export * from './Toc'

import cn from '@/lib/cn'
import { MARKDOWN_REGEX } from '@/utils/docs'
import { Sandpack as SP } from '@codesandbox/sandpack-react'
import { ComponentProps } from 'react'
import { Img } from './Img'

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

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

export const code = (props: ComponentProps<'code'>) => (
<code
Expand Down
2 changes: 2 additions & 0 deletions src/utils/docs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Codesandbox } from '@/components/mdx/Codesandbox'
import { fetchCSB } from '@/components/mdx/Codesandbox/fetchCSB'
import { rehypeCodesandbox } from '@/components/mdx/Codesandbox/rehypeCodesandbox'
import { rehypeGha } from '@/components/mdx/Gha/rehypeGha'
import { rehypeImg } from '@/components/mdx/Img/rehypeImg'
import { rehypeToc } from '@/components/mdx/Toc/rehypeToc'
import resolveMdxUrl from '@/utils/resolveMdxUrl'
import matter from 'gray-matter'
Expand Down Expand Up @@ -156,6 +157,7 @@ async function _getDocs(
mdxOptions: {
remarkPlugins: [remarkGFM],
rehypePlugins: [
rehypeImg,
rehypeGha,
rehypePrismPlus,
rehypeCodesandbox(boxes), // 1. put all Codesandbox[id] into `doc.boxes`
Expand Down

0 comments on commit fc22f64

Please sign in to comment.