Skip to content

Commit 3f02b0d

Browse files
author
Kadi Kraman
authored
Merge pull request #51 from benmccormick/bugfix/count-subchild-length
Don't ignore lengths of subchilds when calculating entity lengths
2 parents 8dc9c09 + 2d6787f commit 3f02b0d

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/mdToDraftjs.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,14 @@ const parseMdLine = (line, existingEntities, extraStyles = {}) => {
9090
};
9191

9292
const getRawLength = children =>
93-
children.reduce((prev, current) => prev + (current.value ? current.value.length : 0), 0);
93+
children.reduce((prev, current) => {
94+
if (current.value) {
95+
return prev + current.value.length;
96+
} else if (current.children && current.children.length) {
97+
return prev + getRawLength(current.children);
98+
}
99+
return prev;
100+
}, 0);
94101

95102
const addLink = child => {
96103
const entityKey = Object.keys(entityMap).length;

test/mdToDraftjs.test.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ describe('mdToDraftjs', () => {
179179
mdToDraftjs(markdown).should.deep.equal(expectedDraftjs);
180180
});
181181

182-
it('converts two styles applied to the a link correctly', () => {
182+
it('converts two styles applied outside a link correctly', () => {
183183
const markdown = '__*[label](http://example.com/here)*__';
184184
const expectedDraftjs = {
185185
blocks: [
@@ -196,7 +196,7 @@ describe('mdToDraftjs', () => {
196196
],
197197
inlineStyleRanges: [
198198
{
199-
length: 0,
199+
length: 5,
200200
offset: 0,
201201
style: 'BOLD'
202202
},
@@ -221,6 +221,43 @@ describe('mdToDraftjs', () => {
221221
resultDraftJs.should.deep.equal(expectedDraftjs);
222222
});
223223

224+
it('converts a style applied inside a link correctly', () => {
225+
const markdown = '[la**b**el](http://example.com/here)';
226+
const expectedDraftjs = {
227+
blocks: [
228+
{
229+
text: 'label',
230+
type: 'unstyled',
231+
depth: 0,
232+
entityRanges: [
233+
{
234+
key: 0,
235+
length: 5,
236+
offset: 0
237+
}
238+
],
239+
inlineStyleRanges: [
240+
{
241+
length: 1,
242+
offset: 2,
243+
style: 'BOLD'
244+
}
245+
]
246+
}
247+
],
248+
entityMap: {
249+
0: {
250+
type: 'LINK',
251+
mutability: 'MUTABLE',
252+
data: { url: 'http://example.com/here' }
253+
}
254+
}
255+
};
256+
257+
const resultDraftJs = mdToDraftjs(markdown);
258+
resultDraftJs.should.deep.equal(expectedDraftjs);
259+
});
260+
224261
it('converts several paragraphs to markdown correctly', () => {
225262
const markdown =
226263
'*First __content__* block.\n*Second __content__* block.\n*Third __content__* block.';

0 commit comments

Comments
 (0)