forked from unocss/unocss
/
utils.ts
89 lines (76 loc) · 2.73 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import path from 'path'
import type { GenerateResult, UnoGenerator } from '@unocss/core'
import { cssIdRE } from '@unocss/core'
import prettier from 'prettier/standalone'
import parserCSS from 'prettier/parser-postcss'
import type { Theme } from '@unocss/preset-mini'
import { parseColor } from '@unocss/preset-mini'
import { colorToString } from '@unocss/preset-mini/utils'
export function throttle<T extends ((...args: any) => any)>(func: T, timeFrame: number): T {
let lastTime = 0
let timer: any
return function () {
const now = Date.now()
clearTimeout(timer)
if (now - lastTime >= timeFrame) {
lastTime = now
return func()
}
else {
timer = setTimeout(func, timeFrame)
}
} as T
}
export async function getPrettiedCSS(uno: UnoGenerator, util: string) {
const result = (await uno.generate(new Set([util]), { preflights: false, safelist: false }))
const prettified = prettier.format(result.css, {
parser: 'css',
plugins: [parserCSS],
})
return {
...result,
prettified,
}
}
export async function getPrettiedMarkdown(uno: UnoGenerator, util: string) {
return `\`\`\`css\n${(await getPrettiedCSS(uno, util)).prettified}\n\`\`\``
}
const matchedAttributifyRE = /(?<=^\[.+~?=").*(?="\]$)/
const _colorsMapCache = new Map<string, string>()
export function getColorsMap(uno: UnoGenerator, result: GenerateResult) {
const theme = uno.config.theme as Theme
const themeColorNames = Object.keys(theme.colors ?? {})
const colorNames = themeColorNames.concat(themeColorNames.map(colorName => colorName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()))
const colorsMap = new Map<string, string>()
for (const i of result.matched) {
const _i = i.replace('~="', '="')
if (_colorsMapCache.get(_i)) {
colorsMap.set(_i, _colorsMapCache.get(_i)!)
continue
}
const matchedAttr = i.match(matchedAttributifyRE)
const body = matchedAttr ? matchedAttr[0].split(':').at(-1) ?? '' : i // remove prefix e.g. `dark:` `hover:`
for (const colorName of colorNames) {
const nameIndex = body.indexOf(colorName)
if (nameIndex > -1) {
const parsedResult = parseColor(body.substring(nameIndex), theme)
if (parsedResult?.cssColor) {
const color = colorToString(parsedResult.cssColor, parsedResult.alpha)
colorsMap.set(_i, color)
_colorsMapCache.set(_i, color)
}
break
}
}
}
if (_colorsMapCache.size > 5000)
_colorsMapCache.clear()
return colorsMap
}
export function isCssId(id: string) {
return cssIdRE.test(id)
}
export function isSubdir(parent: string, child: string) {
const relative = path.relative(parent, child)
return relative && !relative.startsWith('..') && !path.isAbsolute(relative)
}