GithubHelp home page GithubHelp logo

thlorenz / doctoc Goto Github PK

View Code? Open in Web Editor NEW
4.2K 63.0 481.0 192 KB

📜 Generates table of contents for markdown files inside local git repository. Links are compatible with anchors generated by github or other sites.

Home Page: https://www.npmjs.com/package/doctoc

License: MIT License

JavaScript 100.00%

doctoc's Introduction

DocToc Node.js CI

Generates table of contents for markdown files inside local git repository. Links are compatible with anchors generated by github or other sites via a command line flag.

Table of Contents generated with DocToc

Installation

npm install -g doctoc

Usage

In its simplest usage, you can pass one or more files or folders to the doctoc command. This will update the TOCs of each file specified as well as of each markdown file found by recursively searching each folder. Below are some examples.

Adding toc to all files in a directory and sub directories

Go into the directory that contains you local git project and type:

doctoc .

This will update all markdown files in the current directory and all its subdirectories with a table of content that will point at the anchors generated by the markdown parser. Doctoc defaults to using the GitHub parser, but other modes can be specified.

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

If you already have a TOC inserted by doctoc, it will automatically be updated by running the command (rather than inserting a duplicate toc). Doctoc locates the TOC by the <!-- START doctoc --> and <!-- END doctoc --> comments, so you can also move a generated TOC to any other portion of your document and it will be updated there.

Adding toc to individual files

If you want to convert only specific files, do:

doctoc /path/to/file [...]

Examples

doctoc README.md

doctoc CONTRIBUTING.md LICENSE.md

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:

Available modes are:

--bitbucket bitbucket.org
--nodejs    nodejs.org
--github    github.com
--gitlab    gitlab.com
--ghost     ghost.org

Example

doctoc README.md --bitbucket

Specifying location of toc

By default, doctoc places the toc at the top of the file. You can indicate to have it placed elsewhere with the following format:

<!-- START doctoc -->
<!-- END doctoc -->

You place this code directly in your .md file. For example:

// my_new_post.md
Here we are, introducing the post. It's going to be great!
But first: a TOC for easy reference.

<!-- START doctoc -->
<!-- END doctoc -->

# Section One

Here we'll discuss...

Running doctoc will insert the toc at that location.

Specifying a custom TOC title

Use the --title option to specify a (Markdown-formatted) custom TOC title; e.g., doctoc --title '**Contents**' . From then on, you can simply run doctoc <file> and doctoc will will keep the title you specified.

Alternatively, to blank out the title, use the --notitle option. This will simply remove the title from the TOC.

Specifying a maximum heading level for TOC entries

Use the --maxlevel option to limit TOC entries to headings only up to the specified level; e.g., doctoc --maxlevel 3 .

By default,

  • no limit is placed on Markdown-formatted headings,
  • whereas headings from embedded HTML are limited to 4 levels.

Printing to stdout

You can print to stdout by using the -s or --stdout option.

Only update existing ToC

Use --update-only or -u to only update the existing ToC. That is, the Markdown files without ToC will be left untouched. It is good if you want to use doctoc with lint-staged.

Usage as a git hook

doctoc can be used as a pre-commit hook by using the following configuration:

repos:
-   repo: https://github.com/thlorenz/doctoc
    rev: ...  # substitute a tagged version
    hooks:
    -   id: doctoc

This will run doctoc against markdown files when committing to ensure the TOC stays up-to-date.

Docker image

There's an unofficial Docker image project for doctoc, if you'd like to use doctoc via Docker or other container based CI/CD pipeline, you can take a look at PeterDaveHello/docker-doctoc

doctoc's People

Contributors

a0viedo avatar alexandru-constantin avatar andrewsouthpaw avatar asottile avatar bitdeli-chef avatar chocolateboy avatar dependabot[bot] avatar faheel avatar greyscaled avatar hongaar avatar jez avatar jmanuel1 avatar kevin-david avatar lydell avatar miangraham-ss avatar michelle avatar mklement0 avatar peterdavehello avatar plumlee avatar qubyte avatar raynos avatar richardlitt avatar sarisia avatar sheriferson avatar thlorenz avatar uetchy avatar wolfcon avatar zyskowsk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

doctoc's Issues

Seems that --nodejs is broken as of v0.11.0

When using --nodejs, you get the following error message:

Need module name to generate proper anchor for nodejs.org

anchorMarkdownHeader() in anchor-markdown-header.js requires a module name for mode --nodejs, but addAnchor() in transform.js isn't passing one in.

Incidentally,

  • what is the module name that is required - is it the npm package name?
  • is --nodejs meant to target npmjs.com, i.e., the npm registry?
  • does a GitHub-style read-me need to be processed with --nodejs in order for read-me-internal links to work at npmjs.com?

Infer --title

It'd be awesome if you only had to specify --title if

  • you're running doctoc for the first time, or
  • you want to change the title of the ToC

and in other cases (i.e. when the ToC, START doctoc, END doctoc sections already exist) the title is inferred from the existing ToC

Bad links for titles containing ticks

This might also apply to other non-alphanum characters, but the following is not compatible to what GitHub generates:

- ['webdav' Upload Method for 'dput'](#'webdav'-upload-method-for-'dput')

Should be

- ['webdav' Upload Method for 'dput'](#webdav-upload-method-for-dput)

Header with &

For markdown headers containing &, the link doesn't work.

URL-encoding the & (to %26) doesn't work, so maybe display a warning...or?

Feature request: max depth

It would be neat to be able to define a max depth for the headers included in the TOC, passed in as an argument. So a max-depth of 3 would not show #### headers in the TOC.

I may create a pull request on this eventually, haven't looked at the code yet so not sure how tricky it would be to implement, but figured I would create this feature request for now.

Add way of checking if existing ToCs need updating

I'm not currently a doctoc user, but if I were, one feature I imagine I'd like would be the ability to check whether doctoc needs running. This would be handy as part of a CI test suite to ensure that some careless soul hadn't updated the readme and forgotten to run doctoc.

All the check would need to do is locate all markdown files with a in them and check that their current content exactly matches what their content would be after running doctoc.

Specify level of depth

First of all, thanks for this very handy module!

Another suggestion from me for furure releases: It would be great if you could specify the level of depth for headlines to be included in the TOC. Eg. doctoc README.md -d 3 would only include headline levels 1, 2, and 3.

Soft link causes doctoc to crash.

I have a valid soft link in my directory and when I run doctoc, it says there isn't a directory there and then quits. Let me know if you need anymore info!

Handle double # style headings

Currenly, headings like these:

### This is a h3 ###

do not work.

GitHub parses this as <h3>This is a h3</h3> with anchor this-is-a-h3, but doctoc parses it as This is a h3 ### with anchor this-is-a-h3-###.

Doctoc generating newline in header entries

I'm getting markdown that looks like this:

](#changelog)
- [How to Contribute!
](#how-to-contribute)
- [License
](#license)

From a header that looks like this:

`blocks.js`
============

This seems to be happening to all my top level headers

Generate anchors

Hi
Could be possible to automatically add anchors to existing headers, matching to doctoc links?

# header 1
# header 2

And

$ doctoc .

adds <a id="header1"></a> before # header 1 to create something like

<p>
<a id="header1"></a>
</p>
<h1>header1</h1>

Mixes spaces and tabs

Your doctoc adds tabs to my README.

it would be nice if it detected spaces in the README and used spaces for indentation.

`npm install -g doctoc`-> "No compatible version found"

I'd like to generate TOCs for my github wiki pages, but am new to node.js. I'm running

$ lsb_release -ds
Linux Mint Debian Edition
$ cat /etc/debian_version
wheezy/sid
$ uname -rv
3.2.0-3-amd64 #1 SMP Thu Jun 28 09:07:26 UTC 2012
$ node --version
v0.5.11-pre
$ npm --version
1.0.106

I did

$ npm install -g doctoc
...

npm ERR! Additional logging details can be found in:
npm ERR! /home/me/bin/node/npm-debug.log
npm not ok

I noticed npm-debug.log had

verbose mkdir (expected) error ENOENT, No such file or directory '/home/me/.npm/doctoc'
verbose mkdir done: /home/me/.npm/doctoc 755

so I did

$ mkdir -p ~/.npm/doctoc
$ npm install -g doctoc

I no longer get the error above, but still get

$ cat /home/me/bin/node/npm-debug.log

info it worked if it ends with ok
verbose cli [ 'node', '/usr/local/bin/npm', 'install', '-g', 'doctoc' ]
info using [email protected]
info using [email protected]
verbose config file /home/me/.npmrc
verbose config file /usr/local/etc/npmrc
verbose config file /usr/local/lib/node_modules/npm/npmrc
verbose into /usr/local/lib [ 'doctoc' ]
verbose cache add doctoc
verbose cache add [ 'doctoc', null ]
silly cache add: name, spec, args [ undefined, 'doctoc', [ 'doctoc', null ] ]
verbose parsed url { pathname: 'doctoc', path: 'doctoc', href: 'doctoc' }
info addNamed [ 'doctoc', '' ]
verbose addNamed [ null, '' ]
verbose GET doctoc
verbose raw, before any munging doctoc
verbose url resolving [ 'https://registry.npmjs.org/', './doctoc' ]
verbose url resolved https://registry.npmjs.org/doctoc
verbose etag "6IIV956MII1N2VCN38UL5UC81"
silly get cb [ 304,
silly get cb { server: 'CouchDB/1.2.0 (Erlang OTP/R15B)',
silly get cb etag: '"6IIV956MII1N2VCN38UL5UC81"',
silly get cb date: 'Sun, 12 Aug 2012 17:00:27 GMT',
silly get cb 'content-length': '0' } ]
verbose etag doctoc from cache
ERR! Error: No compatible version found: doctoc
ERR! No valid targets found.
ERR! Perhaps not compatible with your version of node?
ERR! at installTargetsError (/usr/local/lib/node_modules/npm/lib/cache.js:424:10)
ERR! at /usr/local/lib/node_modules/npm/lib/cache.js:406:17
ERR! at saved (/usr/local/lib/node_modules/npm/lib/utils/npm-registry-client/get.js:136:7)
ERR! at Object.cb as oncomplete
ERR! Report this entire log at:
ERR! http://github.com/isaacs/npm/issues
ERR! or email it to:
ERR! [email protected]
ERR!
ERR! System Linux 3.2.0-3-amd64
ERR! command "node" "/usr/local/bin/npm" "install" "-g" "doctoc"
ERR! cwd /home/me/bin/node
ERR! node -v v0.5.11-pre
ERR! npm -v 1.0.106
verbose exit [ 1, true ]

Is there another way to install doctoc? Do I need to update my version of node?

Skip file

Maybe a comment in files saying to skip TOC, so if you have many files and still want to do doctoc .

DocToc gets confused by dashes in pargraphs

Given the following markdown:

#### Foo

I am a long paragraph that goes on and on. I am a long paragraph that
goes on and on. I am a long paragraph that goes on and on. I am a long
paragraph that goes on and on. I happen to have a dash halfway
-- through me but that shouldn't affect doctoc.

DocToc generates the following table of contents:

**Table of Contents**  *generated with [DocToc](http://doctoc.herokuapp.com/)*

                - [Foo](#foo)
- [paragraph that goes on and on. I happen to have a dash halfway](#paragraph-that-goes-on-and-on-i-happen-to-have-a-dash-halfway)

DocToc discards text without headers

Given the input

hello world

### Blah blah

foo bar

'hello world' is removed by doctoc:

**Table of Contents**  *generated with [DocToc](http://doctoc.herokuapp.com/)*

- [Blah blah](#blah-blah)

### Blah blah

foo bar

Add support for Bitbucket markdown

At bitbucket.org the automatic anchor labels have a prefix "markdown-header-" and then the label is equal to the ones that generate github.com.

This is and example of a valid TOC link for bitbucket.org wiki:

   - [Installation and Execution](#markdown-header-installation-and-execution)

Here,
https://bitbucket.org/bbglab/svgmap/wiki/User-guide

you have an example of a TOC generated which doctoc and then I only needed to add the "markdown-header-" prefix to all the entries.

Can't regex process <hX> when X is an integer

It works for any other letter besides "h", but breaks when in angle brackets, pared with an integer - whether the brackets are escaped or not.

good:

a1
<a1>
\<a1\>
h1
<9h>

bad:

<h2>
\<h2\>
\<hh2\>
\<h2h\>

Here's the stack:

/usr/local/lib/node_modules/doctoc/lib/get-html-headers.js:11
      if (new RegExp(x.text[0]).test(line)) {
          ^
SyntaxError: Invalid regular expression: /

* HTML5 container semantics
  * proper use of [**\/: \ at end of pattern
    at new RegExp (<anonymous>)
    at /usr/local/lib/node_modules/doctoc/lib/get-html-headers.js:11:11
    at Array.map (native)
    at addLinenos (/usr/local/lib/node_modules/doctoc/lib/get-html-headers.js:8:18)
    at module.exports (/usr/local/lib/node_modules/doctoc/lib/get-html-headers.js:66:13)
    at transform (/usr/local/lib/node_modules/doctoc/lib/transform.js:127:13)
    at /usr/local/lib/node_modules/doctoc/doctoc.js:26:20
    at Array.map (native)
    at transformAndSave (/usr/local/lib/node_modules/doctoc/doctoc.js:24:6)
    at Object.<anonymous> (/usr/local/lib/node_modules/doctoc/doctoc.js:66:1)

github markdown will ignore "(" and ")"

the generated index does not work when the title contains "(" or ")"

one example as follows:

<h1>
<a name="user-content-%E9%A2%84%E8%AE%BE%E9%9B%86preset" class="anchor" href="#%E9%A2%84%E8%AE%BE%E9%9B%86preset">
<span class="octicon octicon-link"></span>
</a>预设集(Preset)</h1>

generated header by doctoc

- [预设集(Preset)](#预设集(preset))

Github does not support UTF-8 fully

# This is ä test of ÜTF-8 & Numb3rs (███)

is translated to #this-is-%C3%A4-test-of-%C3%9Ctf-8--numb3rs- by github.

Doctoc translates it to #this-is-%C3%A4-test-of-%C3%BCtf-8-&-numb3rs-%E2%96%88%E2%96%88%E2%96%88

As you can see, the Ampersand is stripped and also the UTF-8 Blocks (█)

Hyperlinked heading is linked with heading's hyperlink, not heading anchor

Hi, very cool project here! I did notice an issue when using with my README:

I have a heading that is linked (not sure if this is bad form, probably is):

##### [CNN](https://www.cnn.com)

When doctoc is run against this README, it renders the TOC entry as so:

 - [[CNN](https://www.cnn.com)](...urlpathstuff#cnn)

So when I client on this TOC entry, it sends me to www.cnn.com, not #cnn

tia
geremy

Does this still install as a command line executable?

I'm probably not getting something right, but when I install this using

npm install -g doctoc

It install fine, but doesn't work when I try and execute "doctoc" (command not found)

I've installed other things using npm like (e.g. bower) and they work fine and install to:

/usr/local/bin/

Maybe I don't have the path to where doctoc installs it's shell script in my path.

Where does the doctoc shell script install to?

Thanks!

Doctoc generates incorrect anchor link when there is a url link in the header

For the header:

#### Instance properties inherited from [`EventEmitter`](http://nodejs.org/api/events.html)

doctoc is generating:

[Instance properties inherited from [`EventEmitter`](http://nodejs.org/api/events.html)](#instance-properties-inherited-from-eventemitterhttpnodejsorgapieventshtml)

Which doesn't link to the header (tho it does link to EventEmitter)

Overview of all Files/Sections

I don't know if this is already a part of DocTo:

It would be cool to have the ability to generate a seperate file, wich contains all ToCs from the other Files, so that this file could be used as Navigation.

TOC title renders raw (no Markdown processing) on npmjs.com

This is not doctoc's problem, but I thought I'd document it here and suggest a workaround:

The problem has been reported to the npm registry website as npm/newww#1018.

In short: because the TOC title follows doctoc's HTML start comments without an intervening empty line, the title renders without any Markdown processing - presumably a bug in the markdown-it package used by the npm registry website. Let's hope it gets fixed soon.

A quick workaround is to prepend a newline to the title passed to --title.

If a fix on npmjs.com isn't forthcoming, we could consider changing doctoc to insert an empty line by default after the start-of-TOC HTML comments - this would fix the npmjs.com issue and shouldn't change the existing rendering on GitHub.

Bitbucket nested links

It appears that Bitbucket markdown formatting expects nested lists to have an indention of 4 characters in order to properly nest, according to their documentation site. Search that page for 'Lists can be embedded in lists.' - sorry I can't seem to figure out how to get the raw file + the line numbers for a good link.

Doctoc is currently generating an indention level of 2 spaces for each nested list item in the navigation header. Would you be open to a PR that allowed a command line param to adjust this setting?

I don't know when this might have changed, so please clue me in if I'm missing something obvious. Doctoc works like a charm for me otherwise, thanks for a great tool.

Exclamation marks (!) in headers break toc links

In my README.md I have e.g.:

Hi my friend!

Currently, the generated TOC entry will be:

However, the link won't work on GitHub. Exclamation marks must be omitted. The correct TOC entry is:

I observed this behavior for exclamation marks at the end of a header as well as in the middle. In both cases they need to be omitted in the anchor.

TOC incorrect link to header that contains vertical bar character

Very useful tool! I did find a minor issue. In a Github wiki page I have a header that looks like this:

#### `history [pgn | alg]`

When I open the wiki page in my browser and View Source, I see this where the header is in the html code:

<h4>
<a id="user-content-history-pgn--alg" class="anchor" href="#history-pgn--alg" aria-hidden="true"><span class="octicon octicon-link"></span></a><code>history [pgn | alg]</code>
</h4>

DocToc generates the following TOC entry:

- [`history [pgn | alg]`](#history-pgn-%7C-alg)

Note the TOC link's anchor tag has an extraneous %7C in it.

I am manually fixing this for now, but I thought you would want to know.

Update: A similar problem happens when the header title text contains a # character: GitHub's Markdown processor creates an anchor without the #, but that character is still in DocToc's link to the anchor.

doctoc generates TOC before Title, unlike sample

First Thorsten thanks for making this available and thanks for packaging via NPM. That worked great.

I have a problem that my title is always inserted into the table of contents and then the table of contents comes before the title.

I wondered if I am doing something wrong because that does not happen with your sample? You are regenerating the sample as a test as part of your build, right?

If I move the title manually, the next time I run it, it generates a second table of contents.

Here's a simple test to demonstrate:

# My Title

## My first section
blah blah blah

## My second section
blah blah blah

### My first subsection
blah blah

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.