Skip to content

Commit

Permalink
feat(markdown): Add table of contents support for Markdown mode
Browse files Browse the repository at this point in the history
Fixes #220
  • Loading branch information
tmcw committed Dec 29, 2016
1 parent 2ae5d8f commit 611ecbb
Show file tree
Hide file tree
Showing 116 changed files with 1,029 additions and 6 deletions.
10 changes: 10 additions & 0 deletions docs/NODE_API.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

- [build](#build)
- [buildSync](#buildsync)
- [lint](#lint)
- [formats](#formats)
- [formats.html](#formatshtml)
- [formats.markdown](#formatsmarkdown)
- [formats.json](#formatsjson)

## build

Generate JavaScript documentation as a list of parsed JSDoc
Expand Down
6 changes: 6 additions & 0 deletions lib/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ module.exports.builder = extend({},
default: 'json',
choices: ['json', 'md', 'remark', 'html']
},
'no-markdown-toc': {
describe: 'include a table of contents in markdown output',
default: 'stdout',
type: 'boolean'
},
output: {
describe: 'output location. omit for stdout, otherwise is a filename ' +
'for single-file outputs and a directory name for multi-file outputs like html',
Expand Down Expand Up @@ -66,6 +71,7 @@ module.exports.handler = function build(argv, callback) {
version: argv['project-version'] || (argv.package || {}).version,
theme: argv.theme,
paths: argv.paths,
'no-markdown-toc': argv['no-markdown-toc'],
hljs: argv.hljs || {}
};

Expand Down
4 changes: 3 additions & 1 deletion lib/output/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ var remark = require('remark'),
* });
*/
module.exports = function (comments, options, callback) {
var processor = remark().use(toc);
var processor = remark().use(toc, {
tight: true
});
markdownAST(comments, options, function (err, ast) {
var processedAST = processor.run(ast);
return callback(null, processor.stringify(processedAST));
Expand Down
16 changes: 11 additions & 5 deletions lib/output/markdown_ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var u = require('unist-builder'),
* @param {Function} callback called with AST
* @returns {undefined} calls callback
*/
function commentsToAST(comments, options, callback) {
function markdownAST(comments, options, callback) {

// Configure code highlighting
var hljsOptions = (options || {}).hljs || {},
Expand All @@ -33,6 +33,10 @@ function commentsToAST(comments, options, callback) {
u('html', '<!-- Generated by documentation.js. Update this documentation by updating the source code. -->')
];

var tableOfContentsHeading = [
u('heading', { depth: 3 }, [u('text', 'Table of Contents')])
];

/**
* Generate an AST chunk for a comment at a given depth: this is
* split from the main function to handle hierarchially nested comments
Expand Down Expand Up @@ -203,9 +207,11 @@ function commentsToAST(comments, options, callback) {
}

return callback(null, rerouteLinks(linkerStack.link,
u('root', generatorComment.concat(comments.reduce(function (memo, comment) {
return memo.concat(generate(2, comment));
}, [])))));
u('root', generatorComment
.concat(options['no-markdown-toc'] ? [] : tableOfContentsHeading)
.concat(comments.reduce(function (memo, comment) {
return memo.concat(generate(2, comment));
}, [])))));
}

module.exports = commentsToAST;
module.exports = markdownAST;
4 changes: 4 additions & 0 deletions test/fixture/auto_lang_hljs/multilanguage.output.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

- [multilanguage.input](#multilanguageinput)

## multilanguage.input

**Extends Foo, Bar**
Expand Down
4 changes: 4 additions & 0 deletions test/fixture/boolean-literal-type.output.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

- [f](#f)

## f

**Parameters**
Expand Down
10 changes: 10 additions & 0 deletions test/fixture/boolean-literal-type.output.md.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
"type": "html",
"value": "<!-- Generated by documentation.js. Update this documentation by updating the source code. -->"
},
{
"depth": 3,
"type": "heading",
"children": [
{
"type": "text",
"value": "Table of Contents"
}
]
},
{
"depth": 2,
"type": "heading",
Expand Down
7 changes: 7 additions & 0 deletions test/fixture/class.config.output.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

- [MyClass](#myclass)
- [getFoo](#getfoo)
- [getUndefined](#getundefined)
- [Hello](#hello)

## MyClass

This is my class, a demo thing.
Expand Down
6 changes: 6 additions & 0 deletions test/fixture/class.output.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

- [MyClass](#myclass)
- [getFoo](#getfoo)
- [getUndefined](#getundefined)

## MyClass

This is my class, a demo thing.
Expand Down
10 changes: 10 additions & 0 deletions test/fixture/class.output.md.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
"type": "html",
"value": "<!-- Generated by documentation.js. Update this documentation by updating the source code. -->"
},
{
"depth": 3,
"type": "heading",
"children": [
{
"type": "text",
"value": "Table of Contents"
}
]
},
{
"depth": 2,
"type": "heading",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

- [document-exported-export-default-object.input](#document-exported-export-default-objectinput)
- [x](#x)

## document-exported-export-default-object.input

## x
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
"type": "html",
"value": "<!-- Generated by documentation.js. Update this documentation by updating the source code. -->"
},
{
"depth": 3,
"type": "heading",
"children": [
{
"type": "text",
"value": "Table of Contents"
}
]
},
{
"depth": 2,
"type": "heading",
Expand Down
4 changes: 4 additions & 0 deletions test/fixture/document-exported-export-default-value.output.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

- [document-exported-export-default-value.input](#document-exported-export-default-valueinput)

## document-exported-export-default-value.input
10 changes: 10 additions & 0 deletions test/fixture/document-exported-export-default-value.output.md.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
"type": "html",
"value": "<!-- Generated by documentation.js. Update this documentation by updating the source code. -->"
},
{
"depth": 3,
"type": "heading",
"children": [
{
"type": "text",
"value": "Table of Contents"
}
]
},
{
"depth": 2,
"type": "heading",
Expand Down
33 changes: 33 additions & 0 deletions test/fixture/document-exported.output.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

- [z](#z)
- [zMethod](#zmethod)
- [x](#x)
- [Class](#class)
- [classMethod](#classmethod)
- [classGetter](#classgetter)
- [classSetter](#classsetter)
- [staticMethod](#staticmethod)
- [staticGetter](#staticgetter)
- [staticSetter](#staticsetter)
- [T5](#t5)
- [y2Default](#y2default)
- [y4](#y4)
- [object](#object)
- [prop](#prop)
- [func](#func)
- [method](#method)
- [getter](#getter)
- [setter](#setter)
- [f1](#f1)
- [f3](#f3)
- [T](#t)
- [T2](#t2)
- [T4](#t4)
- [f4](#f4)
- [o1](#o1)
- [om1](#om1)
- [f5](#f5)
- [o2](#o2)
- [om2](#om2)

## z

### zMethod
Expand Down
10 changes: 10 additions & 0 deletions test/fixture/document-exported.output.md.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
"type": "html",
"value": "<!-- Generated by documentation.js. Update this documentation by updating the source code. -->"
},
{
"depth": 3,
"type": "heading",
"children": [
{
"type": "text",
"value": "Table of Contents"
}
]
},
{
"depth": 2,
"type": "heading",
Expand Down
7 changes: 7 additions & 0 deletions test/fixture/es6-class.output.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

- [Foo](#foo)
- [Bar](#bar)
- [constructor](#constructor)
- [bar](#bar-1)

## Foo

**Extends React.Component**
Expand Down
10 changes: 10 additions & 0 deletions test/fixture/es6-class.output.md.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
"type": "html",
"value": "<!-- Generated by documentation.js. Update this documentation by updating the source code. -->"
},
{
"depth": 3,
"type": "heading",
"children": [
{
"type": "text",
"value": "Table of Contents"
}
]
},
{
"depth": 2,
"type": "heading",
Expand Down
4 changes: 4 additions & 0 deletions test/fixture/es6-default2.output.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

- [es6-default2.input](#es6-default2input)

## es6-default2.input

**Parameters**
Expand Down
10 changes: 10 additions & 0 deletions test/fixture/es6-default2.output.md.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
"type": "html",
"value": "<!-- Generated by documentation.js. Update this documentation by updating the source code. -->"
},
{
"depth": 3,
"type": "heading",
"children": [
{
"type": "text",
"value": "Table of Contents"
}
]
},
{
"depth": 2,
"type": "heading",
Expand Down
6 changes: 6 additions & 0 deletions test/fixture/es6-import.output.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

- [multiplyTwice](#multiplytwice)
- [es6-ext](#es6-ext)
- [simple.input](#simpleinput)

## multiplyTwice

This function returns the number one.
Expand Down
10 changes: 10 additions & 0 deletions test/fixture/es6-import.output.md.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
"type": "html",
"value": "<!-- Generated by documentation.js. Update this documentation by updating the source code. -->"
},
{
"depth": 3,
"type": "heading",
"children": [
{
"type": "text",
"value": "Table of Contents"
}
]
},
{
"depth": 2,
"type": "heading",
Expand Down
Loading

0 comments on commit 611ecbb

Please sign in to comment.