diff --git a/lib/Toc.coffee b/lib/Toc.coffee index acc87f4..66f2656 100644 --- a/lib/Toc.coffee +++ b/lib/Toc.coffee @@ -1,7 +1,7 @@ module.exports = class Toc - + editor: null constructor: (@editor) -> @lines = [] @list = [] @@ -13,7 +13,7 @@ class Toc @create() at = @ - @editor.getBuffer().onWillSave () -> + @editor?.getBuffer().onWillSave () -> if at.options.updateOnSave is 1 if at._hasToc() at._deleteToc() @@ -52,9 +52,13 @@ class Toc # ---------------------------------------------------------------------------- - + _getActiveEditor : -> + atom.workspace.getActiveTextEditor() _hasToc: () -> + # first update @editor + @editor = @_getActiveEditor() + @___updateLines() if @lines.length > 0 @@ -121,10 +125,11 @@ class Toc __createList: () -> list = [] indicesOfDepth = Array.apply(null, new Array(6)).map(Number.prototype.valueOf, 0); + minDepth = @list[..].sort((a,b)-> a.depth-b.depth)[0].depth for own i, item of @list row = [] - for tab in [1..item.depth] when tab > 1 + for tab in [1..item.depth] when tab > minDepth row.push "\t" if @options.orderedList is 1 row.push ++indicesOfDepth[item.depth-1] + ". " @@ -188,8 +193,9 @@ class Toc # create hash and surround link withit ___createLink: (name) -> hash = new String name - hash = hash.toLowerCase().replace /\s/g, "-" - hash = hash.replace /[^a-z0-9\-]/g, "" + #hash = hash.toLowerCase().replace /\s/g, "-" + #hash = hash.replace /[^a-z0-9\-]/g, "" + hash = hash.replace /\s/g, "-" if hash.indexOf("--") > -1 hash = hash.replace /(-)+/g, "-" if name.indexOf(":-") > -1 diff --git a/lib/markdown-toc.coffee b/lib/markdown-toc.coffee index 95e54f0..05aba50 100644 --- a/lib/markdown-toc.coffee +++ b/lib/markdown-toc.coffee @@ -3,7 +3,7 @@ Toc = require './Toc' module.exports = activate: (state) -> - @toc = new Toc(atom.workspace.getActivePaneItem()) + @toc = new Toc(atom.workspace.getActiveTextEditor()) atom.commands.add 'atom-workspace', 'markdown-toc:create': => @toc.create() atom.commands.add 'atom-workspace', 'markdown-toc:update': => @toc.update()