From d5a79434c695da954010b1919725717cba686168 Mon Sep 17 00:00:00 2001 From: Jruif <597710271@qq.com> Date: Sat, 17 Oct 2015 01:38:49 +0800 Subject: [PATCH 1/3] Update Toc 1.support other language(e.g. Chinese); 2.optimize indent --- lib/Toc.coffee | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/Toc.coffee b/lib/Toc.coffee index acc87f4..7f3cfa0 100644 --- a/lib/Toc.coffee +++ b/lib/Toc.coffee @@ -121,10 +121,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 +189,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 From 6a55cc0b140c55e2b217c76cda8ab7b3ac3e05fd Mon Sep 17 00:00:00 2001 From: Jruif <597710271@qq.com> Date: Mon, 19 Oct 2015 17:27:15 +0800 Subject: [PATCH 2/3] let toc get active text editor --- 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 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() From 338961c5ca42ba3b8a8148cf40c141120bdf4fa5 Mon Sep 17 00:00:00 2001 From: Jruif <597710271@qq.com> Date: Mon, 19 Oct 2015 17:30:50 +0800 Subject: [PATCH 3/3] first update @editor when a change occurs --- lib/Toc.coffee | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/Toc.coffee b/lib/Toc.coffee index 7f3cfa0..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