Skip to content
Merged
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ by github or other sites via a command line flag.
- [Installation](#installation)
- [Usage](#usage)
- [Adding toc to all files in a directory and sub directories](#adding-toc-to-all-files-in-a-directory-and-sub-directories)
- [Ignoring individual files](#ignoring-individual-files)
- [Update existing doctoc TOCs effortlessly](#update-existing-doctoc-tocs-effortlessly)
- [Adding toc to individual files](#adding-toc-to-individual-files)
- [Examples](#examples)
- [Using doctoc to generate links compatible with other sites](#using-doctoc-to-generate-links-compatible-with-other-sites)
- [Example](#example)
- [Specifying location of toc](#specifying-location-of-toc)
- [Specifying a custom TOC title](#specifying-a-custom-toc-title)
- [Specifying a minimum heading level for TOC entries](#specifying-a-minimum-heading-level-for-toc-entries)
- [Specifying a maximum heading level for TOC entries](#specifying-a-maximum-heading-level-for-toc-entries)
- [Printing to stdout](#printing-to-stdout)
- [Only update existing ToC](#only-update-existing-toc)
- [Usage as a `git` hook](#usage-as-a-git-hook)
- [Docker image](#docker-image)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->


## Installation

npm install -g doctoc
Expand All @@ -49,6 +51,7 @@ by the markdown parser. Doctoc defaults to using the GitHub parser, but other
specified](#using-doctoc-to-generate-links-compatible-with-other-sites).

### Ignoring individual files

In order to ignore a specific file when running `doctoc` on an entire directory, just add `<!-- DOCTOC SKIP -->` to the top of the file you wish to ignore.

### Update existing doctoc TOCs effortlessly
Expand Down
7 changes: 4 additions & 3 deletions lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var _ = require('underscore')
var start = '<!-- START doctoc generated TOC please keep comment here to allow auto update -->\n' +
'<!-- DON\'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->'
, end = '<!-- END doctoc generated TOC please keep comment here to allow auto update -->'
, skipTag = '<!-- DOCTOC SKIP -->';
, skipTag = '<!-- DOCTOC SKIP -->\n';


function matchesStart(line) {
Expand Down Expand Up @@ -109,7 +109,8 @@ function determineTitle(title, notitle, lines, info) {
}

exports = module.exports = function transform(content, mode, maxHeaderLevel, minHeaderLevel, title, notitle, entryPrefix, processAll, updateOnly) {
if (content.indexOf(skipTag) !== -1) return { transformed: false };
var index = content.indexOf(skipTag);
if (index === 0 || (index >= 0 && content[index-1] === '\n')) return { transformed: false };

mode = mode || 'github.com';
entryPrefix = entryPrefix || '-';
Expand All @@ -119,7 +120,7 @@ exports = module.exports = function transform(content, mode, maxHeaderLevel, min
var maxHeaderLevelHtml = maxHeaderLevel || 4;

var lines = content.split('\n')
, info = updateSection.parse(lines, matchesStart, matchesEnd)
, info = updateSection.parse(lines, matchesStart, matchesEnd);

if (!info.hasStart && updateOnly) {
return { transformed: false };
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/readme-with-firstline-skipTag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- DOCTOC SKIP -->
# Hello, world!

README to test doctoc with skipTag.

## Installation
## API
## License
9 changes: 9 additions & 0 deletions test/fixtures/readme-with-invalidskipTag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Hello, world!

README to test doctoc with skipTag.

<!-- DOCTOC SKIP -->`

## Installation
## API
## License
26 changes: 25 additions & 1 deletion test/transform-skipTag.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var test = require('tap').test
, transform = require('../lib/transform');

test('\nskip file transform', function (t) {
test('\nskip tag non first line transform', function (t) {
var content = require('fs').readFileSync(__dirname + '/fixtures/readme-with-skipTag.md', 'utf8');
var transformedContent = transform(content);

Expand All @@ -15,3 +15,27 @@ test('\nskip file transform', function (t) {
)
t.end()
});

test('\nskip tag first line transform', function (t) {
var content = require('fs').readFileSync(__dirname + '/fixtures/readme-with-firstline-skipTag.md', 'utf8');
var transformedContent = transform(content);

t.deepEqual(
transformedContent.toc
, undefined
, 'skip correct file'
)
t.end()
});

test('\nskip tag invalid nested transform', function (t) {
var content = require('fs').readFileSync(__dirname + '/fixtures/readme-with-invalidskipTag.md', 'utf8');
var transformedContent = transform(content);

t.deepEqual(
transformedContent.transformed
, true
, 'don\'t skip file as skipTag is invalid'
)
t.end()
});