diff --git a/app/assets/javascripts/discourse/components/d-editor.js.es6 b/app/assets/javascripts/discourse/components/d-editor.js.es6 index cccc12c15a..4a893e9bf3 100644 --- a/app/assets/javascripts/discourse/components/d-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/d-editor.js.es6 @@ -404,7 +404,8 @@ export default Ember.Component.extend({ const remaining = link.replace(m[0], ''); this._addText(this._lastSel, `[${description}](${remaining})`); } else { - this._addText(this._lastSel, `[${link}](${link})`); + const selectedValue = this._lastSel.value || link; + this._addText(this._lastSel, `[${selectedValue}](${link})`); } this.set('link', ''); diff --git a/test/javascripts/components/d-editor-test.js.es6 b/test/javascripts/components/d-editor-test.js.es6 index c0ab017238..4d395f45aa 100644 --- a/test/javascripts/components/d-editor-test.js.es6 +++ b/test/javascripts/components/d-editor-test.js.es6 @@ -193,6 +193,19 @@ testCase('link modal (simple link)', function(assert) { }); }); +testCase('link modal (simple link) with selected text', function(assert, textarea) { + textarea.selectionStart = 0; + textarea.selectionEnd = 12; + + click('button.link'); + fillIn('.insert-link input', 'http://eviltrout.com'); + click('.insert-link button.btn-primary'); + andThen(() => { + assert.equal(this.$('.insert-link.hidden').length, 1); + assert.equal(this.get('value'), '[hello world.](http://eviltrout.com)'); + }); +}); + testCase('link modal (link with description)', function(assert) { click('button.link'); fillIn('.insert-link input', 'http://eviltrout.com "evil trout"');