-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
149 lines (127 loc) · 2.91 KB
/
index.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
export type ComponentPropertyValue = string | number | boolean | DesignComponent
export interface DesignComponent {
name: string
properties: Record<string, ComponentPropertyValue>
}
export interface DevComponent {
name: string
props: Record<string, unknown>
children: (DevComponent | string)[]
}
export type SupportedLang =
| 'text'
| 'tsx'
| 'jsx'
| 'ts'
| 'js'
| 'vue'
| 'html'
| 'css'
| 'sass'
| 'scss'
| 'less'
| 'stylus'
| 'json'
interface TransformBaseParams {
/**
* The user preferences related to code transformation
* @example { useRem: true, rootFontSize: 16 }
*/
options: {
useRem: boolean
rootFontSize: number
}
}
interface TransformParams extends TransformBaseParams {
/**
* The generated CSS code
* @example 'background-color: red; color: blue;'
*/
code: string
/**
* The parsed CSS properties
* @example { 'background-color': 'red', 'color': 'blue' }
*/
style: Record<string, string>
}
interface TransformVariableParams extends TransformBaseParams {
/**
* The generated CSS variable code
* @example 'var(--color-primary, #6699cc)'
*/
code: string
/**
* The variable name
* @example 'color-primary'
*/
name: string
/**
* The variable value
* @example '#6699cc'
*/
value?: string
}
interface TransformPxParams extends TransformBaseParams {
/**
* The length value
* @example 16
*/
value: number
}
interface TransformComponentParams {
/**
* The design component
*/
component: DesignComponent
}
export type TransformOptions = {
/**
* The language of the code block for syntax highlighting
* @example 'scss'
*/
lang?: SupportedLang
/**
* Transform the generated CSS code
*/
transform?: (params: TransformParams) => string
/**
* Transform the generated CSS variable code
* @example 'var(--kui-color-primary, #6699cc)' -> '$ui-color-primary'
*/
transformVariable?: (params: TransformVariableParams) => string
/**
* Transform the pixel value to the desired unit and scale
* @example 16 -> '1rem'
*/
transformPx?: (params: TransformPxParams) => string
/**
* Transform the design component to a dev component
*/
transformComponent?: (params: TransformComponentParams) => DevComponent | string
}
export type CodeBlockOptions =
| (TransformOptions & {
/**
* The title of the code block
* @example 'SCSS'
*/
title?: string
})
| false
type BuiltInCodeBlock = 'css' | 'js'
type CodeOptions = Partial<Record<BuiltInCodeBlock, CodeBlockOptions>> &
Record<string, CodeBlockOptions>
export interface Plugin {
name: string
code: CodeOptions
}
export function definePlugin(plugin: Plugin): Plugin {
return plugin
}
export function h(
name: string,
props?: Record<string, unknown>,
children?: (DevComponent | string)[]
): DevComponent {
return { name, props: props || {}, children: children || [] }
}