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

Commit 3300b08

Browse files
author
Florian Torres
committed
Tailwind exporter
1 parent e542bd8 commit 3300b08

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

output.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
{
2828
"invoke": "index-file.pr",
2929
"write_to": "index.css"
30+
},
31+
{
32+
"invoke": "tailwind-variables.pr",
33+
"write_to": "tailwind-varibles.js"
3034
}
3135
]
3236
}

src/js/helpers.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Convert group name, token name and possible prefix into camelCased string, joining everything together
2+
* Convert group name, token name and possible prefix into kebab-case string, joining everything together
33
*/
44
Pulsar.registerFunction(
55
"readableVariableName",
@@ -21,7 +21,7 @@ Pulsar.registerFunction(
2121
// camelcase string from all segments
2222
sentence = sentence
2323
.toLowerCase()
24-
.replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase())
24+
.replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => "-" + chr)
2525

2626
// only allow letters, digits, underscore and hyphen
2727
sentence = sentence.replace(/[^a-zA-Z0-9_-]/g, '_')
@@ -35,6 +35,23 @@ Pulsar.registerFunction(
3535
}
3636
);
3737

38+
function getGroupNameFromOriginName(originName){
39+
return originName.substr(0, originName.indexOf('/')).trim().toLowerCase()
40+
}
41+
42+
function getGroups(allTokens){
43+
let allGroups = allTokens.map(token => getGroupNameFromOriginName(token.origin.name))
44+
let uniqueGroups = [...new Set(allGroups)]
45+
return uniqueGroups
46+
}
47+
48+
Pulsar.registerFunction("getGroups", getGroups)
49+
50+
function getTokensByGroup(allTokens, group) {
51+
return allTokens.filter(token => token.origin.name.substr(0, getGroupNameFromOriginName(token.origin.name) === group))
52+
}
53+
54+
Pulsar.registerFunction("getTokensByGroup", getTokensByGroup)
3855

3956
function findAliases(token, allTokens){
4057
let aliases = allTokens.filter(t => t.value.referencedToken && t.value.referencedToken.id === token.id)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const tailwindVariables = {
2+
"colors": {
3+
{[ let allColorsToken = @ds.tokensByType("Color") /]}
4+
{[ let groups = @js.getGroups(allColorsToken) /]}
5+
{[ for group in groups ]}
6+
{[ let tokensByGroup = @js.getTokensByGroup(allColorsToken, group) /]}
7+
'{{ group }}': {
8+
{[ for token in tokensByGroup ]}
9+
{[ let tokenName = "" /]}
10+
{[ switch token.name ]}
11+
{[ case "main" ]}{[ tokenName = "DEFAULT" /]}
12+
{[ default ]}{[ tokenName = token.name /]}
13+
{[/]}
14+
'{{ tokenName }}': 'var(--{[ inject "rendered-name" context token /]})',
15+
{[/]}
16+
},
17+
{[/]}
18+
}
19+
}
20+
21+
export default tailwindVariables;

0 commit comments

Comments
 (0)