Skip to content

Commit a87305f

Browse files
Merge pull request #2 from moveyourdigital/fix-add-more-info
Added Github info
2 parents 5e30ede + e95a1d5 commit a87305f

File tree

9 files changed

+276
-14
lines changed

9 files changed

+276
-14
lines changed

.github/CONTRIBUTING.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Contributing to EditorJS Blocks React Renderer
2+
3+
The following is a set of guidelines for contributing to this repository.
4+
5+
## Code of Conduct
6+
We expect participants to adhere to the following set of rules.
7+
8+
## Open Development
9+
All work on this repository happens directly on GitHub. Both core team members and external contributors send pull requests which go through the same review process.
10+
11+
## Branch Organization
12+
We maintain master branch for development of new features. If you want to push a bugfix of the last version, please open a pull request against the last tag version commit and another to the master, if needed.
13+
14+
## Bugs
15+
We are using GitHub Issues for bug tracking. Please, provide reproduction steps for the bug. Before that, make sure the bug is not already reported in the Issues section.
16+
17+
## Proposing a Change
18+
If you want to introduce a new renderer or augment the public API you are free to either open a Pull Request with the change or an issue, which will be opened for further discussions.
19+
20+
If your change breaks the current public API, our recommendation is to start a thread in a new Issue.
21+
22+
## Your First Pull Request
23+
Working on your first Pull Request? You can learn how from this free video series:
24+
25+
[How to Contribute to an Open Source Project on GitHub](https://egghead.io/courses/how-to-contribute-to-an-open-source-project-on-github)
26+
27+
28+
## Sending a Pull Request
29+
The core team is monitoring for pull requests. We will review your pull request and either merge it, request changes to it, or close it with an explanation.
30+
31+
**Before submitting a pull request**, please make sure the following is done:
32+
33+
1) Fork the repository and create your branch from the correct branch/tag.
34+
2) Run `npm install` in the repository root.
35+
3) Add or change tests as needed.
36+
4) Ensure the test suite passes with `npm test`. Tip: `npm run test:watch` development.
37+
5) Run `npm test -- -u` to update the jest snapshots and commit these changes as well (if there are any updates).
38+
6) Run `npm run list && npm run format` and commit any changes.

.github/ISSUE_TEMPLATE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
### Purpose
2+
3+
- [ ] New feature
4+
- [ ] New renderer
5+
- [ ] Bug fix
6+
- [ ] Documentation
7+
- [ ] Refactoring
8+
- [ ] Test Case
9+
- [ ] Other
10+
11+
### Description
12+
13+
<!--
14+
Include a brief description
15+
-->
16+
17+
### Possible solution
18+
19+
<!--
20+
If needed, provide a possible solution for the problem.
21+
-->
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Integration
2+
3+
on: pull_request
4+
5+
jobs:
6+
ci:
7+
name: Continuous Integration
8+
runs-on: ubuntu-latest
9+
10+
strategy:
11+
matrix:
12+
node-version: [12.x]
13+
14+
steps:
15+
- uses: actions/checkout@v1
16+
with:
17+
fetch-depth: 1
18+
19+
- name: Use Node.js ${{ matrix.node-version }}
20+
uses: actions/setup-node@v1
21+
with:
22+
node-version: ${{ matrix.node-version }}
23+
24+
- name: Install
25+
run: |
26+
npm install
27+
env:
28+
CI: true
29+
30+
- name: Testing
31+
run: |
32+
npm run lint
33+
npm test
34+
35+
env:
36+
CI: true

README.md

Lines changed: 142 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,142 @@
1-
# editorjs-blocks-react-renderer
2-
Renders EditorJS block format to React components.
1+
# Editor.js Blocks React Renderer
2+
3+
Renders Editor.js blocks to semantic React HTML5 components. Unnopinated and flexible.
4+
5+
[![](https://flat.badgen.net/npm/v/editorjs-blocks-react-renderer?icon=npm)](https://www.npmjs.com/package/editorjs-blocks-react-renderer)
6+
[![](https://flat.badgen.net/npm/license/editorjs-blocks-react-renderer)](https://www.npmjs.com/package/editorjs-blocks-react-renderer)
7+
8+
An unnopinated React library that renders flexible HTML5 React components from [Editor.js](https://editorjs.io/) block style data.
9+
10+
It follows semantic practices and without inlining styles. It's flexible enough to allow developers to include their own styles via CSS classes, which can be passed to each renderer via configuration.
11+
12+
This also supports server side rendering.
13+
14+
## Usage
15+
16+
Install the package via npm:
17+
```sh
18+
npm i -S editorjs-blocks-react-renderer
19+
```
20+
21+
Import in your component:
22+
```js
23+
import Blocks from 'editorjs-blocks-react-renderer';
24+
```
25+
26+
Use it with a [block style data](https://editorjs.io/saving-data) from Editor.js:
27+
```jsx
28+
export const Article = () => <Blocks data={dataFromEditor} />;
29+
```
30+
31+
## Render a single block
32+
33+
Blocks are independent and you can import only a set of them and use them diretly:
34+
```jsx
35+
import { Header } from 'editorjs-react-renderer';
36+
37+
const dataHeader = {
38+
"text": "Heading 2",
39+
"level": 2
40+
}
41+
42+
export const Heading () => <Header data={dataHeader} />;
43+
```
44+
45+
## Internal blocks
46+
47+
The package ships with the following renderers, but you can include your custom ones:
48+
- Code
49+
- Header
50+
- Paragraph
51+
- Image
52+
- Embed
53+
- List
54+
- Table
55+
- Quote
56+
- Delimiter
57+
58+
## Styling and optional configs
59+
60+
This library does not include/force any style nor inlines any styles. Before you ask, we're not supporting inline styles due to [Content-Security-Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) requirements.
61+
62+
However, each renderer supports a set of props, including `className` which can be used to style each block. You just need to pass a `config` object to `Blocks` or directly to any renderer like so:
63+
64+
```jsx
65+
<Blocks data={dataFromEditor} config={{
66+
code: {
67+
className: "language-js"
68+
},
69+
delimiter: {
70+
className: "article-hr"
71+
},
72+
embed: {
73+
className: "styled-iframe",
74+
rel: "nofollower",
75+
sandbox: "allow-fullscreen"
76+
}
77+
header: {
78+
className: "lead"
79+
},
80+
image: {
81+
className: "img-fluid",
82+
actionsClassNames: {
83+
stretched: 'image-block--stretched',
84+
withBorder: 'image-block--with-border',
85+
withBackground: 'image-block--with-background',
86+
}
87+
},
88+
list: {
89+
className: "unstyled-list"
90+
},
91+
paragraph: {
92+
className: "lead"
93+
},
94+
quote: {
95+
className: "block-quote",
96+
actionsClassNames: {
97+
alignment: 'text-align-{alignment}', // This is a substitution placeholder: left or center.
98+
}
99+
},
100+
table: {
101+
className: "table"
102+
}
103+
}} />
104+
```
105+
106+
Above you have all allowed properties for each renderer.
107+
108+
So, in theory, any CSS framework (such as Bootstrap) can work seamlessly with this library as long as you pass the correct properties.
109+
110+
## Custom Renderers
111+
112+
You can provide your own custom renderers or replace the default ones by passing a `renderers` object to the `Blocks`.
113+
114+
```tsx
115+
const Checklist = ({
116+
data, className = ""
117+
}: {
118+
data: {[s:string]: any}
119+
className?: string
120+
}) => {
121+
122+
return (
123+
<>
124+
{data?.items.map((item, i) => (
125+
<p key={i}>
126+
<label>
127+
<input type="checkbox" /> {ReactHtmlParser(item)}
128+
</label>
129+
</p>
130+
))}
131+
</>
132+
)
133+
}
134+
135+
export default () => <Blocks data={dataFromEditor} renderers={{
136+
checklist: Checklist
137+
}} />
138+
```
139+
140+
## Inspiration
141+
142+
- [EditorJS-React-Renderer](https://github.com/BomdiZane/EditorJS-React-Renderer) but without opinated inline styles.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
"scripts": {
1111
"start": "tsc -w",
1212
"build": "tsc",
13-
"test": "jest",
13+
"test": "jest --coverage",
1414
"test:watch": "jest --watch",
15-
"test:coverage": "jest --coverage",
1615
"format": "prettier --write \"src/**/*.tsx\"",
1716
"lint": "tslint -p tsconfig.json",
1817
"prepare" : "npm run build",

src/renderers/code/index.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ export interface CodeBlockData {
55
lang?: string;
66
}
77

8-
const Code = ({ data }: { data: CodeBlockData }) => {
9-
const codeprops: {
8+
const Code = ({ data, className = '' }: { data: CodeBlockData; className?: string }) => {
9+
const props: {
1010
[s: string]: string;
1111
} = {};
1212

13-
if (data?.lang) codeprops.lang = data.lang;
13+
if (className) {
14+
props.className = className;
15+
}
1416

15-
return <pre>{data?.code && <code {...codeprops}>{`${data.code}`}</code>}</pre>;
17+
if (data?.lang) props.lang = data.lang;
18+
19+
return <pre>{data?.code && <code {...props}>{`${data.code}`}</code>}</pre>;
1620
};
1721

1822
export default Code;

src/renderers/header/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@ export interface HeaderBlockData {
66
level: number;
77
}
88

9-
const Header = ({ data }: { data: HeaderBlockData }) => {
9+
const Header = ({ data, className = '' }: { data: HeaderBlockData; className?: string }) => {
10+
const props: {
11+
[s: string]: string;
12+
} = {};
13+
14+
if (className) {
15+
props.className = className;
16+
}
17+
1018
const Tag = `h${data.level || 1}` as keyof JSX.IntrinsicElements;
11-
return <Tag>{data?.text && ReactHtmlParser(data.text)}</Tag>;
19+
return <Tag {...props}>{data?.text && ReactHtmlParser(data.text)}</Tag>;
1220
};
1321

1422
export default Header;

src/renderers/list/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@ export interface ListBlockData {
66
items: string[];
77
}
88

9-
const List = ({ data }: { data: ListBlockData }) => {
9+
const List = ({ data, className = '' }: { data: ListBlockData; className?: string }) => {
10+
const props: {
11+
[s: string]: string;
12+
} = {};
13+
14+
if (className) {
15+
props.className = className;
16+
}
17+
1018
const Tag = (data?.style === 'ordered' ? `ol` : `ul`) as keyof JSX.IntrinsicElements;
1119
return (
12-
<Tag>
20+
<Tag {...props}>
1321
{data?.items.map((item, i) => (
1422
<li key={i}>{ReactHtmlParser(item)}</li>
1523
))}

src/renderers/paragraph/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@ export interface ParagraphBlockData {
55
text: string;
66
}
77

8-
const Paragraph = ({ data }: { data: ParagraphBlockData }) => {
9-
return <p>{data?.text && ReactHtmlParser(data.text)}</p>;
8+
const Paragraph = ({ data, className = '' }: { data: ParagraphBlockData; className?: string }) => {
9+
const props: {
10+
[s: string]: string;
11+
} = {};
12+
13+
if (className) {
14+
props.className = className;
15+
}
16+
17+
return <p {...props}>{data?.text && ReactHtmlParser(data.text)}</p>;
1018
};
1119

1220
export default Paragraph;

0 commit comments

Comments
 (0)