Skip to content
Closed
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
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@ by github or other sites via a command line flag.
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*

- [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)
- [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 maximum heading level for TOC entries](#specifying-a-maximum-heading-level-for-toc-entries)
- [Printing to stdout](#printing-to-stdout)
- [Usage as a `git` hook](#usage-as-a-git-hook)
- [Docker image](#docker-image)
- [DocToc ](#doctoc-)
- [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 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 -->

> This fork adds MDX support.

## Installation

Expand Down
7 changes: 4 additions & 3 deletions doctoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPref
var transformed = files
.map(function (x) {
var content = fs.readFileSync(x.path, 'utf8')
, result = transform(content, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll, updateOnly);
, mdx = path.extname(x.path)
, result = transform(content, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll, updateOnly, mdx);
result.path = x.path;
return result;
});
Expand Down Expand Up @@ -102,8 +103,8 @@ var title = argv.t || argv.title;
var notitle = argv.T || argv.notitle;
var entryPrefix = argv.entryprefix || '-';
var processAll = argv.all;
var stdOut = argv.s || argv.stdout
var updateOnly = argv.u || argv['update-only']
var stdOut = argv.s || argv.stdout;
var updateOnly = argv.u || argv['update-only'];

var maxHeaderLevel = argv.m || argv.maxlevel;
if (maxHeaderLevel && isNaN(maxHeaderLevel) || maxHeaderLevel < 0) { console.error('Max. heading level specified is not a positive number: ' + maxHeaderLevel), printUsageAndExit(true); }
Expand Down
27 changes: 22 additions & 5 deletions lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@ 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 -->'
, mdx = false;


function matchesStart(line) {
return (/<!-- START doctoc /).test(line);
if(mdx) {
return (/{\/\* START doctoc /).test(line);
} else {
return (/<!-- START doctoc /).test(line);
}
}

function matchesEnd(line) {
return (/<!-- END doctoc /).test(line);
if(mdx) {
return (/{\/\* END doctoc /).test(line);
} else {
return (/<!-- END doctoc /).test(line);
}
}

function notNull(x) { return x !== null; }
Expand All @@ -31,7 +40,6 @@ function isString(y) {
return typeof y === 'string';
}


function getMarkdownHeaders (lines, maxHeaderLevel) {
function extractText (header) {
return header.children
Expand Down Expand Up @@ -108,7 +116,16 @@ function determineTitle(title, notitle, lines, info) {
return info.hasStart ? lines[info.startIdx + 2] : defaultTitle;
}

exports = module.exports = function transform(content, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll, updateOnly) {
exports = module.exports = function transform(content, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll, updateOnly, mdx) {
mdx = mdx || false;

if(mdx) {
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 */}'
}

if (content.indexOf(skipTag) !== -1) return { transformed: false };

mode = mode || 'github.com';
Expand Down
Loading