diff --git a/.gitignore b/.gitignore index 6afa438..bfc599d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ node_modules .DS_Store .snyk npm-debug.log -*.js* + diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e29576..0b9d6c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## atom-markdown-wrapper +### 5.0.0. +- Switch to `use babel` and ECMAScript 6 + ### 4.2.0 - Change **bold** shortcut to `cmd-b` (`ctrl-b`) diff --git a/lib/atom-markdown-wrapper.coffee b/lib/atom-markdown-wrapper.coffee deleted file mode 100644 index d5b9151..0000000 --- a/lib/atom-markdown-wrapper.coffee +++ /dev/null @@ -1,56 +0,0 @@ -mdwrap = require './mdwrap.coffee' -{Notification} = require 'atom' - -module.exports = - config: {} - - activate: -> - @mdwrap = new mdwrap() - - @command = atom.commands.add 'atom-text-editor', - 'atom-markdown-wrapper:paste', (event) => - editor = atom.workspace.getActiveTextEditor() - selection = editor.getSelectedText() - clipboard = atom.clipboard.read() - - try - @mdwrap.paste(editor, selection, clipboard) - catch - atom.notifications.addError('Not a valid URL', { - dismissable: true, - detail: clipboard, - icon: 'zap' - }) - - @command = atom.commands.add 'atom-text-editor', - 'atom-markdown-wrapper:image', (event) => - editor = atom.workspace.getActiveTextEditor() - clipboard = atom.clipboard.read() - selection = editor.getSelectedText() - - try - @mdwrap.image(editor, clipboard, selection) - catch - atom.notifications.addError('Not a valid image URL', { - dismissable: true, - detail: clipboard, - icon: 'zap' - }) - - @command = atom.commands.add 'atom-text-editor', - 'atom-markdown-wrapper:bold', (event) => - editor = atom.workspace.getActiveTextEditor() - selection = editor.getSelectedText() - - @mdwrap.bold(editor, selection) - - @command = atom.commands.add 'atom-text-editor', - 'atom-markdown-wrapper:italic', (event) => - editor = atom.workspace.getActiveTextEditor() - selection = editor.getSelectedText() - - @mdwrap.italic(editor, selection) - - deactivate: -> - @command.dispose() - @mdwrap.destroy() diff --git a/lib/atom-markdown-wrapper.js b/lib/atom-markdown-wrapper.js new file mode 100644 index 0000000..2b1059c --- /dev/null +++ b/lib/atom-markdown-wrapper.js @@ -0,0 +1,39 @@ +'use babel'; + +const mdwrap = require('./mdwrap'); +const {CompositeDisposable, Notification} = require('atom'); + +const cb = (fn) => { + let clipboard = atom.clipboard.read(); + let editor = atom.workspace.getActiveTextEditor(); + let selection = editor.getSelectedText(); + + try { + return this.mdwrap[fn](editor, selection, clipboard); + } catch (e) { + return atom.notifications.addError(e.message, { + dismissable: true, + detail: clipboard, + icon: 'zap' + }); + } +}; + +module.exports = { + config: {}, + activate: () => { + this.mdwrap = new mdwrap(); + + return this.command = atom.commands.add('atom-text-editor', { + 'atom-markdown-wrapper:paste': cb.bind(this, 'paste'), + 'atom-markdown-wrapper:image': cb.bind(this, 'image'), + 'atom-markdown-wrapper:bold': cb.bind(this, 'bold'), + 'atom-markdown-wrapper:italic': cb.bind(this, 'italic') + }); + }, + deactivate: () => { + this.command.dispose(); + + return this.mdwrap.destroy(); + } +}; diff --git a/lib/mdwrap.coffee b/lib/mdwrap.coffee deleted file mode 100644 index 5da7069..0000000 --- a/lib/mdwrap.coffee +++ /dev/null @@ -1,64 +0,0 @@ -{ CompositeDisposable } = require 'atom' -validUrl = require 'valid-url' - -module.exports = - class mdwrap - subscriptions: [] - - constructor: -> - @subscriptions = new CompositeDisposable - @subscriptions.add atom.commands.add 'atom-workspace', - 'atom-markdown-wrapper:paste': => @paste() - @subscriptions.add atom.commands.add 'atom-workspace', - 'atom-markdown-wrapper:image': => @image() - @subscriptions.add atom.commands.add 'atom-workspace', - 'atom-markdown-wrapper:bold': => @bold() - @subscriptions.add atom.commands.add 'atom-workspace', - 'atom-markdown-wrapper:italic': => @italic() - - destroy: -> - @subscriptions.dispose() - - paste: (editor, selection, clipboard) -> - if editor && selection && clipboard - insert = '[$selection]($href)' - .replace('$selection', selection) - .replace('$href', clipboard) - - if !(validUrl.isWebUri(clipboard) || clipboard.match(/^\#/)) - throw new Error('Not a valid URL or #anchor') - - editor.insertText(insert) - - return insert - - image: (editor, clipboard, selection) -> - if editor && clipboard - insert = '![$selection]($img)' - .replace('$selection', selection || '') - .replace('$img', clipboard) - - if !validUrl.isWebUri(clipboard) - throw new Error('Not a valid image URL') - - editor.insertText(insert) - - return insert - - bold: (editor, selection) -> - if editor && selection - insert = '**$selection**' - .replace('$selection', selection) - - editor.insertText(insert) - - return insert - - italic: (editor, selection) -> - if editor && selection - insert = '_$selection_' - .replace('$selection', selection) - - editor.insertText(insert) - - return insert diff --git a/lib/mdwrap.js b/lib/mdwrap.js new file mode 100644 index 0000000..503599e --- /dev/null +++ b/lib/mdwrap.js @@ -0,0 +1,87 @@ +'use babel'; + +const {CompositeDisposable} = require('atom'); +const validUrl = require('valid-url'); + +class mdwrap { + constructor() { + this.subscriptions = new CompositeDisposable; + + this.subscriptions.add( + atom.commands.add( + 'atom-workspace', { + 'atom-markdown-wrapper:paste': this.paste, + 'atom-markdown-wrapper:image': this.image, + 'atom-markdown-wrapper:bold': this.bold, + 'atom-markdown-wrapper:italic': this.italic + } + ) + ) + } + + destroy() { + this.subscriptions.dispose(); + } + + paste(editor, selection, clipboard) { + let insert; + + if (editor && selection && clipboard) { + insert = '[$selection]($href)' + .replace('$selection', selection) + .replace('$href', clipboard); + } + + if (!validUrl.isWebUri(clipboard) && !clipboard.match(/^\#/)) { + throw new Error('Not a valid URL or #anchor'); + } + + editor.insertText(insert); + + return insert; + } + + image(editor, selection, clipboard) { + let insert; + + if (editor && clipboard) { + insert = '![$selection]($href)' + .replace('$selection', selection || '') + .replace('$href', clipboard); + } + + if (!validUrl.isWebUri(clipboard)) { + throw new Error('Not a valid image URL'); + } + + editor.insertText(insert); + + return insert; + } + + bold(editor, selection) { + let insert; + + if (editor && selection) { + insert = '**$selection**' + .replace('$selection', selection); + } + editor.insertText(insert); + + return insert; + } + + italic(editor, selection) { + let insert; + + if (editor && selection) { + insert = '_$selection_' + .replace('$selection', selection); + } + editor.insertText(insert); + + return insert; + } +} + +module.exports = mdwrap; diff --git a/package.json b/package.json index 302ae1a..92142d4 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,6 @@ "valid-url": "^1.0.9" }, "devDependencies": { - "snyk": "^1.16.0" + "snyk": "^1.21.2" } } diff --git a/spec/atom-markdown-wrapper-spec.coffee b/spec/atom-markdown-wrapper-spec.coffee deleted file mode 100644 index f23d513..0000000 --- a/spec/atom-markdown-wrapper-spec.coffee +++ /dev/null @@ -1,134 +0,0 @@ -Atommdwrap = require '../lib/atom-markdown-wrapper' -mdwrap = require '../lib/mdwrap' - -describe 'Atom Markdown Wrapper', -> - [editor, sel, txt, anchor, img] = [] - - beforeEach -> - waitsForPromise -> - atom.packages.activatePackage 'language-gfm' - - waitsForPromise -> - atom.workspace.open 'sample.md' - - @mdwrap = new mdwrap() - - afterEach -> - atom.reset() - - @mdwrap.destroy() - - it 'should load correctly', -> - expect(Atommdwrap).toBeDefined() - - describe '@mdwrap', -> - beforeEach -> - editor = atom.workspace.getActiveTextEditor() - sel = 'selection' - txt = 'https://example.com' - anchor = '#example' - img = 'https://example.com/image.png' - - afterEach -> - editor = null - sel = null - txt = null - anchor = null - img = null - - it 'should be defined', -> - expect(@mdwrap).toBeDefined() - - describe '.paste()', -> - it 'should be defined', -> - expect(@mdwrap.paste).toBeDefined() - - it 'should require three parameters', -> - spyOn(@mdwrap, 'paste') - - @mdwrap.paste(1, 2, 3) - expect(@mdwrap.paste).toHaveBeenCalledWith(1, 2, 3) - - it 'should only paste valid links', -> - expect(-> new mdwrap().paste(editor, sel, 'foobar')).toThrow('Not a valid URL or #anchor') - - it 'should replace `selection` with [selection](https://example.com) for web links', -> - spyOn(@mdwrap, 'paste').andCallThrough() - - res = @mdwrap.paste(editor, sel, txt) - expect(@mdwrap.paste).toHaveBeenCalledWith(editor, sel, txt) - expect(res).toBe '[selection](https://example.com)' - - it 'should replace `selection` with [selection](#example) for anchor links', -> - spyOn(@mdwrap, 'paste').andCallThrough() - - res = @mdwrap.paste(editor, sel, anchor) - expect(@mdwrap.paste).toHaveBeenCalledWith(editor, sel, anchor) - expect(res).toBe '[selection](#example)' - - describe '.image()', -> - it 'should be defined', -> - expect(@mdwrap.image).toBeDefined() - - it 'should require 2 parameters', -> - spyOn(@mdwrap, 'image') - - @mdwrap.image(1, 2) - expect(@mdwrap.image).toHaveBeenCalledWith(1, 2) - - it 'should accept 3 parameters', -> - spyOn(@mdwrap, 'image') - - @mdwrap.image(1, 2, 3) - expect(@mdwrap.image).toHaveBeenCalledWith(1, 2, 3) - - it 'should only paste valid URLs', -> - expect(-> new mdwrap().image(editor, 'image')).toThrow('Not a valid image URL') - - it 'should insert ![](https://example.com/image.png)', -> - spyOn(@mdwrap, 'image').andCallThrough() - - res = @mdwrap.image(editor, img) - expect(@mdwrap.image).toHaveBeenCalledWith(editor, img) - expect(res).toBe '![](https://example.com/image.png)' - - it 'should replace `selection` with ![selection](https://example.com/image.png)', -> - spyOn(@mdwrap, 'image').andCallThrough() - - res = @mdwrap.image(editor, img, sel) - expect(@mdwrap.image).toHaveBeenCalledWith(editor, img, sel) - expect(res).toBe '![selection](https://example.com/image.png)' - - describe '.bold()', -> - it 'should be defined', -> - expect(@mdwrap.bold).toBeDefined() - - it 'should require 2 parameters', -> - spyOn(@mdwrap, 'bold') - - @mdwrap.bold(1, 2) - expect(@mdwrap.bold).toHaveBeenCalledWith(1, 2) - - it 'should insert **selection**', -> - spyOn(@mdwrap, 'bold').andCallThrough() - - res = @mdwrap.bold(editor, sel) - expect(@mdwrap.bold).toHaveBeenCalledWith(editor, sel) - expect(res).toBe '**selection**' - - describe '.italic()', -> - it 'should be defined', -> - expect(@mdwrap.italic).toBeDefined() - - it 'should require 2 parameters', -> - spyOn(@mdwrap, 'italic') - - @mdwrap.italic(1, 2) - expect(@mdwrap.italic).toHaveBeenCalledWith(1, 2) - - it 'should insert _selection_', -> - spyOn(@mdwrap, 'italic').andCallThrough() - - res = @mdwrap.italic(editor, sel) - expect(@mdwrap.italic).toHaveBeenCalledWith(editor, sel) - expect(res).toBe '_selection_' diff --git a/spec/atom-markdown-wrapper-spec.js b/spec/atom-markdown-wrapper-spec.js new file mode 100644 index 0000000..3693839 --- /dev/null +++ b/spec/atom-markdown-wrapper-spec.js @@ -0,0 +1,172 @@ +'use babel'; + +const Atommdwrap = require('../lib/atom-markdown-wrapper'); +const mdwrap = require('../lib/mdwrap'); + +describe('Atom Markdown Wrapper', () => { + let [editor, sel, txt, anchor, img, res] = []; + + beforeEach(() => { + waitsForPromise(() => { + return atom.workspace.open('sample.md'); + }); + + waitsForPromise(() => { + return atom.packages.activatePackage('language-gfm'); + }); + + return this.mdwrap = new mdwrap(); + }); + + afterEach(() => { + atom.reset(); + + return this.mdwrap.destroy(); + }); + + it('should load correctly', () => { + expect(Atommdwrap).toBeDefined(); + }); + + describe('mdwrap', () => { + + beforeEach(() => { + editor = atom.workspace.getActiveTextEditor(); + sel = 'selection'; + txt = 'https://example.com'; + anchor = '#example'; + img = 'https://example.com/image.png'; + }); + + afterEach(() => { + editor = null; + sel = null; + txt = null; + anchor = null; + img = null; + }); + + it('should be defined', () => { + expect(this.mdwrap).toBeDefined(); + }); + + describe('.paste()', () => { + + it('should be defined', () => { + expect(this.mdwrap.paste).toBeDefined(); + }); + + it('should require three parameters', () => { + spyOn(this.mdwrap, 'paste'); + + this.mdwrap.paste(1, 2, 3); + expect(this.mdwrap.paste).toHaveBeenCalledWith(1, 2, 3); + }); + + it('should only paste valid links', () => { + expect(() => new mdwrap().paste(editor, sel, 'foobar')).toThrow('Not a valid URL or #anchor'); + }); + + it('should replace `selection` with [selection](https://example.com) for web links', () => { + spyOn(this.mdwrap, 'paste').andCallThrough(); + + res = this.mdwrap.paste(editor, sel, txt); + expect(this.mdwrap.paste).toHaveBeenCalledWith(editor, sel, txt); + expect(res).toBe('[selection](https://example.com)'); + }); + + it('should replace `selection` with [selection](#example) for anchor links', () => { + spyOn(this.mdwrap, 'paste').andCallThrough() + + res = this.mdwrap.paste(editor, sel, anchor) + expect(this.mdwrap.paste).toHaveBeenCalledWith(editor, sel, anchor) + expect(res).toBe('[selection](#example)'); + }); + + }); + + describe('.image()', () => { + + it('should be defined', () => { + expect(this.mdwrap.image).toBeDefined(); + }); + + it('should require two parameters', () => { + spyOn(this.mdwrap, 'image'); + + this.mdwrap.image(1, 2); + expect(this.mdwrap.image).toHaveBeenCalledWith(1, 2); + }); + + it('should accept three parameters', () => { + spyOn(this.mdwrap, 'image'); + + this.mdwrap.image(1, 2, 3); + expect(this.mdwrap.image).toHaveBeenCalledWith(1, 2, 3); + }); + + it('should only paste valid URLs', () => { + expect(() => new mdwrap().image(editor, sel, 'image')).toThrow('Not a valid image URL'); + }); + + it('should insert ![](https://example.com/image.png)', () => { + spyOn(this.mdwrap, 'image').andCallThrough(); + + res = this.mdwrap.image(editor, undefined, img); + expect(this.mdwrap.image).toHaveBeenCalledWith(editor, undefined, img); + expect(res).toBe('![](https://example.com/image.png)'); + }); + + it('should replace `selection` with ![selection](https://example.com/image.png)', () => { + spyOn(this.mdwrap, 'image').andCallThrough(); + + res = this.mdwrap.image(editor, sel, img); + expect(this.mdwrap.image).toHaveBeenCalledWith(editor, sel, img); + expect(res).toBe('![selection](https://example.com/image.png)'); + }); + + }); + + describe('.bold()', () => { + it('should be defined', () => { + expect(this.mdwrap.bold).toBeDefined(); + }); + + it('should require 2 parameters', () => { + spyOn(this.mdwrap, 'bold'); + + this.mdwrap.bold(1, 2); + expect(this.mdwrap.bold).toHaveBeenCalledWith(1, 2); + }); + + it('should insert **selection**', () => { + spyOn(this.mdwrap, 'bold').andCallThrough(); + + res = this.mdwrap.bold(editor, sel); + expect(this.mdwrap.bold).toHaveBeenCalledWith(editor, sel); + expect(res).toBe('**selection**'); + }); + }); + + describe('.italic()', () => { + it('should be defined', () => { + expect(this.mdwrap.italic).toBeDefined(); + }); + + it('should require 2 parameters', () => { + spyOn(this.mdwrap, 'italic'); + + this.mdwrap.italic(1, 2); + expect(this.mdwrap.italic).toHaveBeenCalledWith(1, 2); + }); + + it('should insert _selection_', () => { + spyOn(this.mdwrap, 'italic').andCallThrough(); + + res = this.mdwrap.italic(editor, sel); + expect(this.mdwrap.italic).toHaveBeenCalledWith(editor, sel); + expect(res).toBe('_selection_'); + }); + }); + }); +});