From 7a09d1e4eaf11cf900489c537b40904609fadd40 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Wed, 26 Mar 2025 10:03:04 -0400 Subject: [PATCH] fix: ignore known badges when reading logo --- src/options/readLogo.test.ts | 20 ++++++++++++++++++++ src/options/readLogo.ts | 14 ++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/options/readLogo.test.ts b/src/options/readLogo.test.ts index 07a70e8f2..e1bf702d7 100644 --- a/src/options/readLogo.test.ts +++ b/src/options/readLogo.test.ts @@ -24,6 +24,26 @@ describe(readLogo, () => { expect(logo).toBeUndefined(); }); + it("resolves undefined when the found image is an All Contributors badge", async () => { + const logo = await readLogo(() => + Promise.resolve( + `\nAll Contributors: 1`, + ), + ); + + expect(logo).toBeUndefined(); + }); + + it("resolves undefined when the found image is a shields.io badge", async () => { + const logo = await readLogo(() => + Promise.resolve( + `\nTypeScript: Strict`, + ), + ); + + expect(logo).toBeUndefined(); + }); + it("parses when found in an unquoted string", async () => { const logo = await readLogo(() => Promise.resolve(` diff --git a/src/options/readLogo.ts b/src/options/readLogo.ts index 750ee910d..3abdbf19e 100644 --- a/src/options/readLogo.ts +++ b/src/options/readLogo.ts @@ -7,19 +7,25 @@ export async function readLogo(getReadme: () => Promise) { return undefined; } + const alt = + /alt=['"](.+)['"]\s*src=/.exec(tag)?.[1].split(/['"]?\s*\w+=/)[0] ?? + "Project logo"; + + if (/All Contributors: \d+/.test(alt)) { + return undefined; + } + const src = /src\s*=(.+)['"/]>/ .exec(tag)?.[1] ?.split(/\s*\w+=/)[0] .replaceAll(/^['"]|['"]$/g, ""); - if (!src) { + if (!src || src.includes("//img.shields.io")) { return undefined; } return { - alt: - /alt=['"](.+)['"]\s*src=/.exec(tag)?.[1].split(/['"]?\s*\w+=/)[0] ?? - "Project logo", + alt, src, ...readLogoSizing(src), };