Skip to content

Commit

Permalink
Handle file skipping internally (#143)
Browse files Browse the repository at this point in the history
The current solution for skipping files is to have a system utility such
as `ack` handle the searching for you.  This is not ideal because it
makes the documentation not as clear and ads a dependancies in some
cases.

Here we have the library handle skipping files, but simply not
transforming files that contain the tag

Co-authored-by: Andrew Smith <andrewsmith@alumni.stanford.edu>
  • Loading branch information
zyskowsk and AndrewSouthpaw authored Jun 15, 2021
1 parent bc62221 commit b115924
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ by the markdown parser. Doctoc defaults to using the GitHub parser, but other
[modes can be
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 All @@ -66,14 +68,6 @@ If you want to convert only specific files, do:

doctoc CONTRIBUTING.md LICENSE.md

You can use this feature to do more sophisticated things. For example, if you
have [ack][ack] installed, you could add `<!-- DOCTOC SKIP -->` to specific
files and then use

ack -L 'DOCTOC SKIP' | xargs doctoc

to recompile only those files which don't have the DOCTOC SKIP comment.

### Using doctoc to generate links compatible with other sites

In order to add a table of contents whose links are compatible other sites add the appropriate mode flag:
Expand Down
4 changes: 4 additions & 0 deletions lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ 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 -->';


function matchesStart(line) {
return (/<!-- START doctoc /).test(line);
Expand Down Expand Up @@ -107,6 +109,8 @@ function determineTitle(title, notitle, lines, info) {
}

exports = module.exports = function transform(content, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll, updateOnly) {
if (content.indexOf(skipTag) !== -1) return { transformed: false };

mode = mode || 'github.com';
entryPrefix = entryPrefix || '-';

Expand Down

0 comments on commit b115924

Please sign in to comment.