Source: task/album-detection (objective/linker-improvements) — project brief asks for artist OR album linking; MVP linked artists only.
- service/lib.js: pickAlbum (the exact-name gate, named for the album path) + linkResult now carries kind:"artist"|"album" (additive; defaults to artist so older clients keep working).
- service/resolver.js: one Spotify type=artist,album search per candidate, exact-match against either; returns {url,kind}; cache key includes the kind hint. resolveOne/resolveAll thread the LLM kind hint through.
- service/llm.js: parseClassification now returns Map(name -> "artist"|"album") so album-kind candidates route to the album path (.has() still works like the old Set).
- extension/extract.js: albumMatchesIn over two precise signals (quoted strings + album cue phrases "the album X", "X LP/EP"), folded into allMatchesIn.
- extension/content.js + styles.css: album links render italicised (.rsl-album) with an album tooltip; artist behavior unchanged.
- Tests: new pure-logic coverage in lib.test.js / llm.test.js / extract.test.cjs (album shaping, Map-kind routing, album candidate extraction). scripts/check.sh green.
- README.md + AGENTS.md: document album linking.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
50 lines
2.4 KiB
JavaScript
50 lines
2.4 KiB
JavaScript
// Pure, side-effect-free helpers for the resolver. Kept separate from resolver.js
|
|
// (which has the HTTP-server side effect) so they can be unit-tested with `node --test`
|
|
// without starting a server. The artist-match precision lever lives here.
|
|
|
|
export const norm = (s) => String(s).normalize("NFKC").trim().toLowerCase();
|
|
|
|
// Fallback link when we have no direct Spotify page: a Google search biased at Bandcamp,
|
|
// which surfaces the artist's Bandcamp page (when it exists) plus general results.
|
|
export const googleSearch = (n) => `https://www.google.com/search?q=${encodeURIComponent(`${n} bandcamp`)}`;
|
|
|
|
// The precision lever: return the item whose name matches the candidate
|
|
// case-insensitively, else null. Tolerates null/garbage items. Used for both
|
|
// Spotify artist items and album items — the exact-name match is the same gate.
|
|
export function pickArtist(name, items) {
|
|
const want = norm(name);
|
|
return (items ?? []).find((a) => a && norm(a.name) === want) ?? null;
|
|
}
|
|
|
|
// Same exact-name gate, named for the album path's readability.
|
|
export const pickAlbum = pickArtist;
|
|
|
|
// A "/search" url means we don't have a direct artist page (just a search fallback).
|
|
export const isSearchUrl = (url) => typeof url === "string" && url.includes("/search");
|
|
|
|
// True for http/https URLs only — reject javascript:, ftp:, garbage. The Spotify
|
|
// external_url flows into an injected link's href, so validate it defensively.
|
|
export function isHttpUrl(u) {
|
|
try {
|
|
const p = new URL(u).protocol;
|
|
return p === "https:" || p === "http:";
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Build the single link to show for a confirmed name. Prefer the direct Spotify
|
|
// url (validated http(s), not a /search url); otherwise fall back to a Google→Bandcamp
|
|
// search. `primary` is what the extension renders ({ platform, url }); `kind`
|
|
// ("artist" | "album") tells it which affordance to show. `kind` is additive — the
|
|
// extension defaults to "artist" when absent, so older clients keep working.
|
|
export function linkResult(name, spotifyUrl, kind = "artist") {
|
|
const k = kind === "album" ? "album" : "artist";
|
|
const direct = spotifyUrl && isHttpUrl(spotifyUrl) && !isSearchUrl(spotifyUrl) ? spotifyUrl : null;
|
|
if (direct) {
|
|
return { name, kind: k, spotify: direct, primary: { platform: "spotify", url: direct } };
|
|
}
|
|
const url = googleSearch(name);
|
|
return { name, kind: k, google: url, primary: { platform: "google", url } };
|
|
}
|