From 428b754f327b6fd592a37b9da91b30389d7d483a Mon Sep 17 00:00:00 2001 From: Chris Terry Date: Sun, 31 Mar 2019 00:04:48 -0600 Subject: [PATCH 1/3] Reads in existing TOC on markdown file load --- README.md | 14 +++----------- lib/Toc.coffee | 16 ++++++++++++++++ lib/markdown-toc.coffee | 37 +++++++++++++++++++++++++------------ package.json | 7 ++++++- 4 files changed, 50 insertions(+), 24 deletions(-) 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..0b4d29a 100644 --- a/lib/markdown-toc.coffee +++ b/lib/markdown-toc.coffee @@ -3,18 +3,31 @@ 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.init(event.textEditor) + + initToc: (thisEditor) -> + 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" From dd5cda38a6f5151a13487255be0e75c57bc55727 Mon Sep 17 00:00:00 2001 From: Chris Terry Date: Sun, 31 Mar 2019 00:21:16 -0600 Subject: [PATCH 2/3] Typo --- lib/markdown-toc.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/markdown-toc.coffee b/lib/markdown-toc.coffee index 0b4d29a..d027efa 100644 --- a/lib/markdown-toc.coffee +++ b/lib/markdown-toc.coffee @@ -10,7 +10,7 @@ module.exports = at = @ atom.workspace.onDidAddTextEditor (event) -> - at.init(event.textEditor) + at.initToc(event.textEditor) initToc: (thisEditor) -> thisGrammar = thisEditor.getGrammar().packageName From 76134c2d3b9ac7bd6449eba1f39a806151186186 Mon Sep 17 00:00:00 2001 From: Chris Terry Date: Tue, 2 Apr 2019 09:25:40 -0600 Subject: [PATCH 3/3] Skip editors with undefined grammars --- lib/markdown-toc.coffee | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/markdown-toc.coffee b/lib/markdown-toc.coffee index d027efa..8f0cecc 100644 --- a/lib/markdown-toc.coffee +++ b/lib/markdown-toc.coffee @@ -13,21 +13,22 @@ module.exports = at.initToc(event.textEditor) initToc: (thisEditor) -> - 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() + 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()