Skip to content

Commit

Permalink
Merge pull request #77 from groupon/badges
Browse files Browse the repository at this point in the history
refactor: add brand logos to badges & detect Github links
  • Loading branch information
aaarichter authored Feb 27, 2021
2 parents 0ff554a + d2dbbf2 commit 17ea2a8
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 85 deletions.
60 changes: 44 additions & 16 deletions lib/steps/readme-badges.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ const path = require('path');
const fs = require('fs');

/**
* @param {string} url
* @param {URL} urlObj
* @param {number?} sections
* @returns {string}
*/
function getLastPath(url) {
if (url.endsWith('/')) {
url = url.substring(0, url.length - 1);
function getLastPaths(urlObj, sections = 1) {
let { pathname } = urlObj;
if (pathname.endsWith('/')) {
pathname = pathname.substring(0, pathname.length - 1);
}
return new URL(url).pathname.split('/').pop();
return pathname
.split('/')
.slice(-1 * sections)
.join('/');
}

/**
Expand All @@ -55,13 +60,26 @@ function getLastPath(url) {
*/
function extractUrl(key, value) {
let url = value;
// slack
if (/([\w-]+)\.(slack)\.com(\/messages\/([\w]+))?/g.test(value)) {
key = 'slack';
value = getLastPath(value);
} else if (/^https?:\/\/jira\.[\w]+[.\w]+/g.test(value)) {
key = 'jira';
value = getLastPath(value);
let urlObject;
try {
urlObject = new URL(value);
} catch (e) {
/**/
}

if (urlObject) {
const { host } = urlObject;
// slack
if (host.includes('slack')) {
key = 'slack';
value = getLastPaths(urlObject, 1);
} else if (host.includes('jira')) {
key = 'jira';
value = getLastPaths(urlObject, 1);
} else if (host.includes('github')) {
key = 'github';
value = getLastPaths(urlObject, 3);
}
} else if (/@/.test(value) && !value.startsWith('mailto:')) {
url = `mailto:${value}`;
}
Expand All @@ -76,6 +94,15 @@ function extractUrl(key, value) {
function encode(str) {
return encodeURIComponent(str).replace(/-/g, '--');
}

function getLogoId(label) {
switch (label) {
case 'node':
return 'node.js';
default:
return label;
}
}
/**
* @param {string} label
* @param {string} message
Expand All @@ -85,7 +112,7 @@ function encode(str) {
function shieldsBadge(label, message, color) {
return `![nlm-${label}](https://img.shields.io/badge/${encode(
label
)}-${encode(message)}-${color})`;
)}-${encode(message)}-${color}?logo=${getLogoId(label)}&logoColor=white)`;
}

/**
Expand Down Expand Up @@ -219,19 +246,20 @@ function codeCoverageBadges(pkg, options, cwd) {
* @returns {RegExp}
*/
function getShieldRegexp(key) {
// /[\[ !]*\[nlm-[\w\s-_s]+]\(https:\/\/img\.shields\.io\/badge\/[\w:\/\-@.%=]+-[\w\S]+-\w+\)]?(\([\w:\/\-@.%=]+\))?/
// /[\[ !]*\[nlm-[\w\s-_s]+]\(https:\/\/img\.shields\.io\/badge\/[\w:\/\-@.%=]+-[\w\S]+-\w+\?[\w\-_.&=]+\)]?(\([\w:\/\-@.%=]+\))?/

const id = /[\w\s-_s]+/;
const shieldsUri = /https:\/\/img\.shields\.io\/badge/;
const message = /[\w\S]+/;
const color = /\w+/;
const encoded = /[\w:\/\-@.%=]+/;
const url = /[\w:\/\-@.%=]+/;
const logo = /\?[\w\-_.&=]+/;

const label = key ? encode(key) : encoded.source;

return new RegExp(
`[\\[ !]*\\[nlm-${id.source}]\\(${shieldsUri.source}\\/${label}-${message.source}-${color.source}\\)]?(\\(${url.source}\\))?`,
`[\\[ !]*\\[nlm-${id.source}]\\(${shieldsUri.source}\\/${label}-${message.source}-${color.source}${logo.source}\\)]?(\\(${url.source}\\))?`,
'g'
);
}
Expand All @@ -254,7 +282,7 @@ async function generateBadges(cwd, pkg, options) {
content = '';
}

const STARTS_WITH_IMAGE_REGEXP = /^[\[ ]?!\[[\w\s-_s]+]\([\w:\/\-.%=]+\)]?(\([\w:\/\-.%=@]+\))?/;
const STARTS_WITH_IMAGE_REGEXP = /^[\[ ]?!\[[\w\s-_s]+]\([\w:\/\-.%=]+\?[\w\-_.&=]+\)]?(\([\w:\/\-.%=@]+\))?/;
const LAST_BADGES_REGEXP = new RegExp(
`${STARTS_WITH_IMAGE_REGEXP.source}([\\s\\n]*${getShieldRegexp().source})*`,
'g'
Expand Down
80 changes: 44 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"minimist": "^1.2.5",
"rc": "^1.2.8",
"semver": "^7.3.4",
"string.prototype.replaceall": "^1.0.4"
"string.prototype.replaceall": "^1.0.5"
},
"devDependencies": {
"c8": "^7.6.0",
Expand Down
Loading

0 comments on commit 17ea2a8

Please sign in to comment.