From c9d94a65bf586669542c193520887bed2276acc2 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Fri, 20 Mar 2026 00:51:01 +1100 Subject: [PATCH 1/3] Add custom mode --- README.md | 3 ++- anchor-markdown-header.js | 10 ++++++++-- test/anchor-markdown-header.js | 12 ++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ab9cdba..5446b5b 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ anchor('"playerJoined" (player)') === anchor('"playerJoined" (player)', 'github. ## API -`anchor(header[, mode] [, repetition)` +`anchor(header[, mode] [, repetition] [, href])` ```js /** @@ -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. */ ``` diff --git a/anchor-markdown-header.js b/anchor-markdown-header.js index 506b529..2ae3706 100644 --- a/anchor-markdown-header.js +++ b/anchor-markdown-header.js @@ -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; @@ -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); } @@ -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) + ')'; }; diff --git a/test/anchor-markdown-header.js b/test/anchor-markdown-header.js index aa15697..e390417 100644 --- a/test/anchor-markdown-header.js +++ b/test/anchor-markdown-header.js @@ -187,3 +187,15 @@ 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('custom', 'Heading Text', 0, 'head'); + var expectedAnchor = '[Heading Text](#head)'; + t.equal(actual, expectedAnchor); +}) + +test('\ngenerating anchor in github mode with href', function (t) { + var actual = anchor('github', 'Heading Text', 1, 'head'); + var expectedAnchor = '[Heading Text](#head)'; + t.equal(actual, expectedAnchor); +}) \ No newline at end of file From e1492fb673673a02583e6c2ff15cc4c997ac647a Mon Sep 17 00:00:00 2001 From: James Thompson Date: Fri, 20 Mar 2026 01:11:29 +1100 Subject: [PATCH 2/3] Fix tests --- test/anchor-markdown-header.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/test/anchor-markdown-header.js b/test/anchor-markdown-header.js index e390417..0afd473 100644 --- a/test/anchor-markdown-header.js +++ b/test/anchor-markdown-header.js @@ -189,13 +189,23 @@ test('\ngenerating anchor for non-english header', function (t) { }) test('\ngenerating anchor in custom mode', function (t) { - var actual = anchor('custom', 'Heading Text', 0, 'head'); + 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('github', 'Heading Text', 1, 'head'); + var actual = anchor('Heading Text', 'github.com', 1, 'head'); var expectedAnchor = '[Heading Text](#head)'; t.equal(actual, expectedAnchor); -}) \ No newline at end of file + t.end(); +}) + +test('\nmissing input for custom mode throws', function (t) { + t.throws( + () => anchor('Heading Text', 'github.com', 0), + { message: 'Missing href' } + ); + t.end(); +}) From b9de55a83e5dc6192227ca3e560dd09bd5c680c0 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Fri, 20 Mar 2026 01:37:28 +1100 Subject: [PATCH 3/3] More tests --- test/anchor-markdown-header.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/anchor-markdown-header.js b/test/anchor-markdown-header.js index 0afd473..74a4482 100644 --- a/test/anchor-markdown-header.js +++ b/test/anchor-markdown-header.js @@ -204,8 +204,16 @@ test('\ngenerating anchor in github mode with href', function (t) { test('\nmissing input for custom mode throws', function (t) { t.throws( - () => anchor('Heading Text', 'github.com', 0), + () => 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(); +})