-
-
Notifications
You must be signed in to change notification settings - Fork 778
/
utils.ts
99 lines (83 loc) · 3.06 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
90
91
92
93
94
95
96
97
98
99
import path from 'path'
import type { GenerateResult, UnoGenerator } 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 (...args) {
const now = Date.now()
clearTimeout(timer)
if (now - lastTime >= timeFrame) {
lastTime = now
return func(...args)
}
else {
timer = setTimeout(func, timeFrame, ...args)
}
} 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\`\`\``
}
export function body2ColorValue(body: string, theme: Theme) {
const themeColorNames = Object.keys(theme.colors ?? {})
const colorNames = themeColorNames.concat(themeColorNames.map(colorName => colorName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()))
for (const colorName of colorNames) {
const nameIndex = body.indexOf(colorName)
if (nameIndex > -1) {
const parsedResult = parseColor(body.substring(nameIndex), theme)
if (parsedResult?.cssColor)
return parsedResult
}
}
return null
}
const matchedAttributifyRE = /(?<=^\[.+~=").*(?="\]$)/
const matchedValuelessAttributifyRE = /(?<=^\[).+(?==""\]$)/
const _colorsMapCache = new Map<string, string>()
export function getColorsMap(uno: UnoGenerator, result: GenerateResult) {
const theme = uno.config.theme as Theme
const colorsMap = new Map<string, string>()
for (const i of result.matched) {
const matchedValueless = i.match(matchedValuelessAttributifyRE)?.[0]
const colorKey = matchedValueless ?? i.replace('~="', '="')
const cachedColor = _colorsMapCache.get(colorKey)
if (cachedColor) {
colorsMap.set(colorKey, cachedColor)
continue
}
const matchedAttr = i.match(matchedAttributifyRE)?.[0] ?? matchedValueless
const body = (matchedAttr ?? i)
.split(':').slice(-1)[0] ?? '' // remove prefix e.g. `dark:` `hover:`
if (body) {
const colorValue = body2ColorValue(body, theme)
if (colorValue) {
const colorString = colorToString(colorValue.cssColor!, colorValue.alpha)
colorsMap.set(colorKey, colorString)
_colorsMapCache.set(colorKey, colorString)
}
}
}
if (_colorsMapCache.size > 5000)
_colorsMapCache.clear()
return colorsMap
}
export function isSubdir(parent: string, child: string) {
const relative = path.relative(parent, child)
return relative && !relative.startsWith('..') && !path.isAbsolute(relative)
}