Skip to content

Commit

Permalink
Add option for IDs to have a prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
randomairborne committed Oct 4, 2024
1 parent 2515cf9 commit 7d0d04f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
27 changes: 15 additions & 12 deletions badge-maker/lib/badge-renderers.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class Badge {
logoPadding,
color = '#4c1',
labelColor,
idPrefix = '',
}) {
const horizPadding = 5
const hasLogo = !!logo
Expand Down Expand Up @@ -178,6 +179,7 @@ class Badge {
this.label = label
this.message = message
this.accessibleText = accessibleText
this.idPrefix = idPrefix

this.logoElement = getLogoElement({
logo,
Expand Down Expand Up @@ -286,7 +288,7 @@ class Badge {
},
}),
],
attrs: { id: 'r' },
attrs: { id: `${this.idPrefix}r` },
})
}

Expand All @@ -313,7 +315,7 @@ class Badge {
attrs: {
width: this.width,
height: this.constructor.height,
fill: 'url(#s)',
fill: `url(#${this.idPrefix}s)`,
},
})
const content = withGradient
Expand Down Expand Up @@ -379,14 +381,14 @@ class Plastic extends Badge {
attrs: { offset: 1, 'stop-color': '#000', 'stop-opacity': '.5' },
}),
],
attrs: { id: 's', x2: 0, y2: '100%' },
attrs: { id: `${this.idPrefix}s`, x2: 0, y2: '100%' },
})

const clipPath = this.getClipPathElement(4)

const backgroundGroup = this.getBackgroundGroupElement({
withGradient: true,
attrs: { 'clip-path': 'url(#r)' },
attrs: { 'clip-path': `url(#${this.idPrefix}r)` },
})

return renderBadge(
Expand Down Expand Up @@ -428,14 +430,14 @@ class Flat extends Badge {
attrs: { offset: 1, 'stop-opacity': '.1' },
}),
],
attrs: { id: 's', x2: 0, y2: '100%' },
attrs: { id: `${this.idPrefix}s`, x2: 0, y2: '100%' },
})

const clipPath = this.getClipPathElement(3)

const backgroundGroup = this.getBackgroundGroupElement({
withGradient: true,
attrs: { 'clip-path': 'url(#r)' },
attrs: { 'clip-path': `url(#${this.idPrefix}r)` },
})

return renderBadge(
Expand Down Expand Up @@ -492,6 +494,7 @@ function social({
logoPadding,
color = '#4c1',
labelColor = '#555',
idPrefix = '',
}) {
// Social label is styled with a leading capital. Convert to caps here so
// width can be measured using the correct characters.
Expand Down Expand Up @@ -565,9 +568,9 @@ function social({
const rect = new XmlElement({
name: 'rect',
attrs: {
id: 'llink',
id: `${idPrefix}llink`,
stroke: '#d5d5d5',
fill: 'url(#a)',
fill: `url(#${idPrefix}a)`,
x: '.5',
y: '.5',
width: labelRectWidth,
Expand Down Expand Up @@ -640,7 +643,7 @@ function social({
name: 'text',
content: [message],
attrs: {
id: 'rlink',
id: `${idPrefix}rlink`,
x: messageTextX,
y: 140,
transform: FONT_SCALE_DOWN_VALUE,
Expand All @@ -660,7 +663,7 @@ function social({
const style = new XmlElement({
name: 'style',
content: [
'a:hover #llink{fill:url(#b);stroke:#ccc}a:hover #rlink{fill:#4183c4}',
`a:hover #${idPrefix}llink{fill:url(#${idPrefix}b);stroke:#ccc}a:hover #${idPrefix}rlink{fill:#4183c4}`,
],
})
const gradients = new ElementList({
Expand All @@ -681,7 +684,7 @@ function social({
attrs: { offset: 1, 'stop-opacity': '.1' },
}),
],
attrs: { id: 'a', x2: 0, y2: '100%' },
attrs: { id: `${idPrefix}a`, x2: 0, y2: '100%' },
}),
new XmlElement({
name: 'linearGradient',
Expand All @@ -695,7 +698,7 @@ function social({
attrs: { offset: 1, 'stop-opacity': '.1' },
}),
],
attrs: { id: 'b', x2: 0, y2: '100%' },
attrs: { id: `${idPrefix}b`, x2: 0, y2: '100%' },
}),
],
})
Expand Down
7 changes: 7 additions & 0 deletions badge-maker/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ function _validate(format) {
`Field \`style\` must be one of (${styleValues.toString()})`,
)
}
if ('idPrefix' in format && /^[a-zA-Z0-9\-_]+$/.test(format.idPrefix)) {
throw new ValidationError(
'field `idPrefix` must contain only numbers, letters, -, and _',
)
}
}

function _clean(format) {
Expand All @@ -63,6 +68,7 @@ function _clean(format) {
'style',
'logoBase64',
'links',
'idPrefix',
]

const cleaned = {}
Expand Down Expand Up @@ -95,6 +101,7 @@ function _clean(format) {
* @param {string} format.style (Optional) Visual style (e.g: 'flat')
* @param {string} format.logoBase64 (Optional) Logo data URL
* @param {Array} format.links (Optional) Links array (e.g: ['https://example.com', 'https://example.com'])
* @param {string} format.idPrefix (Optional) Prefix for IDs, e.g. 1, 2, and 3 for three invocations that will be used on the same page.
* @returns {string} Badge in SVG format
* @see https://github.com/badges/shields/tree/master/badge-maker/README.md
*/
Expand Down
3 changes: 3 additions & 0 deletions badge-maker/lib/make-badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = function makeBadge({
logoSize,
logoWidth,
links = ['', ''],
idPrefix,
}) {
// String coercion and whitespace removal.
label = `${label}`.trim()
Expand All @@ -38,6 +39,7 @@ module.exports = function makeBadge({
link: links,
name: label,
value: message,
idPrefix,
})
}

Expand All @@ -59,6 +61,7 @@ module.exports = function makeBadge({
logoPadding: logo && label.length ? 3 : 0,
color: toSvgColor(color),
labelColor: toSvgColor(labelColor),
idPrefix,
}),
)
}

0 comments on commit 7d0d04f

Please sign in to comment.