diff --git a/grammars/nvatom.cson b/grammars/nvatom.cson index 9f3ef18..de93f9d 100644 --- a/grammars/nvatom.cson +++ b/grammars/nvatom.cson @@ -14,7 +14,7 @@ 'include': 'source.gfm' } { - 'match': '(\\[\\[)([^\\[\\]\|]+)(\\]\\])' + 'match': '(? @subscriptions = new CompositeDisposable - @subscriptions.add atom.commands.add 'atom-workspace', 'nvatom:openInterlink': => @openInterlink() + @subscriptions.add atom.commands.add 'atom-workspace', 'nvatom:openInterlink': => NoteLink.openInterlink() destroy: -> @subscriptions.dispose() - openInterlink: -> + @openInterlink: -> editor = atom.workspace.getActiveTextEditor() return unless editor? return unless Utility.isNote(editor.getPath()) - noteTitle = @getInterlinkUnderCursor(editor) + noteTitle = NoteLink.getInterlinkUnderCursor(editor) return unless noteTitle? return unless noteTitle.length @@ -26,11 +26,13 @@ class NoteLink fs.writeFileSync(notePath, '') atom.workspace.open(notePath) - getInterlinkUnderCursor: (editor) -> + @getInterlinkUnderCursor: (editor) -> cursorPosition = editor.getCursorBufferPosition() token = editor.tokenForBufferPosition(cursorPosition) return unless token return unless token.value return unless token.scopes.indexOf('markup.underline.link.interlink.gfm') > -1 - return token.value + interlink = Utility.trim(token.value) + return unless interlink.length + return interlink diff --git a/lib/utility.coffee b/lib/utility.coffee index 34c7f01..e305680 100644 --- a/lib/utility.coffee +++ b/lib/utility.coffee @@ -10,6 +10,8 @@ class Utility @getPrimaryNoteExtension: -> if atom.config.get('nvatom.extensions').length then atom.config.get('nvatom.extensions')[0] else '.md' - @isNote: (filePath) -> filePath.startsWith(Utility.getNoteDirectory()) and path.extname(filePath) in atom.config.get('nvatom.extensions') + @isNote: (filePath) -> + filePath = fs.normalize(filePath) + return filePath.startsWith(Utility.getNoteDirectory()) and path.extname(filePath) in atom.config.get('nvatom.extensions') @trim: (str) -> str?.replace /^\s+|\s+$/g, '' diff --git a/package.json b/package.json index 7e222a4..ed36aea 100644 --- a/package.json +++ b/package.json @@ -38,5 +38,7 @@ "fs-plus": "2.x", "underscore-plus": "^1.6.6" }, - "devDependencies": {} + "devDependencies": { + "temp": "0.8.1" + } } diff --git a/spec/notelink-spec.coffee b/spec/notelink-spec.coffee new file mode 100644 index 0000000..7695dc6 --- /dev/null +++ b/spec/notelink-spec.coffee @@ -0,0 +1,94 @@ +fs = require 'fs-plus' +path = require 'path' +temp = require 'temp' +NoteLink = require '../lib/notelink' + +describe 'Notelink', -> + defaultDirectory = atom.config.get('nvatom.directory') + noteDirectory = null + + beforeEach -> + workspaceElement = atom.views.getView(atom.workspace) + activationPromise = atom.packages.activatePackage('nvatom') + noteDirectory = temp.mkdirSync() + + atom.config.set('nvatom.directory', noteDirectory) + + atom.commands.dispatch workspaceElement, 'nvatom:toggle' + waitsForPromise -> + activationPromise + + afterEach -> + atom.config.set('nvatom.directory', defaultDirectory) + + describe 'when getInerlinkUnderCursor is called', -> + editor = null + + beforeEach -> + waitsForPromise -> + atom.workspace.open(path.join(noteDirectory, 'Interlink.md')).then (o) -> editor = o + + it 'returns a trimmed interlink text', -> + testdata = [ + { position: [0, 2], text: '[[Car]]', expected: 'Car' }, + { position: [0, 2], text: '[[Notational Velocity]]', expected: 'Notational Velocity' }, + { position: [0, 2], text: '[[한글 Alphabet Test]]', expected: '한글 Alphabet Test' }, + { position: [0, 2], text: '[[ Car ]]', expected: 'Car' }, + { position: [0, 2], text: '[[Car/Mini]]', expected: 'Car/Mini' }, + ] + + for testitem in testdata + editor.setText testitem.text + editor.setCursorBufferPosition testitem.position + expect(NoteLink.getInterlinkUnderCursor(editor)).toBe testitem.expected + + it 'returns undefined for invalid text', -> + testdata = [ + { position: [0, 2], text: '[[]]' }, + { position: [0, 3], text: '[[]]' }, + { position: [0, 2], text: '[[ ]]' }, + { position: [0, 1], text: '[Car]' }, + { position: [0, 2], text: '[[[Car]]]' }, + { position: [0, 2], text: '[[Car]' }, + { position: [0, 2], text: '[[Car]]]' }, + { position: [0, 1], text: 'Car' }, + ] + + for testitem in testdata + editor.setText testitem.text + editor.setCursorBufferPosition testitem.position + expect(NoteLink.getInterlinkUnderCursor(editor)).toBe undefined + + describe 'when openInterlink is called', -> + describe 'when the editor path is under the note directory', -> + editor = null + + beforeEach -> + waitsForPromise -> + atom.workspace.open(path.join(noteDirectory, 'Interlink.md')).then (o) -> editor = o + + it 'opens the referred notes', -> + editor.setText '[[Car]]' + editor.setCursorBufferPosition [0, 2] + + editorPromise = NoteLink.openInterlink() + expect(editorPromise).not.toBe undefined + waitsForPromise -> + editorPromise + + runs -> + expect(atom.workspace.getActiveTextEditor().getPath().endsWith('Car.md')).toBe true + + describe 'when the editor path is not under the note directory', -> + editor = null + + beforeEach -> + waitsForPromise -> + atom.workspace.open(path.join(temp.mkdirSync(), 'Interlink.md')).then (o) -> editor = o + + it 'does nothing', -> + editor.setText '[[Car]]' + editor.setCursorBufferPosition [0, 2] + + editorPromise = NoteLink.openInterlink() + expect(editorPromise).toBe undefined