-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
51 lines (41 loc) · 1.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';
const parseUrl = require('url').parse;
const getUrls = require('get-urls');
const requireDirectory = require('require-directory');
const parsers = requireDirectory(module, './lib', { recurse: false });
const parsersKeys = Object.keys(parsers);
// TODO badges to support:
// gitter https://badges.gitter.im/${user}/${package}.png
// parallelci
// http://www.coverity.com/
// Loose definition of a badge url
// Appveyor is the only that doesn't have any extension or badge on the url. It has /api on the url,
// but this is too generic to be added here, so we playing safe and not applying it.
function isBadgeUrl(url) {
return ['.svg', '.png', '.jpg', '.gif', 'svg=true', 'png=true', 'badge', 'appveyor']
.some(extension => url.indexOf(extension) !== -1);
}
module.exports = readme => {
let urls;
// Separate urls by spaces first
readme = readme || readme.split('http').map(url => `http${url}`).join(' ');
try {
urls = Array.from(getUrls(readme));
} catch (err) {
return [];
}
return urls
.map(url => url && url.split(')')[0]) // Ignore markdown syntax leftovers
.filter(url => !!url && isBadgeUrl(url))
.map(url => {
const parsedUrl = parseUrl(url, true);
let badge;
for (let i = 0; i < parsersKeys.length && !badge; ++i) {
badge = parsers[parsersKeys[i]](parsedUrl);
}
// TODO: Fallback to generic shields.io
// If that is the case we should add a flag indicating that it is a guess.
return badge;
})
.filter(badge => !!badge);
};