Skip to content
This repository was archived by the owner on Dec 28, 2022. It is now read-only.

Commit f975d0b

Browse files
author
Jiří Třečák
committed
Added reusable name generation, javascript helpers, javascript configuration
1 parent eed7e73 commit f975d0b

File tree

8 files changed

+70
-14
lines changed

8 files changed

+70
-14
lines changed

exporter.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"version": "1.0.0",
1010
"config": {
1111
"sources": "sources.json",
12-
"output": "output.json"
12+
"output": "output.json",
13+
"js": "src/js/helpers.js"
1314
},
1415
"engines": {
1516
"pulsar": "1.0.0",

output.json

-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
"invoke": "measures.pr",
1717
"write_to": "measures.css"
1818
},
19-
{
20-
"invoke": "radii.pr",
21-
"write_to": "radii.css"
22-
},
2319
{
2420
"invoke": "shadows.pr",
2521
"write_to": "shadows.css"

src/colors.pr

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@ Generate all color tokens as named variables.
44
Tokens are named by their group path and then name,
55
and their value rendered using `rendered-color` blueprint
66

7-
*}
7+
*}
8+
:root {
9+
{[ for token in @ds.tokensByType("Color") ]}
10+
{[ inject "rendered-token-var" context token /]}
11+
12+
13+
{[/]}
14+
}

src/js/helpers.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Convert group name, token name and possible prefix into camelCased string, joining everything together
3+
*/
4+
Pulsar.registerFunction("readableVariableName", function (token, tokenGroup, prefix) {
5+
6+
// Create array with all path segments and token name at the end
7+
let segments = [...tokenGroup.path, token.name]
8+
if (prefix && prefix.length > 0) {
9+
segments.unshift(prefix)
10+
}
11+
12+
// Create "sentence" separated by spaces so we can camelcase it all
13+
let sentence = segments.join(" ")
14+
15+
// Return camelcased string from all segments
16+
return sentence.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase());
17+
})
18+
19+
/**
20+
* Behavior configuration of the exporter
21+
* Prefixes: Add prefix for each category of the tokens. For example, all colors can start with "color, if needed"
22+
*/
23+
Pulsar.registerPayload("behavior", {
24+
colorTokenPrefix: "color",
25+
borderTokenPrefix: "border",
26+
gradientTokenPrefix: "gradient",
27+
measureTokenPrefix: "measure",
28+
shadowTokenPrefix: "shadow",
29+
typographyTokenPrefix: "typography"
30+
})

src/object-renderers/rendered-name.pr

+20
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,23 @@ Rendered as:
2424
[group1][group2][...][name];
2525

2626
*}
27+
{[ let tokenGroup = @ds.tokenGroupContainingTokenId(context.id) /]}
28+
{[ let prefix = "" /]}
29+
{[ switch context.tokenType ]}
30+
{[ case "Color" ]}
31+
{[ prefix = behavior.colorTokenPrefix /]}
32+
{[ case "Typography" ]}
33+
{[ prefix = behavior.typographyTokenPrefix /]}
34+
{[ case "Shadow" ]}
35+
{[ prefix = behavior.shadowTokenPrefix /]}
36+
{[ case "Border" ]}
37+
{[ prefix = behavior.borderTokenPrefix /]}
38+
{[ case "Measure" ]}
39+
{[ prefix = behavior.measureTokenPrefix /]}
40+
{[ case "Gradient" ]}
41+
{[ prefix = behavior.gradientTokenPrefix /]}
42+
{[ default ]}
43+
{[ prefix = "" /]}
44+
{[/]}
45+
{[ let tokenName = @js.readableVariableName(context, tokenGroup, prefix) /]}
46+
{{ tokenName }}

src/object-renderers/rendered-token-var.pr

+2
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ Rendered as:
2222
--[name]: [value];
2323

2424
*}
25+
{[ let token = context /]}
26+
{[ inject "rendered-name" context token /]}: "value";

src/radii.pr

-7
This file was deleted.

src/typography.pr

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@ Generate all typography tokens as named variables.
44
Tokens are named by their group path and then name,
55
and their value rendered using `rendered-color` blueprint
66

7-
*}
7+
*}
8+
:root {
9+
{[ for token in @ds.tokensByType("Typography") ]}
10+
{[ inject "rendered-token-var" context token /]}
11+
12+
{[/]}
13+
14+
}

0 commit comments

Comments
 (0)