Skip to content

Commit 716b505

Browse files
mxstbrsstur
authored andcommitted
Fix duplicate links in markdown import (sstur#161)
* Add failing test for duplicate link `draft-js-import-markdown` does not correctly handle the case where two links to the same src are in a line. * Fix link parsing in markdown This updates the _href regex to the new version from the marked codebase to fix the bug with two links in a line: https://github.com/markedjs/marked/blob/c77a2244ab5d4a0e1dec0979a51b80df87ec6409/lib/marked.js#L574 * Fix lint warnings
1 parent cdca69b commit 716b505

File tree

2 files changed

+73
-15
lines changed

2 files changed

+73
-15
lines changed

packages/draft-js-import-markdown/src/MarkdownParser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ var inline = {
384384
};
385385

386386
inline._inside = /(?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/;
387-
inline._href = /\s*<?([\s\S]*)>?(?:\s+['"]([\s\S]*?)['"])?\s*/;
387+
inline._href = /\s*(<(?:\\[<>]?|[^\s<>\\])*>|(?:\\[()]?|\([^\s\x00-\x1f\\]*\)|[^\s\x00-\x1f()\\])*?)/;
388388

389389
inline.link = replace(inline.link)('inside', inline._inside)(
390390
'href',

packages/draft-js-import-markdown/src/__tests__/stateFromMarkdown-test.js

+72-14
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ describe('stateFromMarkdown', () => {
4545
});
4646
it('should correctly handle linebreaks option', () => {
4747
let markdown = 'Hello\nWorld';
48-
let contentState = stateFromMarkdown(markdown, {parserOptions: {breaks: true}});
48+
let contentState = stateFromMarkdown(markdown, {
49+
parserOptions: {breaks: true},
50+
});
4951
let rawContentState = convertToRaw(contentState);
5052
let blocks = removeKeys(rawContentState.blocks);
5153
expect(blocks).toEqual([
@@ -60,7 +62,8 @@ describe('stateFromMarkdown', () => {
6062
]);
6163
});
6264
it('should correctly handle images with complex srcs', () => {
63-
const src = 'https://spectrum.imgix.net/threads/c678032e-68a4-4e14-956d-abfa444a707d/Captura%20de%20pantalla%202017-08-19%20a%20la(s)%2000.14.09.png.0.29802431313299893';
65+
const src =
66+
'https://spectrum.imgix.net/threads/c678032e-68a4-4e14-956d-abfa444a707d/Captura%20de%20pantalla%202017-08-19%20a%20la(s)%2000.14.09.png.0.29802431313299893';
6467
let markdown = `![](${src})`;
6568
let contentState = stateFromMarkdown(markdown);
6669
let rawContentState = convertToRaw(contentState);
@@ -80,18 +83,73 @@ describe('stateFromMarkdown', () => {
8083
},
8184
},
8285
},
83-
blocks: [{
84-
text: ' ',
85-
type: 'unstyled',
86-
depth: 0,
87-
inlineStyleRanges: [],
88-
entityRanges: [{
89-
offset: 0,
90-
length: 1,
91-
key: 0,
92-
}],
93-
data: {},
94-
}],
86+
blocks: [
87+
{
88+
text: ' ',
89+
type: 'unstyled',
90+
depth: 0,
91+
inlineStyleRanges: [],
92+
entityRanges: [
93+
{
94+
offset: 0,
95+
length: 1,
96+
key: 0,
97+
},
98+
],
99+
data: {},
100+
},
101+
],
102+
});
103+
});
104+
it('should correctly links', () => {
105+
let markdown = `[link1](https://google.com) [link2](https://google.com)`;
106+
let contentState = stateFromMarkdown(markdown);
107+
let rawContentState = convertToRaw(contentState);
108+
let blocks = removeKeys(rawContentState.blocks);
109+
expect({
110+
...rawContentState,
111+
blocks,
112+
}).toEqual({
113+
entityMap: {
114+
// This is necessary due to flow not supporting non-string literal property keys
115+
// eslint-disable-next-line quote-props
116+
'0': {
117+
type: 'LINK',
118+
mutability: 'MUTABLE',
119+
data: {
120+
url: 'https://google.com',
121+
},
122+
},
123+
// eslint-disable-next-line quote-props
124+
'1': {
125+
type: 'LINK',
126+
mutability: 'MUTABLE',
127+
data: {
128+
url: 'https://google.com',
129+
},
130+
},
131+
},
132+
blocks: [
133+
{
134+
text: 'link1 link2',
135+
type: 'unstyled',
136+
depth: 0,
137+
inlineStyleRanges: [],
138+
entityRanges: [
139+
{
140+
offset: 0,
141+
length: 5,
142+
key: 0,
143+
},
144+
{
145+
offset: 6,
146+
length: 5,
147+
key: 1,
148+
},
149+
],
150+
data: {},
151+
},
152+
],
95153
});
96154
});
97155
});

0 commit comments

Comments
 (0)