Skip to content

Commit

Permalink
fix: image blocks (#968)
Browse files Browse the repository at this point in the history
[![PR App][icn]][demo] | Ref RM-10779
:-------------------:|:----------:

## 🧰 Changes

A previous PR broke compiling images. It was failing to compiler or
parse the height attribute.

## 🧬 QA & Testing

- [Broken on production][prod].
- [Working in this PR app][demo].


[demo]: https://markdown-pr-PR_NUMBER.herokuapp.com
[prod]: https://SUBDOMAIN.readme.io
[icn]:
https://user-images.githubusercontent.com/886627/160426047-1bee9488-305a-4145-bb2b-09d8b757d38a.svg
  • Loading branch information
kellyjosephprice authored Sep 13, 2024
1 parent f880c93 commit 3cc4b4a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion __tests__/compilers/images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('image compiler', () => {
});

it('correctly serializes an Image component back to MDX', () => {
const doc = '<Image src="/path/to/image.png" width="200px" alt="alt text" />';
const doc = '<Image src="/path/to/image.png" width="200px" height="150px" alt="alt text" />';

expect(mdx(mdast(doc))).toMatch(doc);
});
Expand Down
6 changes: 5 additions & 1 deletion processor/transform/readme-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,20 @@ const coerceJsxToMd =
align,
border,
caption,
height,
title = null,
width,
src,
} = getAttrs<Pick<ImageBlock, 'alt' | 'align' | 'border' | 'title' | 'width' | 'src' | 'caption'>>(node);
} = getAttrs<Pick<ImageBlock, 'alt' | 'align' | 'border' | 'height' | 'title' | 'width' | 'src' | 'caption'>>(
node,
);

const attrs = {
...(align && { align }),
...(border && { border }),
...(src && { src }),
...(width && { width }),
...(height && { height }),
alt,
children: caption ? mdast(caption).children : (node.children as any),
title,
Expand Down
2 changes: 1 addition & 1 deletion processor/transform/readme-to-mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { visit } from 'unist-util-visit';
import { toAttributes } from '../utils';
import { type HTMLBlock } from 'types';

const imageAttrs = ['align', 'alt', 'caption', 'border', 'src', 'title', 'width', 'lazy', 'className'];
const imageAttrs = ['align', 'alt', 'caption', 'border', 'height', 'src', 'title', 'width', 'lazy', 'className'];

const readmeToMdx = (): Transform => tree => {
// Unwrap pinned nodes, replace rdme-pin with its child node
Expand Down
1 change: 1 addition & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ interface ImageBlockAttrs {
border?: string;
caption?: string;
className?: string;
height?: string;
lazy?: boolean;
src: string;
title: string;
Expand Down

0 comments on commit 3cc4b4a

Please sign in to comment.