Skip to content

Add support for Raw HTML block #33

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

Merged
merged 1 commit into from
Jan 24, 2022
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ The package ships with the following renderers, but you can include your custom
- Table
- Quote
- Delimiter
- Raw (HTML)

## Styling and optional configs

Expand Down
3 changes: 3 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Image from './renderers/image';
import List from './renderers/list';
import Paragraph from './renderers/paragraph';
import Quote from './renderers/quote';
import Raw from './renderers/raw';
import Table from './renderers/table';

export type ConfigProp = Record<string, RenderConfig>;
Expand Down Expand Up @@ -59,6 +60,7 @@ const Blocks = ({
paragraph: Paragraph,
quote: Quote,
table: Table,
raw: Raw,
};

const availableRenderers = {
Expand Down Expand Up @@ -92,4 +94,5 @@ export {
Paragraph as ParagraphBlock,
Quote as QuoteBlock,
Table as TableBlock,
Raw as RawBlock,
};
16 changes: 16 additions & 0 deletions src/renderers/raw/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`<Raw /> when receives a raw block renders content as HTML 1`] = `
<div
style={
Object {
"background": "#000",
"color": "#fff",
"fontSize": "30px",
"padding": "50px",
}
}
>
Any HTML code
</div>
`;
15 changes: 15 additions & 0 deletions src/renderers/raw/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import { create } from 'react-test-renderer';
import Raw, { RawBlockData } from '.';

describe('<Raw />', () => {
describe('when receives a raw block', () => {
const data: RawBlockData = {
html: '<div style="background: #000; color: #fff; font-size: 30px; padding: 50px;">Any HTML code</div>',
};

it('renders content as HTML', () => {
expect(create(<Raw data={data} />).toJSON()).toMatchSnapshot();
});
});
});
13 changes: 13 additions & 0 deletions src/renderers/raw/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import HTMLReactParser from 'html-react-parser';
import React from 'react';
import { RenderFn } from '../..';

export interface RawBlockData {
html: string;
}

const Raw: RenderFn<RawBlockData> = ({ data }) => {
return <>{data?.html && HTMLReactParser(data.html)}</>;
};

export default Raw;