diff --git a/CliClient/tests/markdownUtils.js b/CliClient/tests/markdownUtils.js index 154a3a522..5dc1dc610 100644 --- a/CliClient/tests/markdownUtils.js +++ b/CliClient/tests/markdownUtils.js @@ -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); + } + })); + }); diff --git a/ReactNativeClient/lib/markdownUtils.js b/ReactNativeClient/lib/markdownUtils.js index 6ac4301b9..8f1110b7c 100644 --- a/ReactNativeClient/lib/markdownUtils.js +++ b/ReactNativeClient/lib/markdownUtils.js @@ -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'); diff --git a/ReactNativeClient/lib/models/BaseItem.js b/ReactNativeClient/lib/models/BaseItem.js index 5427c24fa..1f1941678 100644 --- a/ReactNativeClient/lib/models/BaseItem.js +++ b/ReactNativeClient/lib/models/BaseItem.js @@ -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('');