import type { DesignToken, TokenCategory } from "../token-types.js"; import { isDTCGFile, parseDtcgTokens } from "./dtcg.js"; import { parseCssTokens, parseScssTokens, detectTokenPrefix } from "./scss.js"; import { containsTailwindV4Theme, parseTailwindV4Theme } from "./tailwind-v4.js"; import type { ParsedToken, ParseTokensOptions, TokenFormat, TokenParseOutput } from "./types.js"; export type { DesignTokenParseOptions, ParsedToken, ParseTokensOptions, ThemeParserResult, TokenFormat, TokenParseOutput, } from "./types.js"; export { inferTokenCategory, inferTokenGroup, normalizeTokenGroupComment, } from "./categories.js"; export { calculateDeltaE, colorSimilarity, familyDistance, hexToRgb, isColorLike, nearestByDeltaE, normalizeColor, parseColor, parseColorToRgb, parseRgb, rgbToHex, type DeltaEMatch, type NearestByDeltaEOptions, type RGB, type RGBA, } from "./color.js"; export { type TokenLookup } from "./lookup.js"; export { detectTokenPrefix, parseCssTokens, parseScssTokens, parseScssVariables, parseTokenFile, resolveTokenValue, } from "./scss.js"; export { isDTCGFile, parseDTCGFile, parseDtcgTokens, } from "./dtcg.js"; export { containsTailwindV4Theme, parseTailwindV4Theme, } from "./tailwind-v4.js"; export { normalizeTokenValue, parseDesignTokenContent, resolveDesignTokenValue, } from "./design-token-parser.js"; export function parseTokens( content: string, formatOrOptions: TokenFormat | ParseTokensOptions = "auto", ): TokenParseOutput { const options = typeof formatOrOptions === "string" ? { format: formatOrOptions } : formatOrOptions; const format = options.format ?? "auto"; const filePath = options.filePath ?? ""; if (format === "dtcg" || (format === "auto" && isDTCGFile(filePath))) { return parseDtcgTokens(content, filePath); } if (format === "tailwind-v4" || (format === "auto" && containsTailwindV4Theme(content))) { return designTokensToParseOutput(parseTailwindV4Theme(content, filePath).tokens); } if (format === "css") { return parseCssTokens(content, filePath); } return parseScssTokens(content, filePath); } function designTokensToParseOutput(tokens: DesignToken[]): TokenParseOutput { const categories: Record = {}; for (const token of tokens) { const category = tokenCategoryToGroup(token.category); categories[category] ??= []; categories[category].push({ name: token.name, value: token.rawValue, resolvedValue: token.resolvedValue !== token.rawValue ? token.resolvedValue : undefined, category, description: token.description, }); } return { prefix: detectTokenPrefix(tokens.map((token) => token.name)), categories, total: tokens.length, }; } function tokenCategoryToGroup(category: TokenCategory): string { switch (category) { case "color": return "colors"; case "shadow": return "shadows"; case "border": return "borders"; case "animation": return "transitions"; default: return category; } }