diff --git a/README.md b/README.md index 5f256c0..6e7beca 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ Markdown TOC Generate and update magically a table of contents based on the headlines of a parsed [markdown](http://en.wikipedia.org/wiki/Markdown) file. -## Table of Contents +# Table of Contents + - [Usage](#usage) - [Installation](#installation) @@ -13,21 +14,12 @@ Generate and update magically a table of contents based on the headlines of a pa - [Questions?](#questions) - [License](#license) + ## Usage ![Magic](https://raw.githubusercontent.com/nok/markdown-toc/master/RECORD.gif) - - ## Installation ```bash diff --git a/lib/Toc.coffee b/lib/Toc.coffee index db89079..77783e9 100644 --- a/lib/Toc.coffee +++ b/lib/Toc.coffee @@ -24,6 +24,11 @@ class Toc # main methods (highest logic level) + init: -> + if @_hasToc() + @_readToc() + + create: -> if @_hasToc() @_deleteToc() @@ -85,6 +90,17 @@ class Toc return true return false + _readToc: () -> + @__updateList() + if Object.keys(@list).length > 0 + text = [] + text.push "\n" + list = @__createList() + if list isnt false + Array.prototype.push.apply text, list + text.push "\n" + return text.join "\n" + return "" # embed list with the open and close comment: # [list] diff --git a/lib/markdown-toc.coffee b/lib/markdown-toc.coffee index aa30d5d..8f0cecc 100644 --- a/lib/markdown-toc.coffee +++ b/lib/markdown-toc.coffee @@ -3,18 +3,32 @@ Toc = require './Toc' module.exports = activate: (state) -> - atom.commands.add 'atom-workspace', 'markdown-toc:create': => - @toc = new Toc(atom.workspace.getActivePaneItem()) - @toc.create() - atom.commands.add 'atom-workspace', 'markdown-toc:update': => - @toc = new Toc(atom.workspace.getActivePaneItem()) - @toc.update() - atom.commands.add 'atom-workspace', 'markdown-toc:delete': => - @toc = new Toc(atom.workspace.getActivePaneItem()); - @toc.delete() - atom.commands.add 'atom-workspace', 'markdown-toc:toggle': => - @toc = new Toc(atom.workspace.getActivePaneItem()) - @toc.toggle() + editorCount = 0 + while editorCount < atom.workspace.getTextEditors().length + @initToc(atom.workspace.getTextEditors()[editorCount]) + editorCount++ + at = @ + atom.workspace.onDidAddTextEditor (event) -> + at.initToc(event.textEditor) + + initToc: (thisEditor) -> + if thisEditor.getGrammar().packageName isnt undefined + thisGrammar = thisEditor.getGrammar().packageName + if thisGrammar.match /^.*(markdown|gfm).*$/g + @toc = new Toc(thisEditor) + @toc.init() + atom.commands.add 'atom-workspace', 'markdown-toc:create': => + @toc = new Toc(thisEditor) + @toc.create() + atom.commands.add 'atom-workspace', 'markdown-toc:update': => + @toc = new Toc(thisEditor) + @toc.update() + atom.commands.add 'atom-workspace', 'markdown-toc:delete': => + @toc = new Toc(thisEditor); + @toc.delete() + atom.commands.add 'atom-workspace', 'markdown-toc:toggle': => + @toc = new Toc(thisEditor) + @toc.toggle() # deactivate: -> # @toc.destroy() diff --git a/package.json b/package.json index 69ad74e..e03303d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "markdown-toc", "main": "./lib/markdown-toc", - "version": "0.4.2", + "version": "0.4.3", "description": "Generate TOC (table of contents) of headlines from parsed markdown file", "activationCommands": { "atom-workspace": [ @@ -11,6 +11,11 @@ "markdown-toc:delete" ] }, + "activationHooks": [ + "language-gfm:grammar-used", + "language-gfm-enhanced:grammar-used", + "language-markdown:grammar-used" + ], "repository": { "type": "git", "url": "https://github.com/nok/markdown-toc"