Skip to content

Commit

Permalink
Desktop, Cli: Fixes #2085: Fix escaping of title when generating a ma…
Browse files Browse the repository at this point in the history
…rkdown link (#2456)

Previously a title with brackets was escaped incorrectly. The brackets were replaced by underscores.

The following title `title [square] (round)` looked like this:

[title _square_ _round_](:/c54794f53e5e4b1aa558699e255d5f95)

Now it looks like this:

[title \[square\] (round)](:/c54794f53e5e4b1aa558699e255d5f95)

fixes #2085
  • Loading branch information
tessus authored Feb 7, 2020
1 parent 8cbb0d0 commit 3f23d8e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
15 changes: 15 additions & 0 deletions CliClient/tests/markdownUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,19 @@ describe('markdownUtils', function() {
}
}));

it('escape a markdown link (title)', asyncTest(async () => {

const testCases = [
['Helmut K. C. Tessarek', 'Helmut K. C. Tessarek'],
['Helmut (K. C.) Tessarek', 'Helmut (K. C.) Tessarek'],
['Helmut [K. C.] Tessarek', 'Helmut \\[K. C.\\] Tessarek'],
];

for (let i = 0; i < testCases.length; i++) {
const md = testCases[i][0];
const expected = testCases[i][1];
expect(markdownUtils.escapeTitleText(md)).toBe(expected);
}
}));

});
5 changes: 5 additions & 0 deletions ReactNativeClient/lib/markdownUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const markdownUtils = {
return text.replace(/(\[|\]|\(|\))/g, '_');
},

// Titles for markdown links only need escaping for [ and ]
escapeTitleText(text) {
return text.replace(/(\[|\])/g, '\\$1');
},

escapeLinkUrl(url) {
url = url.replace(/\(/g, '%28');
url = url.replace(/\)/g, '%29');
Expand Down
2 changes: 1 addition & 1 deletion ReactNativeClient/lib/models/BaseItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ class BaseItem extends BaseModel {

const output = [];
output.push('[');
output.push(markdownUtils.escapeLinkText(item.title));
output.push(markdownUtils.escapeTitleText(item.title));
output.push(']');
output.push(`(:/${item.id})`);
return output.join('');
Expand Down

0 comments on commit 3f23d8e

Please sign in to comment.