Skip to content

Commit

Permalink
Fix emoji custom encoding for GitHub and GitLab
Browse files Browse the repository at this point in the history
Closes thlorenz#43

Co-authored-by: Robert Preus-MacLaren <[email protected]>
  • Loading branch information
mcornella and Robert Preus-MacLaren committed Sep 23, 2020
1 parent 9b452aa commit 7dac2fe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
22 changes: 11 additions & 11 deletions anchor-markdown-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ function getGithubId(text, repetition) {
text += '-' + repetition;
}

// Strip emojis
text = text.replace(emojiRegex(), '');

return text;
}

Expand Down Expand Up @@ -109,6 +106,15 @@ function getGitlabId(text, repetition) {
}


function customEmojiEncodeURI(uri) {
var newURI = encodeURI(uri.replace(emojiRegex(), ''));

// encodeURI replaces the zero width joiner character
// (used to generate emoji sequences, e.g.Female Construction Worker 👷🏼‍♀️)
// github doesn't URL encode them, so we replace them after url encoding to preserve the zwj character.
return newURI.replace(/%E2%80%8D/g, '%EF%B8%8F');
}

/**
* Generates an anchor for the given header and mode.
*
Expand All @@ -128,20 +134,14 @@ module.exports = function anchorMarkdownHeader(header, mode, repetition, moduleN
switch(mode) {
case 'github.com':
replace = getGithubId;
customEncodeURI = function(uri) {
var newURI = encodeURI(uri);

// encodeURI replaces the zero width joiner character
// (used to generate emoji sequences, e.g.Female Construction Worker 👷🏼‍♀️)
// github doesn't URL encode them, so we replace them after url encoding to preserve the zwj character.
return newURI.replace(/%E2%80%8D/g, '%EF%B8%8F');
};
customEncodeURI = customEmojiEncodeURI;
break;
case 'bitbucket.org':
replace = getBitbucketId;
break;
case 'gitlab.com':
replace = getGitlabId;
customEncodeURI = customEmojiEncodeURI;
break;
case 'nodejs.org':
if (!moduleName) throw new Error('Need module name to generate proper anchor for ' + mode);
Expand Down
8 changes: 7 additions & 1 deletion test/anchor-markdown-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test('\ngenerating anchor in github mode', function (t) {
, [ 'remove exclamation point!', null, '#remove-exclamation-point']
, [ 'remove = sign', null, '#remove--sign']
, [ '`history [pgn | alg]`', null, '#history-pgn--alg']
, [ 'preseve consecutive | = hyphens', null, '#preseve-consecutive---hyphens']
, [ 'preserve consecutive | = hyphens', null, '#preserve-consecutive---hyphens']
, [ 'Demo #1: using the `-s` option', null, '#demo-1-using-the--s-option']
, [ 'class~method', null, '#classmethod']
, [ 'func($event)', null, '#funcevent']
Expand Down Expand Up @@ -135,6 +135,12 @@ test('\ngenerating anchor in gitlab mode', function (t) {
, ['..Ab_c-d. e [anchor](url) ![alt text](url)..', null, '#ab_c-d-e-anchor']
, [ '存在,【中文】;《标点》、符号!的标题?', null, '#%E5%AD%98%E5%9C%A8%E4%B8%AD%E6%96%87%E6%A0%87%E7%82%B9%E7%AC%A6%E5%8F%B7%E7%9A%84%E6%A0%87%E9%A2%98']
, [ 'exists:*table*,_column_', null, '#existstablecolumn']
, [ 'Modules 📦', null, '#modules-']
, [ 'Modu📦les', null, '#modules']
, [ 'Mo📦du📦les', null, '#modules']
, [ '👷🏼‍♀️ Maintenance', null, '#%EF%B8%8F-maintenance']
, [ 'Alarm clock ⏰', null, '#alarm-clock-']
, [ 'Apple Watch ⌚️', null, '#apple-watch-%EF%B8%8F']
].forEach(function (x) { check(x[0], x[1], x[2]) });
t.end();
})
Expand Down

0 comments on commit 7dac2fe

Please sign in to comment.