Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions __tests__/migration/image.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { migrate } from '../helpers';

describe('migrating images', () => {
it('compiles images', () => {
it('migrates markdown images', () => {
const md = `
![now with alt text!](https://fastly.picsum.photos/id/507/200/300.jpg?hmac=v0NKvUrOWTKZuZFmMlLN_7-RdRgeF-qFLeBGXpufxgg)
`;

const mdx = migrate(md);
expect(mdx).toMatchInlineSnapshot(`
"![now with alt text!](https://fastly.picsum.photos/id/507/200/300.jpg?hmac=v0NKvUrOWTKZuZFmMlLN_7-RdRgeF-qFLeBGXpufxgg)
"
`);
});

it('migrates magic block images', () => {
const md = `
[block:image]
{
Expand All @@ -10,7 +22,7 @@ describe('migrating images', () => {
"image": [
"https://fastly.picsum.photos/id/507/200/300.jpg?hmac=v0NKvUrOWTKZuZFmMlLN_7-RdRgeF-qFLeBGXpufxgg",
"",
""
"now with alt text!"
],
"align": "center",
"border": true
Expand All @@ -22,7 +34,36 @@ describe('migrating images', () => {

const mdx = migrate(md);
expect(mdx).toMatchInlineSnapshot(`
"<Image align="center" className="border" border={true} src="https://fastly.picsum.photos/id/507/200/300.jpg?hmac=v0NKvUrOWTKZuZFmMlLN_7-RdRgeF-qFLeBGXpufxgg" />
"<Image alt="now with alt text!" align="center" className="border" border={true} src="https://fastly.picsum.photos/id/507/200/300.jpg?hmac=v0NKvUrOWTKZuZFmMlLN_7-RdRgeF-qFLeBGXpufxgg" />
"
`);
});

it('migrates magic block images with captions', () => {
const md = `
[block:image]
{
"images": [
{
"image": [
"https://fastly.picsum.photos/id/507/200/300.jpg?hmac=v0NKvUrOWTKZuZFmMlLN_7-RdRgeF-qFLeBGXpufxgg",
"",
"now with alt text!"
],
"align": "center",
"border": true,
"caption": "This is a caption"
}
]
}
[/block]
`;

const mdx = migrate(md);
expect(mdx).toMatchInlineSnapshot(`
"<Image alt="now with alt text!" align="center" border={true} src="https://fastly.picsum.photos/id/507/200/300.jpg?hmac=v0NKvUrOWTKZuZFmMlLN_7-RdRgeF-qFLeBGXpufxgg">
This is a caption
</Image>
"
`);
});
Expand Down
4 changes: 2 additions & 2 deletions processor/transform/readme-to-mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const readmeToMdx = (): Transform => tree => {
if (!('data' in image)) return;

if ('url' in image) image.data.hProperties.src = image.url;
const attributes = toAttributes(image.data.hProperties, imageAttrs);
const attributes = toAttributes({ ...image, ...image.data.hProperties }, imageAttrs);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We put the alt attribute directly on the image node, because that's how remark does it. But we weren't following that shape here.


if (image.data.hProperties.className === 'emoji') {
parent.children.splice(index, 1, {
Expand All @@ -106,7 +106,7 @@ const readmeToMdx = (): Transform => tree => {
});

visit(tree, NodeTypes.imageBlock, (image, index, parent) => {
const attributes = toAttributes(image.data.hProperties, imageAttrs);
const attributes = toAttributes({ ...image, ...image.data.hProperties }, imageAttrs);

if (hasExtra(attributes)) {
parent.children.splice(index, 1, {
Expand Down