From f3efe657fa1627859f1b62f39dc99753999c376e Mon Sep 17 00:00:00 2001 From: Sam Saffron <sam.saffron@gmail.com> Date: Thu, 10 Mar 2016 18:25:02 +1100 Subject: [PATCH] FEATURE: Automatically add http:// when adding links without them via composer --- .../discourse/components/d-editor.js.es6 | 14 +++++++++++--- test/javascripts/components/d-editor-test.js.es6 | 10 ++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/discourse/components/d-editor.js.es6 b/app/assets/javascripts/discourse/components/d-editor.js.es6 index a0737079a..55a673d1b 100644 --- a/app/assets/javascripts/discourse/components/d-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/d-editor.js.es6 @@ -516,18 +516,26 @@ export default Ember.Component.extend({ const link = this.get('link'); const sel = this._lastSel; + const autoHttp = function(l){ + if (l.indexOf("://") === -1) { + return "http://" + l; + } else { + return l; + } + }; + if (Ember.isEmpty(link)) { return; } const m = / "([^"]+)"/.exec(link); if (m && m.length === 2) { const description = m[1]; const remaining = link.replace(m[0], ''); - this._addText(sel, `[${description}](${remaining})`); + this._addText(sel, `[${description}](${autoHttp(remaining)})`); } else { if (sel.value) { - this._addText(sel, `[${sel.value}](${link})`); + this._addText(sel, `[${sel.value}](${autoHttp(link)})`); } else { const desc = I18n.t('composer.link_description'); - this._addText(sel, `[${desc}](${link})`); + this._addText(sel, `[${desc}](${autoHttp(link)})`); this._selectText(sel.start + 1, desc.length); } } diff --git a/test/javascripts/components/d-editor-test.js.es6 b/test/javascripts/components/d-editor-test.js.es6 index d8985c19c..821b4d92f 100644 --- a/test/javascripts/components/d-editor-test.js.es6 +++ b/test/javascripts/components/d-editor-test.js.es6 @@ -207,6 +207,16 @@ testCase('link modal (simple link)', function(assert, textarea) { }); }); +testCase('link modal auto http addition', function(assert) { + click('button.link'); + fillIn('.insert-link input', 'sam.com'); + click('.insert-link button.btn-primary'); + const desc = I18n.t('composer.link_description'); + andThen(() => { + assert.equal(this.get('value'), `hello world.[${desc}](http://sam.com)`); + }); +}); + testCase('link modal (simple link) with selected text', function(assert, textarea) { textarea.selectionStart = 0; textarea.selectionEnd = 12;