diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index 01eca0114..b7bb8b435 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -82,6 +82,8 @@ const CodeEditor = createClass({ 'Cmd-M' : this.makeSpan, 'Ctrl-/' : this.makeComment, 'Cmd-/' : this.makeComment, + 'Ctrl-K' : this.makeLink, + 'Cmd-K' : this.makeLink, 'Ctrl-[' : this.foldAllCode, 'Cmd-[' : this.foldAllCode, 'Ctrl-]' : this.unfoldAllCode, @@ -153,6 +155,23 @@ const CodeEditor = createClass({ } }, + makeLink : function() { + const isLink = /^\[(.*)\]\((.*)\)$/; + const selection = this.codeMirror.getSelection().trim(); + let match; + if(match = isLink.exec(selection)){ + const altText = match[1]; + const url = match[2]; + this.codeMirror.replaceSelection(`${altText} ${url}`); + const cursor = this.codeMirror.getCursor(); + this.codeMirror.setSelection({ line: cursor.line, ch: cursor.ch - url.length }, { line: cursor.line, ch: cursor.ch }); + } else { + this.codeMirror.replaceSelection(`[${selection || 'alt text'}](url)`); + const cursor = this.codeMirror.getCursor(); + this.codeMirror.setSelection({ line: cursor.line, ch: cursor.ch - 4 }, { line: cursor.line, ch: cursor.ch - 1 }); + } + }, + foldAllCode : function() { this.codeMirror.execCommand('foldAll'); },