Skip to content

Commit

Permalink
feat: auto-img [w/h]
Browse files Browse the repository at this point in the history
  • Loading branch information
abernier committed Aug 28, 2024
1 parent 87c19c3 commit 9ed6731
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
13 changes: 6 additions & 7 deletions docs/getting-started/authoring.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,20 @@ Relative images are supported. Eg, inside your [`MDX`](introduction#configuratio
title: Authoring
---

<img src="gutenberg.jpg" />
![](gutenberg.jpg)
```

<details>
<summary>Result</summary>

<img src="gutenberg.jpg" />
![](gutenberg.jpg)

</details>

> [!IMPORTANT]
> To avoid layout-shifts, always provides `[width]`/`[height]` img's attributes:
> ```md
> <img src='gutenberg.jpg' width="1100" height="685" />
> ```
> [!TIP]
> You are encouraged to use relative images, since dimensions are automatically added as `img[width][height]` attributes.
>
> For others, think about adding them to **prevent layout shift**.
> [!NOTE]
> See [`MDX_BASEURL`](introduction#configuration:~:text=MDX_BASEURL) to understand how it is converted to a full URL.
Expand Down
6 changes: 2 additions & 4 deletions docs/getting-started/introduction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: Documentation generator for `pmndrs/*` projects.
nav: 0
---

<img src="gutenberg.jpg" width="1100" height="685" alt="Gutenberg lithography" />
![Gutenberg lithography](gutenberg.jpg)

## INSTALL

Expand Down Expand Up @@ -70,9 +70,7 @@ becomes (for a `MDX_BASEURL=http://localhost:60141` value):

We implement [m3 design system](https://m3.material.io/styles/color/system/overview), using [Tailwind Material Colors](https://tailwind-material-colors-docs.vercel.app).

<a href="https://tailwind-material-colors-docs.vercel.app#palette">
<img src="color-scheme.png" width="1506" height="864" alt="" />
</a>
[![color scheme](color-scheme.png)](https://tailwind-material-colors-docs.vercel.app#palette)

> [!NOTE]
> - [Material Color](https://www.youtube.com/playlist?list=PLsoLz-E4Os4WWkrvRuQ7BJuVF-WfOyfWT) for more information
Expand Down
26 changes: 25 additions & 1 deletion 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 @@ -34,6 +34,7 @@
"@tailwindcss/typography": "^0.5.14",
"clsx": "^2.1.1",
"gray-matter": "^4.0.3",
"image-size": "^1.1.1",
"match-sorter": "^6.3.4",
"next": "^14.2.5",
"next-mdx-remote": "^5.0.0",
Expand Down
17 changes: 16 additions & 1 deletion src/components/mdx/Img/Img.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import cn from '@/lib/cn'
import { ComponentProps } from 'react'

export function Img({ src, alt = '', className, ...props }: ComponentProps<'img'>) {
import sizeOf from 'image-size'
import { resolve } from 'path'

export async function Img({ src, alt = '', className, ...props }: ComponentProps<'img'>) {
const dims: Partial<Pick<ComponentProps<'img'>, 'width' | 'height'>> = {
width: undefined,
height: undefined,
}
if (process.env.MDX_BASEURL && src?.startsWith(process.env.MDX_BASEURL)) {
const path = resolve(src.replace(process.env.MDX_BASEURL, process.env.MDX!))
const { width, height } = sizeOf(path)
dims.width = width
dims.height = height
}

return (
// eslint-disable-next-line @next/next/no-img-element
<img
Expand All @@ -10,6 +24,7 @@ export function Img({ src, alt = '', className, ...props }: ComponentProps<'img'
loading="lazy"
alt={alt}
className={cn('bg-surface-container inline-block rounded-lg', className)}
{...dims}
{...props}
/>
)
Expand Down

0 comments on commit 9ed6731

Please sign in to comment.