Skip to content

Commit ac992bf

Browse files
authored
Merge branch 'master' into master
2 parents 759dc78 + f27e690 commit ac992bf

File tree

8 files changed

+88
-9
lines changed

8 files changed

+88
-9
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ npm-debug.log
1313

1414
#IntelliJ
1515
.idea
16+
17+
stats.json

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.

config/webpack.build.js renamed to config/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = {
1515
},
1616
externals: {
1717
'draft-js': 'draft-js',
18+
immutable: 'immutable',
1819
},
1920
plugins: [
2021
new webpack.DefinePlugin({

dist/html-to-draftjs.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-to-draftjs",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"main": "dist/html-to-draftjs.js",
55
"repository": {
66
"type": "git",
@@ -21,14 +21,15 @@
2121
"webpack": "^3.9.1"
2222
},
2323
"peerDependencies": {
24+
"immutable": "3.x.x || 4.x.x",
2425
"draft-js": "^0.10.x"
2526
},
2627
"scripts": {
2728
"start": "react-scripts start",
2829
"test": "react-scripts test --env=jsdom",
2930
"eject": "react-scripts eject",
3031
"clean": "rimraf dist",
31-
"build": "npm run clean && webpack --config config/webpack.build.js"
32+
"build": "npm run clean && webpack --config config/webpack.config.js"
3233
},
3334
"author": "Jyoti Puri",
3435
"license": "MIT"

report.html

Lines changed: 25 additions & 0 deletions
Large diffs are not rendered by default.

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: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,30 @@ const REGEX_NBSP = new RegExp('&nbsp;', 'g');
2222

2323
let firstBlock = true;
2424

25+
type CustomChunkGenerator = (nodeName: string, node: HTMLElement) => ?{type: string, mutability: string, data: {}};
26+
2527
function genFragment(
2628
node: Object,
2729
inlineStyle: OrderedSet,
2830
depth: number,
2931
lastList: string,
30-
inEntity: number
32+
inEntity: number,
33+
customChunkGenerator: ?CustomChunkGenerator,
3134
): Object {
3235
const nodeName = node.nodeName.toLowerCase();
3336

37+
if (customChunkGenerator) {
38+
const value = customChunkGenerator(nodeName, node);
39+
if (value) {
40+
const entityId = Entity.__create(
41+
value.type,
42+
value.mutability,
43+
value.data || {},
44+
);
45+
return { chunk: getAtomicBlockChunk(entityId) };
46+
}
47+
}
48+
3449
if (nodeName === '#text' && node.textContent !== '\n') {
3550
return createTextChunk(node, inlineStyle, inEntity);
3651
}
@@ -134,27 +149,27 @@ function genFragment(
134149
let child = node.firstChild;
135150
while (child) {
136151
const entityId = getEntityId(child);
137-
const { chunk: generatedChunk } = genFragment(child, inlineStyle, depth, lastList, (entityId || inEntity));
152+
const { chunk: generatedChunk } = genFragment(child, inlineStyle, depth, lastList, (entityId || inEntity), customChunkGenerator);
138153
chunk = joinChunks(chunk, generatedChunk);
139154
const sibling = child.nextSibling;
140155
child = sibling;
141156
}
142157
return { chunk };
143158
}
144159

145-
function getChunkForHTML(html: string): Object {
160+
function getChunkForHTML(html: string, customChunkGenerator: ?CustomChunkGenerator): Object {
146161
const sanitizedHtml = html.trim().replace(REGEX_NBSP, SPACE);
147162
const safeBody = getSafeBodyFromHTML(sanitizedHtml);
148163
if (!safeBody) {
149164
return null;
150165
}
151166
firstBlock = true;
152-
const { chunk } = genFragment(safeBody, new OrderedSet(), -1, '', undefined);
167+
const { chunk } = genFragment(safeBody, new OrderedSet(), -1, '', undefined, customChunkGenerator);
153168
return { chunk };
154169
}
155170

156-
export default function htmlToDraft(html: string): Object {
157-
const chunkData = getChunkForHTML(html);
171+
export default function htmlToDraft(html: string, customChunkGenerator: ?CustomChunkGenerator): Object {
172+
const chunkData = getChunkForHTML(html, customChunkGenerator);
158173
if (chunkData) {
159174
const { chunk } = chunkData;
160175
let entityMap = new OrderedMap({});

0 commit comments

Comments
 (0)