Created
May 28, 2025 11:23
-
-
Save zellwk/3396e8bb98316939a66206f1e198aa1b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cssVarsToDesignTokens(input) { | |
const lines = input.split('\n') | |
const result = {} | |
for (let line of lines) { | |
line = line.trim() | |
if (!line || line.startsWith('//') || line.startsWith('/*')) continue | |
if (!line.startsWith('--')) continue | |
const [rawKey, rawValue] = line.split(':') | |
if (!rawValue) continue | |
const key = rawKey.replace(/^--/, '').trim() | |
let value = rawValue.replace(/;$/, '').trim() | |
// If value is a var reference, extract the referenced variable | |
if (value.startsWith('var(')) { | |
value = value.replace(/^var\(--/, '').replace(/\)$/, '').trim() | |
value = `{${value}}` // Optional: wrap in curly braces to indicate reference | |
} | |
// Guess type | |
let $type = 'other' | |
if (value.match(/^#([0-9a-f]{3}|[0-9a-f]{6})$/i)) $type = 'color' | |
if (value.match(/px$/)) $type = 'dimension' | |
result[key] = { value, $type } | |
} | |
return { color: Object.fromEntries(Object.entries(result).filter(([k]) => k.startsWith('color-') || k.startsWith('color_'))) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment