From 9c5c4e43da980deebda1d314e900a6537d785ef6 Mon Sep 17 00:00:00 2001 From: Seongjae Lee Date: Sat, 26 Sep 2015 00:16:36 -0700 Subject: [PATCH] Add interlink highlight and jump features --- grammars/nvatom.cson | 27 ++++++++++++++++++++++ keymaps/notational-velocity.cson | 2 ++ lib/notational-velocity-view.coffee | 8 +++---- lib/notational-velocity.coffee | 10 ++++---- lib/notelink.coffee | 36 +++++++++++++++++++++++++++++ lib/utility.coffee | 15 ++++++++++++ package.json | 12 +++++++++- 7 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 grammars/nvatom.cson create mode 100644 lib/notelink.coffee create mode 100644 lib/utility.coffee diff --git a/grammars/nvatom.cson b/grammars/nvatom.cson new file mode 100644 index 0000000..9f3ef18 --- /dev/null +++ b/grammars/nvatom.cson @@ -0,0 +1,27 @@ +'name': 'nvAtom Markdown' +'scopeName': 'source.gfm' +'fileTypes': [ + 'markdown' + 'md' + 'mdown' + 'mkd' + 'mkdown' + 'rmd' + 'ron' +] +'patterns': [ + { + 'include': 'source.gfm' + } + { + 'match': '(\\[\\[)([^\\[\\]\|]+)(\\]\\])' + 'name': 'interlink' + 'captures': + '1': + 'name': 'punctuation.definition.begin.gfm' + '2': + 'name': 'markup.underline.link.interlink.gfm' + '3': + 'name': 'punctuation.definition.begin.gfm' + } +] diff --git a/keymaps/notational-velocity.cson b/keymaps/notational-velocity.cson index 19c1d36..12c93fd 100644 --- a/keymaps/notational-velocity.cson +++ b/keymaps/notational-velocity.cson @@ -9,3 +9,5 @@ # https://atom.io/docs/latest/advanced/keymaps 'atom-workspace': 'alt-cmd-l': 'nvatom:toggle' +'atom-workspace atom-text-editor': + 'alt-cmd-o': 'nvatom:openInterlink' diff --git a/lib/notational-velocity-view.coffee b/lib/notational-velocity-view.coffee index 1279c89..143b22e 100644 --- a/lib/notational-velocity-view.coffee +++ b/lib/notational-velocity-view.coffee @@ -3,6 +3,7 @@ fs = require 'fs-plus' _ = require 'underscore-plus' {$, $$, SelectListView} = require 'atom-space-pen-views' DocQuery = require 'docquery' +Utility = require './utility' module.exports = class NotationalVelocityView extends SelectListView @@ -10,7 +11,7 @@ class NotationalVelocityView extends SelectListView @initializedAt = new Date() super @addClass('nvatom from-top overlay') - @rootDirectory = fs.normalize(atom.config.get('nvatom.directory')) + @rootDirectory = Utility.getNoteDirectory() unless fs.existsSync(@rootDirectory) throw new Error("The given directory #{@rootDirectory} does not exist. " + "Set the note directory to the existing one from Settings.") @@ -90,10 +91,9 @@ class NotationalVelocityView extends SelectListView confirmSelection: -> item = @getSelectedItem() + sanitizedQuery = Utility.trim(@getFilterQuery()) + calculatedPath = Utility.getNotePath(sanitizedQuery) filePath = null - sanitizedQuery = @getFilterQuery().replace(/\s+$/, '') - extension = if atom.config.get('nvatom.extensions').length then atom.config.get('nvatom.extensions')[0] else '.md' - calculatedPath = path.join(@rootDirectory, sanitizedQuery + extension) if item? filePath = item.filePath else if fs.existsSync(calculatedPath) diff --git a/lib/notational-velocity.coffee b/lib/notational-velocity.coffee index d89ff65..fb9ea9a 100644 --- a/lib/notational-velocity.coffee +++ b/lib/notational-velocity.coffee @@ -1,6 +1,7 @@ path = require 'path' fs = require 'fs-plus' {CompositeDisposable, Disposable} = require 'atom' +NoteLink = require './notelink' module.exports = config: @@ -49,8 +50,11 @@ module.exports = @subscriptions.add atom.workspace.onWillDestroyPaneItem ({item}) => @autosave(item) unless @autodelete(item) + @noteLink = new NoteLink() + deactivate: -> @subscriptions.dispose() + @noteLink.destroy() @notationalVelocityView.destroy() serialize: -> @@ -66,15 +70,13 @@ module.exports = return unless paneItem?.getURI?()? return unless paneItem?.isModified?() uri = paneItem.getURI() - return unless uri.indexOf(@rootDirectory) is 0 - return unless path.extname(uri) in atom.config.get('nvatom.extensions') + return unless Utility.isNote(uri) paneItem?.save?() autodelete: (paneItem) -> return false unless paneItem?.getURI?()? uri = paneItem.getURI() - return false unless uri.indexOf(@rootDirectory) is 0 - return false unless path.extname(uri) in atom.config.get('nvatom.extensions') + return unless Utility.isNote(uri) return false unless paneItem?.isEmpty() fs.unlinkSync(uri) noteName = uri.substring(@rootDirectory.length + 1) diff --git a/lib/notelink.coffee b/lib/notelink.coffee new file mode 100644 index 0000000..41b7dbb --- /dev/null +++ b/lib/notelink.coffee @@ -0,0 +1,36 @@ +fs = require 'fs-plus' +{CompositeDisposable} = require 'atom' +Utility = require './utility' + +module.exports = +class NoteLink + constructor: -> + @subscriptions = new CompositeDisposable + @subscriptions.add atom.commands.add 'atom-workspace', 'nvatom:openInterlink': => @openInterlink() + + destroy: -> + @subscriptions.dispose() + + openInterlink: -> + editor = atom.workspace.getActiveTextEditor() + return unless editor? + return unless Utility.isNote(editor.getPath()) + + noteTitle = @getInterlinkUnderCursor(editor) + return unless noteTitle? + return unless noteTitle.length + + notePath = Utility.getNotePath(noteTitle) + + unless fs.existsSync(notePath) + fs.writeFileSync(notePath, '') + atom.workspace.open(notePath) + + 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 diff --git a/lib/utility.coffee b/lib/utility.coffee new file mode 100644 index 0000000..34c7f01 --- /dev/null +++ b/lib/utility.coffee @@ -0,0 +1,15 @@ +path = require 'path' +fs = require 'fs-plus' + +module.exports = +class Utility + + @getNotePath: (title) -> path.join(Utility.getNoteDirectory(), Utility.trim(title) + Utility.getPrimaryNoteExtension()) + + @getNoteDirectory: -> fs.normalize(atom.config.get('nvatom.directory')) + + @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') + + @trim: (str) -> str?.replace /^\s+|\s+$/g, '' diff --git a/package.json b/package.json index 8be3b98..7e222a4 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,18 @@ ], "description": "Notational Velocity for Atom", "activationCommands": { - "atom-workspace": "nvatom:toggle" + "atom-workspace": [ + "nvatom:toggle", + "nvatom:openInterlink" + ] }, + "keywords": [ + "wiki", + "notational velocity", + "nvatom", + "note", + "notetaking" + ], "repository": "https://github.com/seongjaelee/nvatom", "license": "MIT", "engines": {