Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): render image file with base href #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions src/renderers/image/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`<Image /> when receives a Image block renders with file base href 1`] = `
<figure>
<img
alt="Deep in the universe"
src="https://google.com/uploads/2020/08/681-2000x1300-2.jpg"
/>
<figcaption>
Deep in the universe
</figcaption>
</figure>
`;

exports[`<Image /> when receives a Image block renders without file base href 1`] = `
<figure>
<img
alt="Deep in the universe"
src="/uploads/2020/08/681-2000x1300-2.jpg"
/>
<figcaption>
Deep in the universe
</figcaption>
</figure>
`;

exports[`<Image /> when receives a Image block with actions renders a <figure> block with <img> and <figcaption> 1`] = `
<figure
className="special-image image-block--stretched image-block--with-border image-block--with-background"
Expand Down
23 changes: 23 additions & 0 deletions src/renderers/image/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,27 @@ describe('<Image />', () => {
expect(create(<Image data={data} />).toJSON()).toMatchSnapshot();
});
});



describe('when receives a Image block', () => {
const data: ImageBlockData = {
file: {
url: '/uploads/2020/08/681-2000x1300-2.jpg',
name: '681-2000x1300-2',
},
caption: 'Deep in the universe',
withBorder: false,
stretched: false,
withBackground: false,
};

it('renders with file base href', () => {
expect(create(<Image data={data} fileBaseHref={'https://google.com'} />).toJSON()).toMatchSnapshot();
});

it('renders without file base href', () => {
expect(create(<Image data={data} />).toJSON()).toMatchSnapshot();
});
});
});
4 changes: 3 additions & 1 deletion src/renderers/image/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ export interface ImageBlockData {
}

export interface ImageBlockConfig {
fileBaseHref?: string;
actionsClassNames?: {
[s: string]: string;
};
}

const Image: RenderFn<ImageBlockData, ImageBlockConfig> = ({
data,
fileBaseHref = null,
className = '',
actionsClassNames = {
stretched: 'image-block--stretched',
Expand Down Expand Up @@ -50,7 +52,7 @@ const Image: RenderFn<ImageBlockData, ImageBlockConfig> = ({

return (
<figure {...figureprops}>
{data?.file?.url && <img src={data.file.url} alt={data.caption || data.file.name} />}
{data?.file?.url && <img src={fileBaseHref ? fileBaseHref + data.file.url : data.file.url} alt={data.caption || data.file.name} />}
{data?.url && <img src={data.url} alt={data.caption} />}
{data?.caption && <figcaption>{HTMLReactParser(data.caption)}</figcaption>}
</figure>
Expand Down