Skip to content

feat: add shields.io-style badges#1487

Draft
wojtekmaj wants to merge 2 commits intonpmx-dev:mainfrom
wojtekmaj:shieldsio
Draft

feat: add shields.io-style badges#1487
wojtekmaj wants to merge 2 commits intonpmx-dev:mainfrom
wojtekmaj:shieldsio

Conversation

@wojtekmaj
Copy link
Contributor

@wojtekmaj wojtekmaj commented Feb 14, 2026

Depends on #1486

This PR implements a new, optional parameter in badges API: style. By passing style=shieldsio we change the appearance of the generated shields from the default one to custom one.

Default:
default

Default with custom color:
defaultcolor

shields.io-style:
shieldsio

shields.io-style with custom color:
shieldsiocolor

Copilot AI review requested due to automatic review settings February 14, 2026 00:19
@vercel
Copy link

vercel bot commented Feb 14, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
npmx.dev Ready Ready Preview, Comment Feb 14, 2026 0:45am
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
docs.npmx.dev Ignored Ignored Preview Feb 14, 2026 0:45am
npmx-lunaria Ignored Ignored Feb 14, 2026 0:45am

Request Review

@codecov
Copy link

codecov bot commented Feb 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new optional style parameter to the badge API that allows users to switch between the default badge style and a shields.io-compatible style. The implementation introduces accurate text measurement using the @napi-rs/canvas library with fallback to character-based estimation.

Changes:

  • Added @napi-rs/canvas dependency for accurate text width measurement
  • Implemented two badge rendering functions: renderDefaultBadgeSvg and renderShieldsBadgeSvg
  • Added style query parameter validation with values 'default' and 'shieldsio'

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 8 comments.

File Description
package.json Added @napi-rs/canvas dependency (v0.1.92)
pnpm-lock.yaml Lockfile updates for @napi-rs/canvas and its platform-specific binaries
server/api/registry/badge/[type]/[...pkg].get.ts Implemented style parameter, canvas-based text measurement, and two SVG rendering functions
test/e2e/badge.spec.ts Added E2E tests for default and shieldsio style parameters
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

Comment on lines 64 to 66
function OLD_measureTextWidth(text: string): number {
return Math.max(MIN_BADGE_TEXT_WIDTH, Math.round(text.length * CHAR_WIDTH) + BADGE_PADDING_X * 2)
}
Copy link

Copilot AI Feb 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function name OLD_measureTextWidth suggests this is temporary legacy code. While keeping backward compatibility fallback is appropriate, the naming convention with all caps and underscore prefix is inconsistent with the codebase conventions (camelCase). Consider renaming to something like measureTextWidthFallback or legacyMeasureTextWidth to better communicate its purpose.

Copilot uses AI. Check for mistakes.
This PR improves badge appearance by using a more accurate method to measure text width. Instead of relying on a predefined character width, it utilizes the Canvas API to measure the actual width of the text, resulting in better alignment and spacing for badges with varying character widths.

Speaking in code, this is how the old and new methods compare:
{ label: 'iiiiiiiiii', leftWidthOld: 86, leftWidth: 41 }
{ label: 'wwwwwwwwww', leftWidthOld: 86, leftWidth: 97 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants