Skip to content

Commit

Permalink
Fix duplicate links in markdown import (#161)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mxstbr authored and sstur committed Jan 29, 2019
1 parent cdca69b commit 716b505
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/draft-js-import-markdown/src/MarkdownParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ var inline = {
};

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

inline.link = replace(inline.link)('inside', inline._inside)(
'href',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ describe('stateFromMarkdown', () => {
});
it('should correctly handle linebreaks option', () => {
let markdown = 'Hello\nWorld';
let contentState = stateFromMarkdown(markdown, {parserOptions: {breaks: true}});
let contentState = stateFromMarkdown(markdown, {
parserOptions: {breaks: true},
});
let rawContentState = convertToRaw(contentState);
let blocks = removeKeys(rawContentState.blocks);
expect(blocks).toEqual([
Expand All @@ -60,7 +62,8 @@ describe('stateFromMarkdown', () => {
]);
});
it('should correctly handle images with complex srcs', () => {
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';
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';
let markdown = `![](${src})`;
let contentState = stateFromMarkdown(markdown);
let rawContentState = convertToRaw(contentState);
Expand All @@ -80,18 +83,73 @@ describe('stateFromMarkdown', () => {
},
},
},
blocks: [{
text: ' ',
type: 'unstyled',
depth: 0,
inlineStyleRanges: [],
entityRanges: [{
offset: 0,
length: 1,
key: 0,
}],
data: {},
}],
blocks: [
{
text: ' ',
type: 'unstyled',
depth: 0,
inlineStyleRanges: [],
entityRanges: [
{
offset: 0,
length: 1,
key: 0,
},
],
data: {},
},
],
});
});
it('should correctly links', () => {
let markdown = `[link1](https://google.com) [link2](https://google.com)`;
let contentState = stateFromMarkdown(markdown);
let rawContentState = convertToRaw(contentState);
let blocks = removeKeys(rawContentState.blocks);
expect({
...rawContentState,
blocks,
}).toEqual({
entityMap: {
// This is necessary due to flow not supporting non-string literal property keys
// eslint-disable-next-line quote-props
'0': {
type: 'LINK',
mutability: 'MUTABLE',
data: {
url: 'https://google.com',
},
},
// eslint-disable-next-line quote-props
'1': {
type: 'LINK',
mutability: 'MUTABLE',
data: {
url: 'https://google.com',
},
},
},
blocks: [
{
text: 'link1 link2',
type: 'unstyled',
depth: 0,
inlineStyleRanges: [],
entityRanges: [
{
offset: 0,
length: 5,
key: 0,
},
{
offset: 6,
length: 5,
key: 1,
},
],
data: {},
},
],
});
});
});
Expand Down

0 comments on commit 716b505

Please sign in to comment.