Skip to content

Commit 837fd79

Browse files
Merge pull request #33 from moveyourdigital/issue-25-raw-html
Add support for Raw HTML block
2 parents 6ff7af4 + 0c40feb commit 837fd79

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ The package ships with the following renderers, but you can include your custom
5757
- Table
5858
- Quote
5959
- Delimiter
60+
- Raw (HTML)
6061

6162
## Styling and optional configs
6263

src/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Image from './renderers/image';
77
import List from './renderers/list';
88
import Paragraph from './renderers/paragraph';
99
import Quote from './renderers/quote';
10+
import Raw from './renderers/raw';
1011
import Table from './renderers/table';
1112

1213
export type ConfigProp = Record<string, RenderConfig>;
@@ -59,6 +60,7 @@ const Blocks = ({
5960
paragraph: Paragraph,
6061
quote: Quote,
6162
table: Table,
63+
raw: Raw,
6264
};
6365

6466
const availableRenderers = {
@@ -92,4 +94,5 @@ export {
9294
Paragraph as ParagraphBlock,
9395
Quote as QuoteBlock,
9496
Table as TableBlock,
97+
Raw as RawBlock,
9598
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`<Raw /> when receives a raw block renders content as HTML 1`] = `
4+
<div
5+
style={
6+
Object {
7+
"background": "#000",
8+
"color": "#fff",
9+
"fontSize": "30px",
10+
"padding": "50px",
11+
}
12+
}
13+
>
14+
Any HTML code
15+
</div>
16+
`;

src/renderers/raw/index.test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react';
2+
import { create } from 'react-test-renderer';
3+
import Raw, { RawBlockData } from '.';
4+
5+
describe('<Raw />', () => {
6+
describe('when receives a raw block', () => {
7+
const data: RawBlockData = {
8+
html: '<div style="background: #000; color: #fff; font-size: 30px; padding: 50px;">Any HTML code</div>',
9+
};
10+
11+
it('renders content as HTML', () => {
12+
expect(create(<Raw data={data} />).toJSON()).toMatchSnapshot();
13+
});
14+
});
15+
});

src/renderers/raw/index.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import HTMLReactParser from 'html-react-parser';
2+
import React from 'react';
3+
import { RenderFn } from '../..';
4+
5+
export interface RawBlockData {
6+
html: string;
7+
}
8+
9+
const Raw: RenderFn<RawBlockData> = ({ data }) => {
10+
return <>{data?.html && HTMLReactParser(data.html)}</>;
11+
};
12+
13+
export default Raw;

0 commit comments

Comments
 (0)