UNPKG

@sinchsmb/mktheme

Version:

Util that allow to make frontend theme from Figma theme export file

55 lines (45 loc) 1.09 kB
const _ = require('lodash'); const tokenRegExp = /{([\w-.]+)}/g; const strictTokenRegExp = /^{([\w-.]+)}$/; /** * Resolve Figma theme refs * @param {object} theme * @return {object} */ function resolveLinks(theme) { /** * @param {String} key * @return {String} */ function get(key) { const result = _.get(theme, key); return resolve(_.isObject(result) ? result.value : result); } /** * @param {String} value * @return {String} */ function resolve(value) { if (typeof value !== 'string') { return value; } // normalize links value = value.replace(/\$([\w-.]+)/g, '{$1}'); if (strictTokenRegExp.test(value)) { const path = value.match(strictTokenRegExp)[1]; return get(path); } if (tokenRegExp.test(value)) { return value.replace(tokenRegExp, (match, path) => { return get(path); }); } return value; } return _.cloneDeepWith(theme, function (value) { if (typeof value === 'string') { return resolve(value); } }); } module.exports = { resolveLinks };