Skip to content

Commit 96bcc96

Browse files
committed
feat: badge support colorLeft
1 parent e4f9d61 commit 96bcc96

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

docs/content/2.guide/1.features.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ Overrides the default strategy color. You can pass a standard hex code (with or
167167
| **Pure Black** | `.../badge/version/nuxt?color=000000` |
168168
| **Brand Blue** | `.../badge/version/nuxt?color=3b82f6` |
169169

170+
#### `colorLeft`
171+
172+
Overrides the default label color. You can pass a standard hex code (with or without the `#` prefix).
173+
174+
- **Default**: `#0a0a0a`
175+
- **Usage**: `?colorLeft=HEX_CODE`
176+
170177
##### `name`
171178

172179
When set to `true`, this parameter replaces the static category label (like "version" or "downloads/mo") with the actual name of the package. This is useful for brand-focused READMEs.

server/api/registry/badge/[type]/[...pkg].get.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const NPMS_API = 'https://api.npms.io/v2/package'
1414
const QUERY_SCHEMA = v.object({
1515
color: v.optional(v.string()),
1616
name: v.optional(v.string()),
17+
colorLeft: v.optional(v.string()),
1718
})
1819

1920
const COLORS = {
@@ -263,6 +264,7 @@ export default defineCachedEventHandler(
263264

264265
const queryParams = v.safeParse(QUERY_SCHEMA, query)
265266
const userColor = queryParams.success ? queryParams.output.color : undefined
267+
const userColorLeft = queryParams.success ? queryParams.output.colorLeft : undefined
266268
const showName = queryParams.success && queryParams.output.name === 'true'
267269

268270
const badgeTypeResult = v.safeParse(BadgeTypeSchema, typeParam)
@@ -280,6 +282,9 @@ export default defineCachedEventHandler(
280282
const rawColor = userColor ?? strategyResult.color
281283
const finalColor = rawColor?.startsWith('#') ? rawColor : `#${rawColor}`
282284

285+
const rawLeftColor = userColorLeft ?? '#0a0a0a'
286+
const finalLeftColor = rawLeftColor?.startsWith('#') ? rawLeftColor : `#${rawLeftColor}`
287+
283288
const leftWidth = measureTextWidth(finalLabel)
284289
const rightWidth = measureTextWidth(finalValue)
285290
const totalWidth = leftWidth + rightWidth
@@ -291,7 +296,7 @@ export default defineCachedEventHandler(
291296
<rect width="${totalWidth}" height="${height}" rx="3" fill="#fff"/>
292297
</clipPath>
293298
<g clip-path="url(#r)">
294-
<rect width="${leftWidth}" height="${height}" fill="#0a0a0a"/>
299+
<rect width="${leftWidth}" height="${height}" fill="${finalLeftColor}"/>
295300
<rect x="${leftWidth}" width="${rightWidth}" height="${height}" fill="${finalColor}"/>
296301
</g>
297302
<g text-anchor="middle" font-family="'Geist', system-ui, -apple-system, sans-serif" font-size="11">

0 commit comments

Comments
 (0)