|
| 1 | +/** |
| 2 | + * The per-entry badge (#117): a static SVG at badge/<id>.svg saying the |
| 3 | + * catalog read the source's licence and when, for publishers to link back |
| 4 | + * with. Coloured by licence family. Pure. |
| 5 | + */ |
| 6 | +import { esc } from "./shared.mjs"; |
| 7 | + |
| 8 | +const FAMILY_COLOURS = { |
| 9 | + "public-domain": "#2e7d32", |
| 10 | + permissive: "#1565c0", |
| 11 | + "cc-by": "#00796b", |
| 12 | + copyleft: "#b26a00", |
| 13 | + "non-commercial": "#8b2e2e", |
| 14 | +}; |
| 15 | + |
| 16 | +/** Approximate text width at 11px Verdana, as shields.io badges measure it. */ |
| 17 | +export function textWidth(text) { |
| 18 | + let w = 0; |
| 19 | + for (const ch of String(text)) w += /[mwMW@]/.test(ch) ? 10 : /[ijlI.,:;'|!·]/.test(ch) ? 3.5 : /[A-Z0-9]/.test(ch) ? 7.5 : 6.5; |
| 20 | + return Math.ceil(w); |
| 21 | +} |
| 22 | + |
| 23 | +/** The badge SVG: "Free Game Dev Assets | <licence> · read <date>". */ |
| 24 | +export function badgeSvg(entry) { |
| 25 | + const left = "Free Game Dev Assets"; |
| 26 | + const right = `${entry.license}${entry.verified ? ` · read ${entry.verified}` : ""}`; |
| 27 | + const lw = textWidth(left) + 12; |
| 28 | + const rw = textWidth(right) + 12; |
| 29 | + const colour = FAMILY_COLOURS[entry.licenseFamily] || "#5a6472"; |
| 30 | + const label = `${left}: ${right}`; |
| 31 | + return `<svg xmlns="http://www.w3.org/2000/svg" width="${lw + rw}" height="20" role="img" aria-label="${esc(label)}"> |
| 32 | + <title>${esc(label)}</title> |
| 33 | + <rect width="${lw}" height="20" fill="#12151a"/> |
| 34 | + <rect x="${lw}" width="${rw}" height="20" fill="${colour}"/> |
| 35 | + <g fill="#fff" font-family="Verdana,DejaVu Sans,sans-serif" font-size="11" text-anchor="middle"> |
| 36 | + <text x="${lw / 2}" y="14">${esc(left)}</text> |
| 37 | + <text x="${lw + rw / 2}" y="14">${esc(right)}</text> |
| 38 | + </g> |
| 39 | +</svg> |
| 40 | +`; |
| 41 | +} |
| 42 | + |
| 43 | +/** Markdown and HTML a publisher can paste to show the badge, linking the entry page. */ |
| 44 | +export function badgeSnippets(entry, siteUrl) { |
| 45 | + const base = String(siteUrl).replace(/\/+$/, ""); |
| 46 | + const page = `${base}/entry/${entry.id}/`; |
| 47 | + const img = `${base}/badge/${entry.id}.svg`; |
| 48 | + const alt = `Licence read by Free Game Dev Assets`; |
| 49 | + return { |
| 50 | + markdown: `[](${page})`, |
| 51 | + html: `<a href="${page}"><img src="${img}" alt="${alt}" /></a>`, |
| 52 | + }; |
| 53 | +} |
0 commit comments