Skip to content

Commit af54ecb

Browse files
author
nicky.out
committed
Simple no-die test, updated README
1 parent 49bca6a commit af54ecb

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,28 @@ const contentState = ContentState.createFromBlockArray(contentBlocks, entityMap)
2121
const editorState = EditorState.createWithContent(contentState);
2222
```
2323

24+
### (optional) customChunkRenderer
25+
Use to define additional html nodes. Only supports atomic blocks.
26+
27+
* _nodeName: string_ - the name of the node, in lowercase
28+
* _node: HTMLElement_ - the parsed node itself
29+
30+
This renderer function is executed before any other html to draft conversion.
31+
Return nothing (or something falsy) to continue with the normal translation.
32+
33+
Example:
34+
35+
```
36+
htmlToDraft('<hr/>', (nodeName, node) => {
37+
if (nodeName === 'hr') {
38+
return {
39+
type: 'HORIZONTAL_RULE',
40+
mutability: 'MUTABLE',
41+
data: {}
42+
};
43+
}
44+
})
45+
```
46+
47+
2448
**Take Care:** Plz not use version `1.2.0` it has build issues.

src/library/__tests__/mainTest.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ describe('htmlToDraft test suite', () => {
2424
contentBlocks = htmlToDraft('<p>test<a>link</a></p>');
2525
console.log('contentBlocks', contentBlocks);
2626

27+
contentBlocks = htmlToDraft('<hr/>', function(nodeName) {
28+
if (nodeName === 'hr') {
29+
return {
30+
type: 'HORIZONTAL_RULE',
31+
mutability: 'IMMUTABLE',
32+
data: {}
33+
};
34+
}
35+
});
36+
console.log('contentBlocks', contentBlocks);
37+
2738
assert.equal(true, true);
2839
});
2940
});

src/library/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function genFragment(
3030
depth: number,
3131
lastList: string,
3232
inEntity: number,
33-
customChunkGenerator: CustomChunkGenerator,
33+
customChunkGenerator: ?CustomChunkGenerator,
3434
): Object {
3535
const nodeName = node.nodeName.toLowerCase();
3636

@@ -39,7 +39,7 @@ function genFragment(
3939
if (value) {
4040
const entityId = Entity.__create(
4141
value.type,
42-
value.mutable,
42+
value.mutability,
4343
value.data || {},
4444
);
4545
return { chunk: getAtomicBlockChunk(entityId) };
@@ -137,7 +137,7 @@ function genFragment(
137137
return { chunk };
138138
}
139139

140-
function getChunkForHTML(html: string, customChunkGenerator: CustomChunkGenerator): Object {
140+
function getChunkForHTML(html: string, customChunkGenerator: ?CustomChunkGenerator): Object {
141141
const sanitizedHtml = html.trim().replace(REGEX_NBSP, SPACE);
142142
const safeBody = getSafeBodyFromHTML(sanitizedHtml);
143143
if (!safeBody) {
@@ -148,7 +148,7 @@ function getChunkForHTML(html: string, customChunkGenerator: CustomChunkGenerato
148148
return { chunk };
149149
}
150150

151-
export default function htmlToDraft(html: string, customChunkGenerator: CustomChunkGenerator): Object {
151+
export default function htmlToDraft(html: string, customChunkGenerator: ?CustomChunkGenerator): Object {
152152
const chunkData = getChunkForHTML(html, customChunkGenerator);
153153
if (chunkData) {
154154
const { chunk } = chunkData;

0 commit comments

Comments
 (0)