From 96bcc96023cd21e65bdb4f63f2d8d6970ae8a0b0 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Mon, 2 Feb 2026 22:51:45 +0800 Subject: [PATCH 1/3] feat: badge support colorLeft --- docs/content/2.guide/1.features.md | 7 +++++++ server/api/registry/badge/[type]/[...pkg].get.ts | 7 ++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/content/2.guide/1.features.md b/docs/content/2.guide/1.features.md index 81eee0564..5a12199f2 100644 --- a/docs/content/2.guide/1.features.md +++ b/docs/content/2.guide/1.features.md @@ -167,6 +167,13 @@ Overrides the default strategy color. You can pass a standard hex code (with or | **Pure Black** | `.../badge/version/nuxt?color=000000` | | **Brand Blue** | `.../badge/version/nuxt?color=3b82f6` | +#### `colorLeft` + +Overrides the default label color. You can pass a standard hex code (with or without the `#` prefix). + +- **Default**: `#0a0a0a` +- **Usage**: `?colorLeft=HEX_CODE` + ##### `name` 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. diff --git a/server/api/registry/badge/[type]/[...pkg].get.ts b/server/api/registry/badge/[type]/[...pkg].get.ts index 8f6a56d45..bafbeecb2 100644 --- a/server/api/registry/badge/[type]/[...pkg].get.ts +++ b/server/api/registry/badge/[type]/[...pkg].get.ts @@ -14,6 +14,7 @@ const NPMS_API = 'https://api.npms.io/v2/package' const QUERY_SCHEMA = v.object({ color: v.optional(v.string()), name: v.optional(v.string()), + colorLeft: v.optional(v.string()), }) const COLORS = { @@ -263,6 +264,7 @@ export default defineCachedEventHandler( const queryParams = v.safeParse(QUERY_SCHEMA, query) const userColor = queryParams.success ? queryParams.output.color : undefined + const userColorLeft = queryParams.success ? queryParams.output.colorLeft : undefined const showName = queryParams.success && queryParams.output.name === 'true' const badgeTypeResult = v.safeParse(BadgeTypeSchema, typeParam) @@ -280,6 +282,9 @@ export default defineCachedEventHandler( const rawColor = userColor ?? strategyResult.color const finalColor = rawColor?.startsWith('#') ? rawColor : `#${rawColor}` + const rawLeftColor = userColorLeft ?? '#0a0a0a' + const finalLeftColor = rawLeftColor?.startsWith('#') ? rawLeftColor : `#${rawLeftColor}` + const leftWidth = measureTextWidth(finalLabel) const rightWidth = measureTextWidth(finalValue) const totalWidth = leftWidth + rightWidth @@ -291,7 +296,7 @@ export default defineCachedEventHandler( - + From 51fcd1beb73e8352be608c986c52db34309a759f Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Tue, 3 Feb 2026 11:27:03 +0800 Subject: [PATCH 2/3] feat: update --- docs/content/2.guide/1.features.md | 26 +++++++++---------- .../api/registry/badge/[type]/[...pkg].get.ts | 8 +++--- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/docs/content/2.guide/1.features.md b/docs/content/2.guide/1.features.md index 81b675f40..3e6294a76 100644 --- a/docs/content/2.guide/1.features.md +++ b/docs/content/2.guide/1.features.md @@ -154,25 +154,25 @@ You can add custom npmx badges to your markdown files using the following syntax You can further customize your badges by appending query parameters to the badge URL. -##### `color` +#### `colorA` -Overrides the default strategy color. You can pass a standard hex code (with or without the `#` prefix). +Overrides the default label color. You can pass a standard hex code (with or without the `#` prefix). -- **Default**: Depends on the badge type (e.g., version is blue, downloads are orange). -- **Usage**: `?color=HEX_CODE` +- **Default**: `#0a0a0a` +- **Usage**: `?colorA=HEX_CODE` -| Example | URL | -| :------------- | :------------------------------------ | -| **Hot Pink** | `.../badge/version/nuxt?color=ff69b4` | -| **Pure Black** | `.../badge/version/nuxt?color=000000` | -| **Brand Blue** | `.../badge/version/nuxt?color=3b82f6` | +##### `colorB` -#### `colorLeft` +Overrides the default strategy color. You can pass a standard hex code (with or without the `#` prefix). -Overrides the default label color. You can pass a standard hex code (with or without the `#` prefix). +- **Default**: Depends on the badge type (e.g., version is blue, downloads are orange). +- **Usage**: `?colorB=HEX_CODE` -- **Default**: `#0a0a0a` -- **Usage**: `?colorLeft=HEX_CODE` +| Example | URL | +| :------------- | :------------------------------------- | +| **Hot Pink** | `.../badge/version/nuxt?colorB=ff69b4` | +| **Pure Black** | `.../badge/version/nuxt?colorB=000000` | +| **Brand Blue** | `.../badge/version/nuxt?colorB=3b82f6` | ##### `name` diff --git a/server/api/registry/badge/[type]/[...pkg].get.ts b/server/api/registry/badge/[type]/[...pkg].get.ts index bafbeecb2..74e8d47c7 100644 --- a/server/api/registry/badge/[type]/[...pkg].get.ts +++ b/server/api/registry/badge/[type]/[...pkg].get.ts @@ -12,9 +12,9 @@ const BUNDLEPHOBIA_API = 'https://bundlephobia.com/api/size' const NPMS_API = 'https://api.npms.io/v2/package' const QUERY_SCHEMA = v.object({ - color: v.optional(v.string()), + colorA: v.optional(v.string()), name: v.optional(v.string()), - colorLeft: v.optional(v.string()), + colorB: v.optional(v.string()), }) const COLORS = { @@ -263,8 +263,8 @@ export default defineCachedEventHandler( }) const queryParams = v.safeParse(QUERY_SCHEMA, query) - const userColor = queryParams.success ? queryParams.output.color : undefined - const userColorLeft = queryParams.success ? queryParams.output.colorLeft : undefined + const userColor = queryParams.success ? queryParams.output.colorB : undefined + const userColorLeft = queryParams.success ? queryParams.output.colorA : undefined const showName = queryParams.success && queryParams.output.name === 'true' const badgeTypeResult = v.safeParse(BadgeTypeSchema, typeParam) From f15e3aa72a4043af590b10601fa8d1f67f3d64e5 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Tue, 3 Feb 2026 14:19:52 +0800 Subject: [PATCH 3/3] test: update --- test/e2e/badge.spec.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/test/e2e/badge.spec.ts b/test/e2e/badge.spec.ts index 9254ec910..c7dbb74c6 100644 --- a/test/e2e/badge.spec.ts +++ b/test/e2e/badge.spec.ts @@ -102,9 +102,17 @@ test.describe('badge API', () => { }) }) - test('custom color parameter is applied to SVG', async ({ page, baseURL }) => { + test('custom colorA parameter is applied to SVG', async ({ page, baseURL }) => { + const customColor = '00ff00' + const url = toLocalUrl(baseURL, `/api/registry/badge/version/nuxt?colorA=${customColor}`) + const { body } = await fetchBadge(page, url) + + expect(body).toContain(`fill="#${customColor}"`) + }) + + test('custom colorB parameter is applied to SVG', async ({ page, baseURL }) => { const customColor = 'ff69b4' - const url = toLocalUrl(baseURL, `/api/registry/badge/version/nuxt?color=${customColor}`) + const url = toLocalUrl(baseURL, `/api/registry/badge/version/nuxt?colorB=${customColor}`) const { body } = await fetchBadge(page, url) expect(body).toContain(`fill="#${customColor}"`)