Skip to content

Commit

Permalink
ci(build): create custom transform to remove line break characters fr…
Browse files Browse the repository at this point in the history
…om comments created by ts descriptions
  • Loading branch information
druhill committed Dec 18, 2024
1 parent 12c4048 commit 8677662
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
19 changes: 14 additions & 5 deletions scripts/style-dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ Copyright © 2024 The Sage Group plc or its licensors. All Rights reserved

import StyleDictionary from 'style-dictionary'
import { register } from '@tokens-studio/sd-transforms'
import { mapDescriptionToComment } from './transforms/mapDescriptionToComment.js'

StyleDictionary.registerTransform({
name: 'custom/descriptionToComment',
type: 'attribute',
filter: token => token['description'],
transform: token => mapDescriptionToComment(token),
});

const groups = {
css: [
Expand All @@ -12,7 +20,7 @@ const groups = {
'transition/css/shorthand',
'typography/css/shorthand',
'name/kebab',
'ts/descriptionToComment',
'custom/descriptionToComment',
'ts/size/px',
'ts/opacity',
'ts/size/lineheight',
Expand All @@ -22,8 +30,8 @@ const groups = {
'ts/color/modifiers'
],
scss: [
'custom/descriptionToComment',
'name/kebab',
'ts/descriptionToComment',
'ts/size/px',
'ts/opacity',
'ts/size/lineheight',
Expand All @@ -33,8 +41,8 @@ const groups = {
'ts/color/modifiers'
],
js: [
'custom/descriptionToComment',
'name/camel',
'ts/descriptionToComment',
'ts/size/px',
'ts/opacity',
'ts/size/lineheight',
Expand All @@ -44,8 +52,8 @@ const groups = {
'ts/color/modifiers'
],
json: [
'custom/descriptionToComment',
'name/camel',
'ts/descriptionToComment',
'ts/size/px',
'ts/opacity',
'ts/size/lineheight',
Expand All @@ -55,8 +63,8 @@ const groups = {
'ts/color/modifiers'
],
mobile: [
'custom/descriptionToComment',
'name/camel',
'ts/descriptionToComment',
'ts/size/px',
'ts/opacity',
'ts/size/lineheight',
Expand All @@ -77,3 +85,4 @@ export {
StyleDictionary,
groups
}

13 changes: 13 additions & 0 deletions scripts/transforms/mapDescriptionToComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { DesignToken } from 'style-dictionary/types';

/**
* Helper: Maps the token description to a style dictionary comment attribute - this will be picked up by some Style Dictionary
* formats and automatically output as code comments
*/
export function mapDescriptionToComment(token: DesignToken): DesignToken {
// intentional mutation of the original object
const _t = token;
// Replace carriage returns with just newlines
_t.comment = _t['description'].replace(/\r?\n|\r/g, ' ');
return _t;
}

0 comments on commit 8677662

Please sign in to comment.