Skip to content

Commit fd9582c

Browse files
benjiesstur
authored andcommitted
Escape both parentheses in links (sstur#183)
1 parent 188ac67 commit fd9582c

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

packages/draft-js-export-markdown/src/stateToMarkdown.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,9 @@ function encodeCode(text) {
283283

284284
// Encode chars that would normally be allowed in a URL but would conflict with
285285
// our markdown syntax: `[foo](http://foo/)`
286+
const LINK_CHARACTER_REPLACEMENTS = {'(': '%28', ')': '%29'};
286287
function encodeURL(url) {
287-
return url.replace(/\)/g, '%29');
288+
return url.replace(/[()]/g, (char) => LINK_CHARACTER_REPLACEMENTS[char]);
288289
}
289290

290291
// Escape quotes using backslash.

packages/draft-js-export-markdown/test/test-cases.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ Hello [World](/a).
3838
{"entityMap":{"0":{"type":"LINK","mutability":"MUTABLE","data":{"url":"/a","title":"f\"oo"}}},"blocks":[{"key":"2m141","text":"Hello World.","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[{"offset":6,"length":5,"key":0}]}]}
3939
Hello [World](/a "f\"oo").
4040

41+
>> Link with brackets
42+
{"entityMap":{"0":{"type":"LINK","mutability":"MUTABLE","data":{"url":"http://msdn.microsoft.com/en-us/library/aa752574(VS.85).aspx","foo":"x"}}},"blocks":[{"key":"f131g","text":"Hello World.","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[{"offset":6,"length":5,"key":0}]}]}
43+
Hello [World](http://msdn.microsoft.com/en-us/library/aa752574%28VS.85%29.aspx).
44+
4145
>> Ordered List
4246
{"entityMap":{},"blocks":[{"key":"33nh8","text":"An ordered list:","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[]},{"key":"8kinl","text":"One","type":"ordered-list-item","depth":0,"inlineStyleRanges":[],"entityRanges":[]},{"key":"ekll4","text":"Two","type":"ordered-list-item","depth":0,"inlineStyleRanges":[],"entityRanges":[]}]}
4347
An ordered list:

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,43 @@ describe('stateFromMarkdown', () => {
212212
],
213213
});
214214
});
215+
it('should correctly parse link containing escaped parenthesis', () => {
216+
let markdown = `[link1](http://msdn.microsoft.com/en-us/library/aa752574%28VS.85%29.aspx)`;
217+
let contentState = stateFromMarkdown(markdown);
218+
let rawContentState = convertToRaw(contentState);
219+
let blocks = removeKeys(rawContentState.blocks);
220+
expect({
221+
...rawContentState,
222+
blocks,
223+
}).toEqual({
224+
entityMap: {
225+
[0]: {
226+
type: 'LINK',
227+
mutability: 'MUTABLE',
228+
data: {
229+
url:
230+
'http://msdn.microsoft.com/en-us/library/aa752574%28VS.85%29.aspx',
231+
},
232+
},
233+
},
234+
blocks: [
235+
{
236+
text: 'link1',
237+
type: 'unstyled',
238+
depth: 0,
239+
inlineStyleRanges: [],
240+
entityRanges: [
241+
{
242+
offset: 0,
243+
length: 5,
244+
key: 0,
245+
},
246+
],
247+
data: {},
248+
},
249+
],
250+
});
251+
});
215252
});
216253

217254
function removeKeys(blocks) {

0 commit comments

Comments
 (0)