Skip to content

Commit dc06076

Browse files
feat: Add custom mode (#63)
1 parent b16a0b5 commit dc06076

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ anchor('"playerJoined" (player)') === anchor('"playerJoined" (player)', 'github.
2020

2121
## API
2222

23-
`anchor(header[, mode] [, repetition)`
23+
`anchor(header[, mode] [, repetition] [, href])`
2424

2525
```js
2626
/**
@@ -29,6 +29,7 @@ anchor('"playerJoined" (player)') === anchor('"playerJoined" (player)', 'github.
2929
* @param header {String} The header to be anchored.
3030
* @param mode {String} The anchor mode (github.com|nodejs.org|bitbucket.org|ghost.org|gitlab.com).
3131
* @param repetition {Number} The nth occurrence of this header text, starting with 0. Not required for the 0th instance.
32+
* @param href {String} The href to be used in the anchor.
3233
* @return {String} The header anchor that is compatible with the given mode.
3334
*/
3435
```

anchor-markdown-header.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ function getGitlabId(text, repetition) {
116116
* @param header {String} The header to be anchored.
117117
* @param mode {String} The anchor mode (github.com|nodejs.org|bitbucket.org|ghost.org|gitlab.com).
118118
* @param repetition {Number} The nth occurrence of this header text, starting with 0. Not required for the 0th instance.
119+
* @param href {String} The href to be used in the anchor.
119120
* @return {String} The header anchor that is compatible with the given mode.
120121
*/
121-
module.exports = function anchorMarkdownHeader(header, mode, repetition) {
122+
module.exports = function anchorMarkdownHeader(header, mode, repetition, href) {
122123
mode = mode || 'github.com';
123124
var replace;
124125
var customEncodeURI = encodeURI;
@@ -152,6 +153,11 @@ module.exports = function anchorMarkdownHeader(header, mode, repetition) {
152153
case 'ghost.org':
153154
replace = getGhostId;
154155
break;
156+
case 'custom':
157+
if(href === undefined){
158+
throw new Error('Missing href');
159+
}
160+
break;
155161
default:
156162
throw new Error('Unknown mode: ' + mode);
157163
}
@@ -168,7 +174,7 @@ module.exports = function anchorMarkdownHeader(header, mode, repetition) {
168174
return result;
169175
}
170176

171-
var href = replace(customCasing(header.trim()), repetition);
177+
href = href || replace(customCasing(header.trim()), repetition);
172178

173179
return '[' + header + '](#' + customEncodeURI(href) + ')';
174180
};

test/anchor-markdown-header.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,33 @@ test('\ngenerating anchor for non-english header', function (t) {
187187
].forEach(function (x) { check(x[0], x[1], x[2]) });
188188
t.end();
189189
})
190+
191+
test('\ngenerating anchor in custom mode', function (t) {
192+
var actual = anchor('Heading Text', 'custom', 0, 'head');
193+
var expectedAnchor = '[Heading Text](#head)';
194+
t.equal(actual, expectedAnchor);
195+
t.end();
196+
})
197+
198+
test('\ngenerating anchor in github mode with href', function (t) {
199+
var actual = anchor('Heading Text', 'github.com', 1, 'head');
200+
var expectedAnchor = '[Heading Text](#head)';
201+
t.equal(actual, expectedAnchor);
202+
t.end();
203+
})
204+
205+
test('\nmissing input for custom mode throws', function (t) {
206+
t.throws(
207+
() => anchor('Heading Text', 'custom', 0),
208+
{ message: 'Missing href' }
209+
);
210+
t.end();
211+
})
212+
213+
test('\ninvalid input for mode throws', function (t) {
214+
t.throws(
215+
() => anchor('Heading Text', 'random'),
216+
{ message: 'Unknown mode: random' }
217+
);
218+
t.end();
219+
})

0 commit comments

Comments
 (0)