opencode-config/.opencode/draw-image/src/recolor.ts
Sergey 56b67d2d5c
feat(draw-image): SVG template renderer for on-brand covers (#134)
* feat(draw-image): add SVG template render engine

* feat(draw-image): add opencode plugin and tool tests

* test(draw-image): add vitest unit integration e2e suite

* chore(draw-image): wire CI dependabot docs and ADR

* docs(handoff): set PR number

* docs(handoff): fix PR number placeholder in frontmatter

* fix(ci): use tempfile and trailing newline in draw-image tests

---------

Co-authored-by: opencode-agent <agent@opencode.local>
2026-07-29 23:42:50 +03:00

26 lines
No EOL
1 KiB
TypeScript

export function recolorSvg(svg: string, color: string): string {
if (color === "none") return svg
let out = svg
out = out.replace(/\bstroke=["'](?!none)([^"']+)["']/g, `stroke="${color}"`)
out = out.replace(/\bfill=["'](?!none)([^"']+)["']/g, (match, _val) => {
if (match.includes(`fill="none"`)) return match
return `fill="${color}"`
})
return out
}
export function stripSvgWrapper(svg: string): string {
const m = svg.match(/<svg[^>]*>([\s\S]*)<\/svg>/)
return m ? m[1].trim() : svg
}
export function extractRootAttrs(svg: string): { stroke?: string; fill?: string; strokeWidth?: string } {
const attrs: { stroke?: string; fill?: string; strokeWidth?: string } = {}
const strokeMatch = svg.match(/<svg[^>]*\bstroke=["']([^"']+)["']/)
if (strokeMatch) attrs.stroke = strokeMatch[1]
const fillMatch = svg.match(/<svg[^>]*\bfill=["']([^"']+)["']/)
if (fillMatch) attrs.fill = fillMatch[1]
const swMatch = svg.match(/<svg[^>]*\bstroke-width=["']([^"']+)["']/)
if (swMatch) attrs.strokeWidth = swMatch[1]
return attrs
}