/** * 获取动态类名 */ export const getDynamicClassNames = (value: string): string[] => { const names = new Set(); // 匹配数组中的字符串元素(如 'text-center') const arrayRegex = /['"](.*?)['"]/g; let arrayMatch; while ((arrayMatch = arrayRegex.exec(value)) !== null) { arrayMatch[1].trim() && names.add(arrayMatch[1]); } // 匹配对象键(如 { 'text-a': 1 }) const objKeyRegex = /[{,]\s*['"](.*?)['"]\s*:/g; let objKeyMatch; while ((objKeyMatch = objKeyRegex.exec(value)) !== null) { objKeyMatch[1].trim() && names.add(objKeyMatch[1]); } // 匹配三元表达式中的字符串(如 'dark' 和 'light') const ternaryRegex = /(\?|:)\s*['"](.*?)['"]/g; let ternaryMatch; while ((ternaryMatch = ternaryRegex.exec(value)) !== null) { ternaryMatch[2].trim() && names.add(ternaryMatch[2]); } return Array.from(names); }; /** * 获取类名 */ export function getClassNames(html: string): string[] { const classRegex = /(?:class|:class)\s*=\s*(["'])([\s\S]*?)\1/gi; const classNames = new Set(); let match; while ((match = classRegex.exec(html)) !== null) { const isStaticClass = match[0].startsWith("class"); const value = match[2].trim(); if (isStaticClass) { // 处理静态 class value.split(/\s+/).forEach((name) => name && classNames.add(name)); } else { // 处理动态 :class getDynamicClassNames(value).forEach((name) => classNames.add(name)); } } return Array.from(classNames); } /** * 获取 class 内容 */ export function getClassContent(html: string) { const regex = /(?:class|:class)\s*=\s*(['"])([\s\S]*?)\1/g; const texts: string[] = []; let match; while ((match = regex.exec(html)) !== null) { texts.push(match[2]); } return texts; } /** * 获取节点 */ export function getNodes(code: string) { const nodes: string[] = []; const templateMatch = /