Skip to content
Merged
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ anchor('"playerJoined" (player)') === anchor('"playerJoined" (player)', 'github.

## API

`anchor(header[, mode] [, repetition)`
`anchor(header[, mode] [, repetition] [, href])`

```js
/**
Expand All @@ -29,6 +29,7 @@ anchor('"playerJoined" (player)') === anchor('"playerJoined" (player)', 'github.
* @param header {String} The header to be anchored.
* @param mode {String} The anchor mode (github.com|nodejs.org|bitbucket.org|ghost.org|gitlab.com).
* @param repetition {Number} The nth occurrence of this header text, starting with 0. Not required for the 0th instance.
* @param href {String} The href to be used in the anchor.
* @return {String} The header anchor that is compatible with the given mode.
*/
```
10 changes: 8 additions & 2 deletions anchor-markdown-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ function getGitlabId(text, repetition) {
* @param header {String} The header to be anchored.
* @param mode {String} The anchor mode (github.com|nodejs.org|bitbucket.org|ghost.org|gitlab.com).
* @param repetition {Number} The nth occurrence of this header text, starting with 0. Not required for the 0th instance.
* @param href {String} The href to be used in the anchor.
* @return {String} The header anchor that is compatible with the given mode.
*/
module.exports = function anchorMarkdownHeader(header, mode, repetition) {
module.exports = function anchorMarkdownHeader(header, mode, repetition, href) {
mode = mode || 'github.com';
var replace;
var customEncodeURI = encodeURI;
Expand Down Expand Up @@ -152,6 +153,11 @@ module.exports = function anchorMarkdownHeader(header, mode, repetition) {
case 'ghost.org':
replace = getGhostId;
break;
case 'custom':
if(href === undefined){
throw new Error('Missing href');
}
break;
default:
throw new Error('Unknown mode: ' + mode);
}
Expand All @@ -168,7 +174,7 @@ module.exports = function anchorMarkdownHeader(header, mode, repetition) {
return result;
}

var href = replace(customCasing(header.trim()), repetition);
href = href || replace(customCasing(header.trim()), repetition);

return '[' + header + '](#' + customEncodeURI(href) + ')';
};
30 changes: 30 additions & 0 deletions test/anchor-markdown-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,33 @@ test('\ngenerating anchor for non-english header', function (t) {
].forEach(function (x) { check(x[0], x[1], x[2]) });
t.end();
})

test('\ngenerating anchor in custom mode', function (t) {
var actual = anchor('Heading Text', 'custom', 0, 'head');
var expectedAnchor = '[Heading Text](#head)';
t.equal(actual, expectedAnchor);
t.end();
})

test('\ngenerating anchor in github mode with href', function (t) {
var actual = anchor('Heading Text', 'github.com', 1, 'head');
var expectedAnchor = '[Heading Text](#head)';
t.equal(actual, expectedAnchor);
t.end();
})

test('\nmissing input for custom mode throws', function (t) {
t.throws(
() => anchor('Heading Text', 'custom', 0),
{ message: 'Missing href' }
);
t.end();
})

test('\ninvalid input for mode throws', function (t) {
t.throws(
() => anchor('Heading Text', 'random'),
{ message: 'Unknown mode: random' }
);
t.end();
})