|
| 1 | +// v10.4 — Icon System fingerprint |
| 2 | +// |
| 3 | +// Pure extractor — operates on the icon payload the crawler already collects. |
| 4 | +// We can't reliably match against Lucide/Phosphor/Heroicons path-data without |
| 5 | +// shipping the full libraries, so this extractor does the next-best thing: |
| 6 | +// infers the *system* an icon set came from (stroke vs fill, stroke width, |
| 7 | +// corner style, grid size, viewBox convention) and emits guidance any LLM can |
| 8 | +// act on ("use Lucide @ 1.5 stroke, 24px grid"). |
| 9 | + |
| 10 | +const LIBRARY_HINTS = [ |
| 11 | + { id: 'lucide', match: (ctx) => ctx.strokeDominant && ctx.avgWeight > 1.3 && ctx.avgWeight < 1.7 && ctx.grid24 && !ctx.roundedCaps, score: 0.8 }, |
| 12 | + { id: 'heroicons-outline', match: (ctx) => ctx.strokeDominant && ctx.avgWeight >= 1.8 && ctx.avgWeight <= 2.2 && ctx.grid24, score: 0.8 }, |
| 13 | + { id: 'heroicons-solid', match: (ctx) => ctx.fillDominant && ctx.grid24, score: 0.55 }, |
| 14 | + { id: 'phosphor', match: (ctx) => ctx.strokeDominant && ctx.roundedCaps && ctx.grid24, score: 0.7 }, |
| 15 | + { id: 'tabler', match: (ctx) => ctx.strokeDominant && ctx.avgWeight > 1.9 && ctx.grid24, score: 0.6 }, |
| 16 | + { id: 'feather', match: (ctx) => ctx.strokeDominant && ctx.avgWeight > 1.8 && ctx.roundedCaps && ctx.grid24, score: 0.7 }, |
| 17 | + { id: 'remix', match: (ctx) => ctx.mixedFillStroke && ctx.grid24, score: 0.45 }, |
| 18 | + { id: 'material', match: (ctx) => ctx.fillDominant && ctx.grid24, score: 0.4 }, |
| 19 | +]; |
| 20 | + |
| 21 | +function parseStroke(v) { |
| 22 | + if (!v) return 0; |
| 23 | + const n = parseFloat(v); |
| 24 | + return Number.isFinite(n) ? n : 0; |
| 25 | +} |
| 26 | + |
| 27 | +function viewBoxGrid(vb) { |
| 28 | + if (!vb) return null; |
| 29 | + const parts = vb.trim().split(/\s+/).map(Number); |
| 30 | + if (parts.length !== 4 || parts.some(n => !Number.isFinite(n))) return null; |
| 31 | + const w = parts[2], h = parts[3]; |
| 32 | + if (w === h && [16, 20, 24, 32, 48, 64].includes(w)) return w; |
| 33 | + return null; |
| 34 | +} |
| 35 | + |
| 36 | +function detectRoundedCaps(svg) { |
| 37 | + // Look for `stroke-linecap="round"` or `stroke-linejoin="round"` as a |
| 38 | + // proxy for Phosphor/Feather-style rounded terminals. |
| 39 | + return /stroke-linecap="round"|stroke-linejoin="round"/i.test(svg || ''); |
| 40 | +} |
| 41 | + |
| 42 | +export function extractIconSystem(icons = []) { |
| 43 | + if (!icons.length) { |
| 44 | + return { library: 'unknown', confidence: 0, stats: {}, signals: [], icons: [] }; |
| 45 | + } |
| 46 | + |
| 47 | + let strokeCount = 0, fillCount = 0, mixed = 0, weights = [], gridHits = {}; |
| 48 | + let rounded = 0; |
| 49 | + const perIconHints = []; |
| 50 | + |
| 51 | + for (const icon of icons) { |
| 52 | + const svg = icon.svg || ''; |
| 53 | + const stroke = icon.stroke || (svg.match(/stroke="([^"]+)"/i) || [])[1]; |
| 54 | + const fill = icon.fill || (svg.match(/fill="([^"]+)"/i) || [])[1]; |
| 55 | + const strokeWidthMatch = svg.match(/stroke-width="([0-9.]+)"/i); |
| 56 | + const sw = strokeWidthMatch ? parseStroke(strokeWidthMatch[1]) : 0; |
| 57 | + |
| 58 | + const hasStroke = !!(stroke && stroke !== 'none'); |
| 59 | + const hasFill = !!(fill && fill !== 'none'); |
| 60 | + if (hasStroke && !hasFill) strokeCount++; |
| 61 | + else if (hasFill && !hasStroke) fillCount++; |
| 62 | + else if (hasStroke && hasFill) mixed++; |
| 63 | + if (sw > 0) weights.push(sw); |
| 64 | + const grid = viewBoxGrid(icon.viewBox); |
| 65 | + if (grid) gridHits[grid] = (gridHits[grid] || 0) + 1; |
| 66 | + if (detectRoundedCaps(svg)) rounded++; |
| 67 | + |
| 68 | + perIconHints.push({ |
| 69 | + class: (icon.classList || '').slice(0, 80), |
| 70 | + grid, |
| 71 | + strokeWidth: sw || null, |
| 72 | + style: hasStroke && !hasFill ? 'stroke' : hasFill && !hasStroke ? 'fill' : 'mixed', |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + const avgWeight = weights.length ? weights.reduce((a, b) => a + b, 0) / weights.length : 0; |
| 77 | + const total = icons.length; |
| 78 | + const ctx = { |
| 79 | + strokeDominant: strokeCount / total > 0.55, |
| 80 | + fillDominant: fillCount / total > 0.55, |
| 81 | + mixedFillStroke: mixed / total > 0.3, |
| 82 | + avgWeight, |
| 83 | + roundedCaps: rounded / total > 0.4, |
| 84 | + grid24: gridHits[24] ? gridHits[24] / total > 0.5 : false, |
| 85 | + }; |
| 86 | + |
| 87 | + const scored = LIBRARY_HINTS |
| 88 | + .map(lib => ({ id: lib.id, score: lib.match(ctx) ? lib.score : 0 })) |
| 89 | + .filter(x => x.score > 0) |
| 90 | + .sort((a, b) => b.score - a.score); |
| 91 | + |
| 92 | + const primary = scored[0] || { id: 'unknown', score: 0 }; |
| 93 | + |
| 94 | + return { |
| 95 | + library: primary.id, |
| 96 | + confidence: Number(primary.score.toFixed(3)), |
| 97 | + alternates: scored.slice(1, 4), |
| 98 | + stats: { |
| 99 | + count: total, |
| 100 | + strokeOnly: strokeCount, |
| 101 | + fillOnly: fillCount, |
| 102 | + mixed, |
| 103 | + avgStrokeWidth: Number(avgWeight.toFixed(2)), |
| 104 | + gridDistribution: gridHits, |
| 105 | + roundedCapsFraction: Number((rounded / total).toFixed(2)), |
| 106 | + }, |
| 107 | + signals: Object.entries(ctx).filter(([, v]) => v === true).map(([k]) => k), |
| 108 | + icons: perIconHints.slice(0, 30), |
| 109 | + }; |
| 110 | +} |
0 commit comments