Skip to content
This repository was archived by the owner on May 29, 2026. It is now read-only.
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
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<!-- TOC depthFrom:2 depthTo:6 withLinks:1 updateOnSave:1 orderedList:0 -->

- [Usage](#usage)
- [Installation](#installation)
Expand All @@ -13,21 +14,12 @@ Generate and update magically a table of contents based on the headlines of a pa
- [Questions?](#questions)
- [License](#license)

<!-- /TOC -->

## Usage

![Magic](https://raw.githubusercontent.com/nok/markdown-toc/master/RECORD.gif)

<!--
## API Attributes

- `depthFrom:1`
- `depthTo:6`
- `withLinks:1`
- `updateOnSave:1`
- `orderedList:0`
-->

## Installation

```bash
Expand Down
16 changes: 16 additions & 0 deletions lib/Toc.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class Toc
# main methods (highest logic level)


init: ->
if @_hasToc()
@_readToc()


create: ->
if @_hasToc()
@_deleteToc()
Expand Down Expand Up @@ -85,6 +90,17 @@ class Toc
return true
return false

_readToc: () ->
@__updateList()
if Object.keys(@list).length > 0
text = []
text.push "<!-- TOC depthFrom:"+@options.depthFrom+" depthTo:"+@options.depthTo+" withLinks:"+@options.withLinks+" updateOnSave:"+@options.updateOnSave+" orderedList:"+@options.orderedList+" -->\n"
list = @__createList()
if list isnt false
Array.prototype.push.apply text, list
text.push "\n<!-- /TOC -->"
return text.join "\n"
return ""

# embed list with the open and close comment:
# <!-- TOC --> [list] <!-- /TOC -->
Expand Down
38 changes: 26 additions & 12 deletions lib/markdown-toc.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -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"
Expand Down