Skip to content

Commit

Permalink
fix references (#704)
Browse files Browse the repository at this point in the history
* fix references

* changeset
  • Loading branch information
lukasoppermann authored Jul 24, 2023
1 parent 3532f60 commit aeb3b30
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-ties-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/primitives': patch
---

fix for figma references
25 changes: 11 additions & 14 deletions src/formats/jsonFigma.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import StyleDictionary from 'style-dictionary'
import {format} from 'prettier'
import type {FormatterArguments} from 'style-dictionary/types/Format'
import {transformNamePathToFigma} from '../transformers/namePathToFigma'
const {sortByReference} = StyleDictionary.formatHelpers

const replaceRegEx = /(?:{|})/g

const isReference = (string: string): boolean => /^\{([^\\]*)\}$/g.test(string)

const getReference = (dictionary: StyleDictionary.Dictionary, refString: string) => {
const getReference = (
dictionary: StyleDictionary.Dictionary,
refString: string,
platform: StyleDictionary.Platform,
) => {
if (isReference(refString)) {
// retrieve reference token
const refToken = dictionary.getReferences(refString)[0]
// return full reference
return [refToken.attributes?.collection, ...refToken.path].filter(Boolean).join('/')
return [refToken.attributes?.collection, transformNamePathToFigma(refToken, platform)].filter(Boolean).join('/')
}
return undefined
}

const getFigmaType = (type: string): string => {
Expand All @@ -31,25 +33,20 @@ const getFigmaType = (type: string): string => {
* @param arguments [FormatterArguments](https://github.com/amzn/style-dictionary/blob/main/types/Format.d.ts)
* @returns formatted `string`
*/
export const jsonFigma: StyleDictionary.Formatter = ({
dictionary,
file: _file,
platform: _platform,
}: FormatterArguments) => {
export const jsonFigma: StyleDictionary.Formatter = ({dictionary, file: _file, platform}: FormatterArguments) => {
// sort tokens by reference
const tokens = dictionary.allTokens.sort(sortByReference(dictionary)).map(token => {
const {attributes, value, $type, comment: description, original, alpha, mix} = token
const {mode, collection, scopes} = attributes || {}
const tokenName = token.name.replace(replaceRegEx, '')
return {
name: tokenName,
name: token.name,
value,
type: getFigmaType($type),
alpha,
isMix: mix ? true : undefined,
description,
refId: [collection, tokenName].filter(Boolean).join('/'),
reference: getReference(dictionary, original.value),
refId: [collection, token.name].filter(Boolean).join('/'),
reference: getReference(dictionary, original.value, platform),
collection,
mode,
scopes,
Expand Down
29 changes: 17 additions & 12 deletions src/transformers/namePathToFigma.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import type StyleDictionary from 'style-dictionary'

export const transformNamePathToFigma = (
token: StyleDictionary.TransformedToken,
options?: StyleDictionary.Platform,
): string => {
let pathArray = token.path
if (['fgColor', 'borderColor', 'bgColor'].includes(pathArray[0]) && pathArray.length === 3) {
pathArray = [pathArray[0], `${pathArray[1]}-${pathArray[2]}`]
}
return (
[options?.prefix, ...pathArray]
// remove undefined if exists
.filter((part: unknown): part is string => typeof part === 'string')
.join('/')
)
}

/**
* @description converts the [TransformedToken's](https://github.com/amzn/style-dictionary/blob/main/types/TransformedToken.d.ts) `.path` array to a specific figma name
* @type name transformer — [StyleDictionary.NameTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
Expand All @@ -8,16 +24,5 @@ import type StyleDictionary from 'style-dictionary'
*/
export const namePathToFigma: StyleDictionary.Transform = {
type: `name`,
transformer: (token: StyleDictionary.TransformedToken, options?: StyleDictionary.Platform): string => {
let pathArray = token.path
if (['fgColor', 'borderColor', 'bgColor'].includes(pathArray[0]) && pathArray.length === 3) {
pathArray = [pathArray[0], `${pathArray[1]}-${pathArray[2]}`]
}
return (
[options?.prefix, ...pathArray]
// remove undefined if exists
.filter((part: unknown): part is string => typeof part === 'string')
.join('/')
)
},
transformer: transformNamePathToFigma,
}

0 comments on commit aeb3b30

Please sign in to comment.