Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. fs = require 'fs-plus'
  2. path = require 'path'
  3. {CompositeDisposable} = require 'atom'
  4. Utility = require './utility'
  5.  
  6. module.exports =
  7. class Interlink
  8. constructor: ->
  9. Interlink.loadGrammarSync()
  10. @subscriptions = new CompositeDisposable
  11. @subscriptions.add atom.commands.add 'atom-workspace', 'nvatom:openInterlink': => Interlink.openInterlink()
  12. @subscriptions.add atom.workspace.observeTextEditors (editor) ->
  13. if Utility.isNote(editor.getPath())
  14. editor.setGrammar(atom.grammars.grammarForScopeName('text.md'))
  15.  
  16. destroy: ->
  17. @subscriptions.dispose()
  18.  
  19. @loadGrammarSync: ->
  20. unless atom.grammars.grammarForScopeName('text.md')
  21. grammarPath = path.join(atom.packages.resolvePackagePath('language-markdown'), 'grammars', 'language-markdown.json')
  22. atom.grammars.loadGrammarSync(grammarPath)
  23.  
  24. @openInterlink: ->
  25. editor = atom.workspace.getActiveTextEditor()
  26. return unless editor?
  27. return unless Utility.isNote(editor.getPath())
  28.  
  29. noteTitle = Interlink.getInterlinkUnderCursor(editor)
  30. return unless noteTitle?
  31. return unless noteTitle.length
  32.  
  33. notePath = Utility.getNotePath(noteTitle)
  34.  
  35. unless fs.existsSync(notePath)
  36. fs.writeFileSync(notePath, '')
  37. atom.workspace.open(notePath)
  38.  
  39. @getInterlinkUnderCursor: (editor) ->
  40. cursorPosition = editor.getCursorBufferPosition()
  41. token = editor.tokenForBufferPosition(cursorPosition)
  42. return unless token
  43. return unless token.value
  44. return unless token.scopes.indexOf('markup.underline.link.interlink.md') > -1
  45.  
  46. interlink = Utility.trim(token.value)
  47. return unless interlink.length
  48. return interlink
  49.  
  50.  
  51. # Second part
  52.  
  53. 'name': 'nvAtom'
  54. 'scopeName': 'text.md.nvatom'
  55. 'patterns': [
  56. {
  57. 'include': 'text.md'
  58. }
  59. {
  60. 'match': '(?<!\\[)(\\[\\[)([^\\[\\]\|]+)(\\]\\])(?!\\])'
  61. 'name': 'interlink'
  62. 'captures':
  63. '1':
  64. 'name': 'punctuation.definition.begin.md'
  65. '2':
  66. 'name': 'markup.underline.link.interlink.md'
  67. '3':
  68. 'name': 'punctuation.definition.begin.md'
  69. }
  70. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement