diff --git a/docs/content/2.guide/1.features.md b/docs/content/2.guide/1.features.md index 04557f0f8..4b13cb7f2 100644 --- a/docs/content/2.guide/1.features.md +++ b/docs/content/2.guide/1.features.md @@ -154,18 +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 label color. You can pass a standard hex code (with or without the `#` prefix). + +- **Default**: `#0a0a0a` +- **Usage**: `?colorA=HEX_CODE` + +##### `colorB` Overrides the default strategy 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` +- **Usage**: `?colorB=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` | +| 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 8f6a56d45..74e8d47c7 100644 --- a/server/api/registry/badge/[type]/[...pkg].get.ts +++ b/server/api/registry/badge/[type]/[...pkg].get.ts @@ -12,8 +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()), + colorB: v.optional(v.string()), }) const COLORS = { @@ -262,7 +263,8 @@ export default defineCachedEventHandler( }) const queryParams = v.safeParse(QUERY_SCHEMA, query) - const userColor = queryParams.success ? queryParams.output.color : 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) @@ -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( - + 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}"`)