Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lib/Toc.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports =
class Toc


editor: null
constructor: (@editor) ->
@lines = []
@list = []
Expand All @@ -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()
Expand Down Expand Up @@ -52,9 +52,13 @@ class Toc


# ----------------------------------------------------------------------------

_getActiveEditor : ->
atom.workspace.getActiveTextEditor()

_hasToc: () ->
# first update @editor
@editor = @_getActiveEditor()

@___updateLines()

if @lines.length > 0
Expand Down Expand Up @@ -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] + ". "
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/markdown-toc.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down