Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix: images
  • Loading branch information
kellyjosephprice committed May 29, 2024
commit a597c05e6843c8105b58be5d7c698884b5fa0d1b
2 changes: 1 addition & 1 deletion __tests__/transformers/readme-components.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Second
},
image: {
md: `![](http://placekitten.com/600/200)`,
mdx: `<Image src="http://placekitten.com/600/200" />`,
mdx: `<Image url="http://placekitten.com/600/200" />`,
},
table: {
md: `
Expand Down
15 changes: 10 additions & 5 deletions processor/compile/image.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import type { Image } from "mdast";
import type { Image } from 'mdast';

const image = (node: Image) => {
const { align, className, width } = node.data?.hProperties || {};
const complexImage: boolean = Boolean(width) || Boolean(className) || Boolean(align);


console.log(JSON.stringify({ node }, null, 2));

if (complexImage) {
const attributes = Object.keys(node.data?.hProperties).map(key => `${key}="${node.data?.hProperties[key]}"`).join(' ')
const attributes = Object.keys(node.data?.hProperties)
.map(key => `${key}="${node.data?.hProperties[key]}"`)
.join(' ');
return `<Image ${attributes} />`;
}

return `![${node.alt}](${node.url}${node.title ? ` "${node.title}")` : ')'}`;
}
};

export default image;

export default image;
4 changes: 2 additions & 2 deletions processor/transform/readme-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ const coerceJsxToMd =
parent.children[index] = mdNode;
} else if (node.name === 'Image') {
const { position } = node;
const { alt = '', src, title = null } = attributes<Pick<Image, 'alt' | 'title'> & { src: string }>(node);
const { alt = '', url, title = null } = attributes<Pick<Image, 'alt' | 'title' | 'url'>>(node);

const mdNode: Image = {
alt,
position,
title,
type: 'image',
url: src,
url,
};

parent.children[index] = mdNode;
Expand Down