Skip to content

add slug prefix #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion .verb.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -239,4 +247,4 @@ Type: `Boolean`

Default: `true`

Strip extraneous HTML tags from heading text before slugifying. This is similar to GitHub markdown behavior.
Strip extraneous HTML tags from heading text before slugifying. This is similar to GitHub markdown behavior.
26 changes: 20 additions & 6 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] <input> ',
'Usage: markdown-toc [--json] [-i] [--slug-prefix <prefix>] <input> ',
'',
' 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 <input> file directly, injecting the TOC at <!-- toc -->',
' (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 <prefix>:',
' Add <prefix> 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);
Expand All @@ -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);
}
}));
Expand Down
5 changes: 5 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: ['?']
Expand Down