You've already forked dynamic-badges-action
This adds the posibility of saving an SVG badge generated by the same shields.io dirictly to the gist. Instead of prepering a JSON file to be sent to their service, we use their library directly, which outputs an SVG file that we can save to the user’s gist. Filenames ending in `.svg` will use this library automatically. Additionally there is a major refactoring where the older `node:http` library has been swapped out for `fetch`. Also swap from node 16 to node 20 fixes #24
64 lines
1.4 KiB
JavaScript
64 lines
1.4 KiB
JavaScript
'use strict'
|
|
|
|
const { normalizeColor, toSvgColor } = require('./color')
|
|
const badgeRenderers = require('./badge-renderers')
|
|
const { stripXmlWhitespace } = require('./xml')
|
|
|
|
/*
|
|
note: makeBadge() is fairly thinly wrapped so if we are making changes here
|
|
it is likely this will impact on the package's public interface in index.js
|
|
*/
|
|
module.exports = function makeBadge({
|
|
format,
|
|
style = 'flat',
|
|
label,
|
|
message,
|
|
color,
|
|
labelColor,
|
|
logo,
|
|
logoPosition,
|
|
logoWidth,
|
|
links = ['', ''],
|
|
}) {
|
|
// String coercion and whitespace removal.
|
|
label = `${label}`.trim()
|
|
message = `${message}`.trim()
|
|
|
|
// This ought to be the responsibility of the server, not `makeBadge`.
|
|
if (format === 'json') {
|
|
return JSON.stringify({
|
|
label,
|
|
message,
|
|
logoWidth,
|
|
// Only call normalizeColor for the JSON case: this is handled
|
|
// internally by toSvgColor in the SVG case.
|
|
color: normalizeColor(color),
|
|
labelColor: normalizeColor(labelColor),
|
|
link: links,
|
|
name: label,
|
|
value: message,
|
|
})
|
|
}
|
|
|
|
const render = badgeRenderers[style]
|
|
if (!render) {
|
|
throw new Error(`Unknown badge style: '${style}'`)
|
|
}
|
|
|
|
logoWidth = +logoWidth || (logo ? 14 : 0)
|
|
|
|
return stripXmlWhitespace(
|
|
render({
|
|
label,
|
|
message,
|
|
links,
|
|
logo,
|
|
logoPosition,
|
|
logoWidth,
|
|
logoPadding: logo && label.length ? 3 : 0,
|
|
color: toSvgColor(color),
|
|
labelColor: toSvgColor(labelColor),
|
|
})
|
|
)
|
|
}
|