Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/danielroe/postcss-capsize. PostCSS plugin to inject Capsize font metrics
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164import type { FontMetrics } from '@capsizecss/core'import type { Declaration, Helpers, PluginCreator, Rule } from 'postcss'import { createStyleObject } from '@capsizecss/core'
export interface PluginOptions { metrics: Record<string, FontMetrics>}
function useDeclare( declaration: Declaration, Declaration: Helpers['Declaration'],) { function declareOne([prop, value]: [prop: string, value: string]) { declaration.after( new Declaration({ prop: prop.replace(/[A-Z]/g, r => `-${r.toLowerCase()}`), value, }), ) }
function declare(props: Record<string, string>) { Object.entries(props).forEach(declareOne) }
return declare}
function useParentDeclare( parent: Rule, Declaration: Helpers['Declaration'], Rule: Helpers['Rule'],) { function declareOnParent([selector, props]: [ selector: string, props: Record<string, string | number>, ]) { parent.after( new Rule({ selector: parent.selector + selector, source: parent.source, nodes: Object.entries(props).map( ([prop, value]) => new Declaration({ prop: prop.replace(/[A-Z]/g, r => `-${r.toLowerCase()}`), value: String(value), }), ), }), ) }
function declareAllOnParent( props: Record<string, Record<string, string | number>>, ) { Object.entries(props).forEach(declareOnParent) }
return declareAllOnParent}
interface FontConfig { size: string fontFamily: string gap: string}
const plugin: PluginCreator<PluginOptions> = (ctx) => { /* c8 ignore next */ const { metrics = {} } = ctx || {}
const fontFamilies = Object.keys(metrics)
const matcher = new RegExp( `^(?<size>\\d+)px (?<family>(${ fontFamilies.join('|') })) (?<gap>\\d+)px$`, )
function addCapsizedRules( fontConfig: FontConfig, source: Declaration, helpers: Helpers, ) { const { size, fontFamily, gap } = fontConfig const { Declaration, Rule } = helpers
const declare = useDeclare(source, Declaration) const declareOnParent = useParentDeclare( source.parent as Rule, Declaration, Rule, )
const values = createStyleObject({ fontMetrics: metrics[fontFamily], fontSize: Number(size), lineGap: Number(gap), })
declare({ lineHeight: values.lineHeight, })
declareOnParent({ '::before': values['::before'], '::after': values['::after'], })
source.remove() }
return { postcssPlugin: 'postcss-capsize',
Declaration: { 'font-metrics': (declaration, helpers) => { const { size, family: fontFamily, gap, } = declaration.value.match(matcher)?.groups || {}
if (!size || !fontFamily || !gap) { throw new Error( 'Correct syntax is `font-metrics: [font-size]px [font-family] [line-gap]px;', ) }
const declare = useDeclare(declaration, helpers.Declaration)
declare({ fontFamily, fontSize: `${size}px`, })
addCapsizedRules({ size, fontFamily, gap }, declaration, helpers) }, 'line-gap': (declaration, helpers) => { let fontFamily!: string let size!: string const gap = (declaration.value.match(/^(\d+)px$/) || [])[1]
declaration.parent?.walkDecls('font-family', (d) => { fontFamily = d.value .split(',') .map(val => val.trim().replace(/['"]/g, '')) .find(val => fontFamilies.includes(val))! })
declaration.parent?.walkDecls('font-size', (d) => { size = (d.value.match(/^(\d+)px$/) || [])[1] })
addCapsizedRules({ size, fontFamily, gap }, declaration, helpers) }, }, }}
plugin.postcss = true
export default plugin