-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from thomasmattheussen/feature/parser-italics
add a parser function to add fontStyle to typography tokens
- Loading branch information
Showing
17 changed files
with
254 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@tokens-studio/sd-transforms': minor | ||
--- | ||
|
||
BREAKING: add parser that extracts fontStyle from fontWeight and adds it as a separate property on Typography tokens object values. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@tokens-studio/sd-transforms': patch | ||
--- | ||
|
||
Properly take into account fontStyle inside fontWeights values, in both the fontWeights and CSS typography shorthand transforms. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { DeepKeyTokenMap, SingleToken, TokenTypographyValue } from '@tokens-studio/types'; | ||
// @ts-expect-error no type exported for this function | ||
import getReferences from 'style-dictionary/lib/utils/references/getReferences.js'; | ||
// @ts-expect-error no type exported for this function | ||
import usesReference from 'style-dictionary/lib/utils/references/usesReference.js'; | ||
import { fontWeightReg } from '../transformFontWeights.js'; | ||
|
||
function recurse( | ||
slice: DeepKeyTokenMap<false>, | ||
boundGetRef: (ref: string) => Array<SingleToken<false>>, | ||
) { | ||
for (const key in slice) { | ||
const token = slice[key]; | ||
const { type, value } = token; | ||
if (type === 'typography') { | ||
if (typeof value !== 'object') { | ||
continue; | ||
} | ||
let fontWeight = value.fontWeight; | ||
|
||
if (usesReference(fontWeight)) { | ||
let ref = { value: fontWeight } as SingleToken<false>; | ||
while (ref && ref.value && typeof ref.value === 'string' && usesReference(ref.value)) { | ||
try { | ||
ref = Object.fromEntries( | ||
Object.entries(boundGetRef(ref.value)[0]).map(([k, v]) => [k, v]), | ||
) as SingleToken<false>; | ||
} catch (e) { | ||
console.warn(`Warning: could not resolve reference ${ref.value}`); | ||
return; | ||
} | ||
} | ||
fontWeight = ref.value as string; | ||
} | ||
|
||
// cast it to TokenTypographyValue now that we've resolved references all the way, we know it cannot be a string anymore. | ||
const tokenValue = value as TokenTypographyValue; | ||
|
||
if (fontWeight) { | ||
const fontStyleMatch = fontWeight.match(fontWeightReg); | ||
if (fontStyleMatch?.groups?.weight && fontStyleMatch.groups.style) { | ||
// @ts-expect-error fontStyle is not a property that exists on Typography Tokens, we just add it ourselves | ||
tokenValue.fontStyle = fontStyleMatch.groups.style.toLowerCase(); | ||
tokenValue.fontWeight = fontStyleMatch?.groups?.weight; | ||
} | ||
} | ||
} else if (typeof token === 'object') { | ||
recurse(token as unknown as DeepKeyTokenMap<false>, boundGetRef); | ||
} | ||
} | ||
} | ||
|
||
export function addFontStyles(dictionary: DeepKeyTokenMap<false>): DeepKeyTokenMap<false> { | ||
const copy = { ...dictionary }; | ||
const boundGetRef = getReferences.bind({ properties: copy }); | ||
recurse(copy, boundGetRef); | ||
return copy; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.