Commit 33665fa5 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch 'markdown-autocomplete-escaping' into 'master'

Only use backslash escapes in autocomplete when needed

Closes #45348

See merge request gitlab-org/gitlab-ce!27457
parents 28e42f1c 67b16f7e
...@@ -461,7 +461,10 @@ class GfmAutoComplete { ...@@ -461,7 +461,10 @@ class GfmAutoComplete {
// We can ignore this for quick actions because they are processed // We can ignore this for quick actions because they are processed
// before Markdown. // before Markdown.
if (!this.setting.skipMarkdownCharacterTest) { if (!this.setting.skipMarkdownCharacterTest) {
withoutAt = withoutAt.replace(/([~\-_*`])/g, '\\$&'); withoutAt = withoutAt
.replace(/(~~|`|\*)/g, '\\$1')
.replace(/(\b)(_+)/g, '$1\\$2') // only escape underscores at the start
.replace(/(_+)(\b)/g, '\\$1$2'); // or end of words
} }
return `${at}${withoutAt}`; return `${at}${withoutAt}`;
......
---
title: Only escape Markdown emphasis characters in autocomplete when necessary
merge_request: 27457
author:
type: changed
...@@ -94,7 +94,7 @@ describe('GfmAutoComplete', () => { ...@@ -94,7 +94,7 @@ describe('GfmAutoComplete', () => {
}); });
it('should quote if value contains any non-alphanumeric characters', () => { it('should quote if value contains any non-alphanumeric characters', () => {
expect(beforeInsert(atwhoInstance, '~label-20')).toBe('~"label\\-20"'); expect(beforeInsert(atwhoInstance, '~label-20')).toBe('~"label-20"');
expect(beforeInsert(atwhoInstance, '~label 20')).toBe('~"label 20"'); expect(beforeInsert(atwhoInstance, '~label 20')).toBe('~"label 20"');
}); });
...@@ -102,12 +102,21 @@ describe('GfmAutoComplete', () => { ...@@ -102,12 +102,21 @@ describe('GfmAutoComplete', () => {
expect(beforeInsert(atwhoInstance, '~1234')).toBe('~"1234"'); expect(beforeInsert(atwhoInstance, '~1234')).toBe('~"1234"');
}); });
it('should escape Markdown emphasis characters, except in the first character', () => { it('escapes Markdown strikethroughs when needed', () => {
expect(beforeInsert(atwhoInstance, '@_group')).toEqual('@\\_group'); expect(beforeInsert(atwhoInstance, '~a~bug')).toEqual('~"a~bug"');
expect(beforeInsert(atwhoInstance, '~_bug')).toEqual('~\\_bug'); expect(beforeInsert(atwhoInstance, '~a~~bug~~')).toEqual('~"a\\~~bug\\~~"');
});
it('escapes Markdown emphasis when needed', () => {
expect(beforeInsert(atwhoInstance, '~a_bug_')).toEqual('~a_bug\\_');
expect(beforeInsert(atwhoInstance, '~a _bug_')).toEqual('~"a \\_bug\\_"');
expect(beforeInsert(atwhoInstance, '~a*bug*')).toEqual('~"a\\*bug\\*"');
expect(beforeInsert(atwhoInstance, '~a *bug*')).toEqual('~"a \\*bug\\*"');
});
it('escapes Markdown code spans when needed', () => {
expect(beforeInsert(atwhoInstance, '~a`bug`')).toEqual('~"a\\`bug\\`"');
expect(beforeInsert(atwhoInstance, '~a `bug`')).toEqual('~"a \\`bug\\`"'); expect(beforeInsert(atwhoInstance, '~a `bug`')).toEqual('~"a \\`bug\\`"');
expect(beforeInsert(atwhoInstance, '~a ~bug')).toEqual('~"a \\~bug"');
expect(beforeInsert(atwhoInstance, '~a **bug')).toEqual('~"a \\*\\*bug"');
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment