@@ -31,9 +31,10 @@ export const colormode: Format = {
31
31
usesDtcg,
32
32
} ) ;
33
33
34
- const colorSchemeProperty = mode_ === 'dark' || mode_ === 'light' ? `color-scheme: ${ mode_ } ;\n` : '' ;
34
+ const colorSchemeProperty = mode_ === 'dark' || mode_ === 'light' ? `\n color-scheme: ${ mode_ } ;\n` : '' ;
35
35
36
- const content = `{\n${ allTokens . map ( format ) . join ( '\n' ) } \n${ colorSchemeProperty } }\n` ;
36
+ const formattedTokens = dictionary . allTokens . map ( format ) . join ( '\n' ) ;
37
+ const content = `{\n${ formattedTokens } \n${ colorSchemeProperty } }\n` ;
37
38
const autoSelectorContent = [ 'light' , 'dark' ] . includes ( mode_ ) ? prefersColorScheme ( mode_ , content ) : '' ;
38
39
const body = R . isNotNil ( layer )
39
40
? `@layer ${ layer } {\n${ selector } ${ content } ${ autoSelectorContent } \n}\n`
@@ -48,7 +49,6 @@ const calculatedVariable = R.pipe(R.split(/:(.*?);/g), (split) => `${split[0]}:
48
49
export const semantic : Format = {
49
50
name : 'ds/css-semantic' ,
50
51
format : async ( { dictionary, file, options, platform } ) => {
51
- const { allTokens } = dictionary ;
52
52
const { outputReferences, usesDtcg } = options ;
53
53
const { selector, isCalculatedToken, layer } = platform ;
54
54
@@ -61,7 +61,7 @@ export const semantic: Format = {
61
61
usesDtcg,
62
62
} ) ;
63
63
64
- const formatTokens = R . map ( ( token : TransformedToken ) => {
64
+ const formattedTokens = R . map ( ( token : TransformedToken ) => {
65
65
const originalValue = getValue < string > ( token . original ) ;
66
66
67
67
if (
@@ -75,50 +75,19 @@ export const semantic: Format = {
75
75
}
76
76
77
77
return format ( token ) ;
78
- } ) ;
78
+ } , dictionary . allTokens ) ;
79
79
80
- const formattedVariables = formatTokens ( allTokens ) ;
81
- const content = `{\n${ formattedVariables . join ( '\n' ) } \n}\n` ;
80
+ const content = `{\n${ formattedTokens . join ( '\n' ) } \n}\n` ;
82
81
const body = R . isNotNil ( layer ) ? `@layer ${ layer } {\n${ selector } ${ content } \n}\n` : `${ selector } ${ content } \n` ;
83
82
84
83
return header + body ;
85
84
} ,
86
85
} ;
87
86
88
- type Typgraphy = {
89
- fontWeight : string ;
90
- fontSize : string ;
91
- lineHeight : number ;
92
- fontFamily : string ;
93
- } ;
94
-
95
- type ProcessedTokens = { variables : string [ ] ; classes : string [ ] } ;
96
-
97
- const sortByType = R . sortBy < TransformedToken > ( ( token ) => token ?. $type === 'typography' ) ;
98
- const getVariableName = R . pipe < string [ ] , string [ ] , string , string , string , string > (
99
- R . split ( ':' ) ,
100
- R . head ,
101
- R . defaultTo ( '' ) ,
102
- R . trim ,
103
- ( name ) => `var(${ name } )` ,
104
- ) ;
105
-
106
- const bemify = R . pipe (
107
- ( path : string [ ] ) => {
108
- const filteredPath = path . filter ( ( p ) => p !== 'typography' ) ;
109
- const withPrefix = R . concat ( [ prefix ] , R . remove ( 0 , 0 , filteredPath ) ) ;
110
- const [ rest , last ] = R . splitAt ( - 1 , withPrefix ) ;
111
-
112
- const className = `${ rest . join ( '-' ) } --${ R . head ( last ) } ` ;
113
- return className ;
114
- } ,
115
- R . trim ,
116
- R . toLower ,
117
- ) ;
118
-
119
- const classSelector = R . pipe ( R . prop ( 'path' ) , bemify ) ;
120
- const sortTypographyLast = R . sortWith < TransformedToken > ( [
121
- R . ascend ( ( token ) => ( typeEquals ( 'typography' ) ( token ) ? 1 : 0 ) ) ,
87
+ // Predicate to filter tokens with .path array that includes both typography and fontFamily
88
+ const typographyFontFamilyPredicate = R . allPass ( [
89
+ R . pathSatisfies ( R . includes ( 'typography' ) , [ 'path' ] ) ,
90
+ R . pathSatisfies ( R . includes ( 'fontFamily' ) , [ 'path' ] ) ,
122
91
] ) ;
123
92
124
93
export const typography : Format = {
@@ -136,74 +105,13 @@ export const typography: Format = {
136
105
usesDtcg,
137
106
} ) ;
138
107
139
- const sortedTokens = sortTypographyLast ( dictionary . allTokens ) ;
140
-
141
- const formattedTokens = R . pipe (
142
- sortByType ,
143
- R . reduce < TransformedToken , ProcessedTokens > (
144
- ( acc , token ) => {
145
- if ( typeEquals ( 'fontweight' , token ) ) {
146
- const className = `
147
- .${ classSelector ( token ) } {
148
- font-weight: ${ getValue < string > ( token ) } ;
149
- }` ;
150
-
151
- return Object . assign ( acc , {
152
- variables : [ ...acc . variables , format ( token ) ] ,
153
- classes : [ ...acc . classes , className ] ,
154
- } ) ;
155
- }
156
-
157
- if ( typeEquals ( 'lineheight' , token ) ) {
158
- const className = `
159
- .${ classSelector ( token ) } {
160
- line-height: ${ getValue < string > ( token ) } ;
161
- }` ;
162
-
163
- return Object . assign ( acc , {
164
- variables : [ ...acc . variables , format ( token ) ] ,
165
- classes : [ ...acc . classes , className ] ,
166
- } ) ;
167
- }
168
-
169
- if ( typeEquals ( 'typography' , token ) ) {
170
- let references : TransformedToken [ ] = [ ] ;
171
- try {
172
- references = getReferences ( getValue < Typgraphy > ( token . original ) , dictionary . tokens ) ;
173
- } catch ( error ) {
174
- console . error ( 'Error getting references' , error ) ;
175
- throw new Error ( JSON . stringify ( token , null , 2 ) ) ;
176
- }
177
- const fontweight = R . find < TransformedToken > ( typeEquals ( [ 'fontweight' ] ) ) ( references ) ;
178
- const lineheight = R . find < TransformedToken > ( typeEquals ( [ 'lineheight' ] ) ) ( references ) ;
179
- const fontsize = R . find < TransformedToken > ( typeEquals ( [ 'fontsize' ] ) ) ( references ) ;
180
- const letterSpacing = R . find < TransformedToken > ( typeEquals ( [ 'dimension' ] ) ) ( references ) ;
181
-
182
- const fontSizeVar = fontsize ? getVariableName ( format ( fontsize ) ) : null ;
183
- const fontWeightVar = fontweight ? getVariableName ( format ( fontweight ) ) : null ;
184
- const lineheightVar = lineheight ? getVariableName ( format ( lineheight ) ) : null ;
185
- const letterSpacingVar = letterSpacing ? getVariableName ( format ( letterSpacing ) ) : null ;
186
-
187
- const className = `
188
- .${ classSelector ( token ) } {
189
- ${ fontSizeVar ? `font-size: ${ fontSizeVar } ;` : '' }
190
- ${ lineheightVar ? `line-height: ${ lineheightVar } ;` : '' }
191
- ${ fontWeightVar ? `font-weight: ${ fontWeightVar } ;` : '' }
192
- ${ letterSpacingVar ? `letter-spacing: ${ letterSpacingVar } ;` : '' }
193
- }` ;
194
-
195
- return Object . assign ( acc , { classes : [ className , ...acc . classes ] } ) ;
196
- }
197
-
198
- return Object . assign ( acc , { variables : [ ...acc . variables , format ( token ) ] } ) ;
199
- } ,
200
- { variables : [ ] , classes : [ ] } ,
201
- ) ,
202
- ) ( sortedTokens ) ;
203
-
204
- const classes = formattedTokens . classes . join ( '\n' ) ;
205
- const variables = formattedTokens . variables . join ( '\n' ) ;
206
- const content = selector ? `${ selector } {\n${ variables } \n${ classes } \n}` : classes ;
108
+ console . log ( 'dictionary.allTokens' , dictionary . allTokens ) ;
109
+
110
+ const filteredTokens = R . reject ( typographyFontFamilyPredicate , dictionary . allTokens ) ;
111
+
112
+ const formattedTokens = R . pipe ( R . map ( format ) , R . join ( '\n' ) ) ( filteredTokens ) ;
113
+
114
+ const content = selector ? `${ selector } {\n${ formattedTokens } \n}` : formattedTokens ;
207
115
const body = R . isNotNil ( layer ) ? `@layer ${ layer } {\n${ content } \n}` : content ;
208
116
209
117
return header + body ;
0 commit comments