diff --git a/.verb.md b/.verb.md index 1720e3a..72c411d 100644 --- a/.verb.md +++ b/.verb.md @@ -206,6 +206,14 @@ Default: Basic non-word character replacement. var str = toc('# Some Article', {slugify: require('uslug')}); ``` +### options.slug_prefix + +Type: `String` + +Default: `` + +Adds `options.slug_prefix` to generated slug. Useful when there is no support in-document anchors and slugs are generated with prefixes. + ### options.bullets Type: `String|Array` @@ -239,4 +247,4 @@ Type: `Boolean` Default: `true` -Strip extraneous HTML tags from heading text before slugifying. This is similar to GitHub markdown behavior. \ No newline at end of file +Strip extraneous HTML tags from heading text before slugifying. This is similar to GitHub markdown behavior. diff --git a/cli.js b/cli.js index 59df22a..2a5aaf2 100755 --- a/cli.js +++ b/cli.js @@ -4,24 +4,33 @@ var fs = require('fs'); var toc = require('./index.js'); var utils = require('./lib/utils'); var args = utils.minimist(process.argv.slice(2), { - boolean: ['i', 'json'] + boolean: ['i', 'json'], + string: ['slug-prefix'] }); if (args._.length !== 1) { console.error([ - 'Usage: markdown-toc [--json] [-i] ', + 'Usage: markdown-toc [--json] [-i] [--slug-prefix ] ', '', - ' input: The markdown file to parse for table of contents,', + ' input: The markdown file to parse for table of contents,', ' or "-" to read from stdin.', '', ' --json: Print the TOC in json format', '', ' -i: Edit the file directly, injecting the TOC at ', - ' (Without this flag, the default is to print the TOC to stdout.)' + ' (Without this flag, the default is to print the TOC to stdout.)', + '', + ' --slug-prefix :', + ' Add to the generated slugs.' ].join('\n')); process.exit(1); } +if (!args.i && !args.json) { + console.error('markdown-toc: you must specify either --json or -i'); + process.exit(1); +} + if (args.i && args.json) { console.error('markdown-toc: you cannot use both --json and -i'); process.exit(1); @@ -36,11 +45,16 @@ var input = process.stdin; if (args._[0] !== '-') input = fs.createReadStream(args._[0]); input.pipe(utils.concat(function(input) { + options = {}; + if (args['slug-prefix']) { + options['slug_prefix'] = args['slug-prefix']; + } + if (args.i) { - var newMarkdown = toc.insert(input.toString()); + var newMarkdown = toc.insert(input.toString(), options); fs.writeFileSync(args._[0], newMarkdown); } else { - var parsed = toc(input.toString()); + var parsed = toc(input.toString(), options); output(parsed); } })); diff --git a/lib/utils.js b/lib/utils.js index 16125e2..bae8319 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -70,6 +70,11 @@ utils.slugify = function(str, options) { if (options.num) { str += '-' + options.num; } + + if (options.slug_prefix) { + str = options.slug_prefix + str; + } + return str; }; diff --git a/test/test.js b/test/test.js index e93e41b..6d8e99b 100644 --- a/test/test.js +++ b/test/test.js @@ -269,6 +269,14 @@ describe('toc', function() { ].join('\n')); }); + it('should add a slug prefix, if defined:', function() { + assert.equal(toc('# AAA\n# BBB\n# CCC\nfoo\nbar\nbaz', {slug_prefix: 'test-'}).content, [ + '- [AAA](#test-aaa)', + '- [BBB](#test-bbb)', + '- [CCC](#test-ccc)' + ].join('\n')); + }); + it('should rotate bullets when there are more levels than bullets defined:', function() { var actual = toc('# AAA\n## BBB\n### CCC', { bullets: ['?']