Skip to content

Commit

Permalink
refactor: improve badges
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 7, 2024
1 parent 68547e6 commit db2fb78
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# 🤖 automd

<!-- automd:badges labelColor="f0db4f" color="18181b" -->
<!-- automd:badges color=yellow -->

[![npm version](https://img.shields.io/npm/v/automd?color=18181b&labelColor=f0db4f)](https://npmjs.com/package/automd)
[![npm downloads](https://img.shields.io/npm/dm/automd?color=18181b&labelColor=f0db4f)](https://npmjs.com/package/automd)
[![npm version](https://flat.badgen.net/npm/v/automd?color=yellow)](https://npmjs.com/package/automd)
[![npm downloads](https://flat.badgen.net/npm/dm/automd?color=yellow)](https://npmjs.com/package/automd)

<!-- /automd -->

Expand Down Expand Up @@ -181,17 +181,17 @@ The `badges` generator generates badges for the latest npm version, npm download

#### Usage

<!-- automd:badges name=defu codecov bundlephobia -->
<!-- automd:badges color=yellow name=defu codecov bundlephobia -->
<!-- /automd -->

**Updated Result:**

<!-- automd:badges name=defu codecov bundlephobia -->
<!-- automd:badges color=yellow name=defu codecov bundlephobia -->

[![npm version](https://img.shields.io/npm/v/defu)](https://npmjs.com/package/defu)
[![npm downloads](https://img.shields.io/npm/dm/defu)](https://npmjs.com/package/defu)
[![bundle size](https://img.shields.io/bundlephobia/minzip/defu)](https://bundlephobia.com/package/defu)
[![codecov](https://img.shields.io/codecov/c/gh/unjs/automd)](https://codecov.io/gh/unjs/automd)
[![npm version](https://flat.badgen.net/npm/v/defu?color=yellow)](https://npmjs.com/package/defu)
[![npm downloads](https://flat.badgen.net/npm/dm/defu?color=yellow)](https://npmjs.com/package/defu)
[![bundle size](https://flat.badgen.net/bundlephobia/minzip/defu?color=yellow)](https://bundlephobia.com/package/defu)
[![codecov](https://flat.badgen.net/codecov/c/github/unjs/automd?color=yellow)](https://codecov.io/gh/unjs/automd)

<!-- /automd -->

Expand All @@ -206,7 +206,7 @@ The `badges` generator generates badges for the latest npm version, npm download
- `provider`: Can be one of `shields` (for [shields.io](https://shields.io/)) or `badgen` / `badgenClassic` (for [badgen.net](https://badgen.net/)). Default is `badgen`.

> [!TIP]
> You can use additional `style`, `labelColor` and `color` args for `shields` provider for example: `provider=shields style=flat-square labelColor=f0db4f color=18181b`.
> You can use additional args `color`, `labelColor` to customize style. For provider specific params, use `styleParams`.
## Development

Expand Down
26 changes: 13 additions & 13 deletions playground/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# automd playground

<!-- automd:badges name=defu codecov bundlephobia -->
<!-- automd:badges color=yellow name=defu codecov bundlephobia -->

[![npm version](https://img.shields.io/npm/v/defu)](https://npmjs.com/package/defu)
[![npm downloads](https://img.shields.io/npm/dm/defu)](https://npmjs.com/package/defu)
[![bundle size](https://img.shields.io/bundlephobia/minzip/defu)](https://bundlephobia.com/package/defu)
[![codecov](https://img.shields.io/codecov/c/gh/unjs/automd)](https://codecov.io/gh/unjs/automd)
[![npm version](https://flat.badgen.net/npm/v/defu?color=yellow)](https://npmjs.com/package/defu)
[![npm downloads](https://flat.badgen.net/npm/dm/defu?color=yellow)](https://npmjs.com/package/defu)
[![bundle size](https://flat.badgen.net/bundlephobia/minzip/defu?color=yellow)](https://bundlephobia.com/package/defu)
[![codecov](https://flat.badgen.net/codecov/c/github/unjs/automd?color=yellow)](https://codecov.io/gh/unjs/automd)

<!-- /automd -->

Expand All @@ -15,13 +15,13 @@

```sh
# npm
npx automd@0.1.5 .
npx automd@0.2.0 .

# pnpm
pnpm dlx automd@0.1.5 .
pnpm dlx automd@0.2.0 .

# bun
bunx automd@0.1.5 .
bunx automd@0.2.0 .
```

<!-- /automd -->
Expand All @@ -30,19 +30,19 @@ bunx automd@0.1.5 .

```sh
# ✨ Auto-detect
npx nypm i -D automd@^0.1.5
npx nypm i -D automd@^0.2.0

# npm
npm install -D automd@^0.1.5
npm install -D automd@^0.2.0

# yarn
yarn add -D automd@^0.1.5
yarn add -D automd@^0.2.0

# pnpm
pnpm install -D automd@^0.1.5
pnpm install -D automd@^0.2.0

# bun
bun install -D automd@^0.1.5
bun install -D automd@^0.2.0
```

<!-- /automd -->
Expand Down
19 changes: 11 additions & 8 deletions src/generators/badges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineGenerator } from "../generator";
import { getPkg } from "../_utils";

type BadgeType = keyof typeof badgeTypes;
type BadgeProvider = Record<BadgeType, string> & { _params?: string[] };
type BadgeProvider = Record<BadgeType, string>;

const badgeTypes = {
npmVersion: {
Expand All @@ -25,13 +25,14 @@ const badgeTypes = {
};

const badgeProviders = <Record<string, BadgeProvider>>{
// https://shields.io/badges/static-badge
shields: {
_params: ["style", "color", "labelColor"],
npmVersion: "https://img.shields.io/npm/v/{name}",
npmDownloads: "https://img.shields.io/npm/dm/{name}",
bundlephobia: "https://img.shields.io/bundlephobia/minzip/{name}",
codecov: "https://img.shields.io/codecov/c/gh/{github}",
},
// https://badgen.net/help
badgen: {
npmVersion: "https://flat.badgen.net/npm/v/{name}",
npmDownloads: "https://flat.badgen.net/npm/dm/{name}",
Expand Down Expand Up @@ -59,12 +60,14 @@ export const badges = defineGenerator({
const fillStr = (str: string) =>
str.replace(/{(\w+)}/g, (_, key) => ctx[key] || "");

const provider = badgeProviders[args.provider] || badgeProviders.shields;
const providerParams = (provider._params || [])
.filter((key) => ctx[key])
.map(
(key) => `${encodeURIComponent(key)}=${encodeURIComponent(ctx[key])}`,
)
const provider = badgeProviders[args.provider] || badgeProviders.badgen;
const providerParams = Object.entries({
color: args.color,
labelColor: args.labelColor,
...args.styleParams,
})
.filter(([, value]) => value)
.map(([key, value]) => `${key}=${encodeURIComponent(value as string)}`)
.join("&");

const badges = {
Expand Down

0 comments on commit db2fb78

Please sign in to comment.