CSS Font Value Parser
css-font-parser is a lightweight JavaScript utility designed to accurately parse CSS `font` shorthand and `font-family` property values. It extracts structured data such as font size, line height, and a list of font families from a given CSS string. The package is currently at version 2.0.1, with its last publication approximately a year ago. Its release cadence is driven by necessary feature enhancements or bug fixes rather than strict timeframes, reflecting its focused scope. The library's primary differentiation lies in its simplicity and direct focus on parsing these specific CSS properties, providing a programmatic way to deconstruct complex font declarations without the overhead of a full CSS parser. It is particularly useful for tools that need to analyze or manipulate font styles based on their CSS declarations.
Common errors
-
TypeError: __WEBPACK_IMPORTED_MODULE_0__.parseFont is not a function
cause This error typically occurs in a bundled environment (like Webpack or Vite) when attempting to use a CommonJS `require` for an ESM-only library, or when the bundler cannot correctly resolve the named exports of an ESM module.fixEnsure you are using ESM named imports: `import { parseFont } from 'css-font-parser';`. Verify your bundler configuration supports ESM and is correctly transpiling or resolving modules. If running in Node.js, confirm your `package.json` has `"type": "module"` or use a `.mjs` file extension. -
Cannot read properties of undefined (reading 'font-size') or similar property access error on parser output.
cause This error occurs when attempting to access a property (like `font-size`) on the object returned by `parseFont`, but the property is missing because the input CSS string was invalid or did not contain that specific font component.fixAlways check for the existence of properties on the parsed object before accessing them, especially when dealing with potentially malformed user input. For example, `if (parsedFont['font-size']) { /* use font-size */ }`.
Warnings
- breaking Version 2.0.0 of `css-font-parser` likely transitioned to an ECMAScript Modules (ESM) primary distribution, dropping direct CommonJS (CJS) compatibility without explicit default exports or wrapper files. This change is typical for modern JavaScript libraries to align with browser and Node.js standards.
- gotcha The library is specifically designed to parse CSS `font` shorthand and `font-family` properties. It does not provide a full CSS parsing solution. Attempting to pass unrelated CSS properties or complex stylesheets will lead to incomplete or incorrect results.
- gotcha The parser might be strict with malformed or invalid CSS font syntax. Depending on the exact malformation, it might return an empty or partial result, or throw a parsing error. Its error reporting for invalid input may not be highly descriptive.
Install
-
npm install css-font-parser -
yarn add css-font-parser -
pnpm add css-font-parser
Imports
- parseFont
const { parseFont } = require('css-font-parser');import { parseFont } from 'css-font-parser'; - parseFontFamily
const parseFontFamily = require('css-font-parser').parseFontFamily;import { parseFontFamily } from 'css-font-parser'; - FontDeclaration
import type { FontDeclaration, FontFamily } from 'css-font-parser';
Quickstart
import { parseFont, parseFontFamily } from 'css-font-parser';
// Example 1: Parsing a basic font shorthand value
const parsedFont1 = parseFont('15px sans-serif');
console.log('Parsed Font 1:', parsedFont1);
/* Expected output:
{
'font-family': ['sans-serif'],
'font-size': '15px'
}
*/
// Example 2: Parsing a more complex font shorthand with line-height and multiple font families
const parsedFont2 = parseFont('15px/18px "Neue Helvetica", Helvetica, sans-serif');
console.log('Parsed Font 2:', parsedFont2);
/* Expected output:
{
'font-family': ['Neue Helvetica', 'Helvetica', 'sans-serif'],
'font-size': '15px',
'line-height': '18px'
}
*/
// Example 3: Parsing only a font-family string
const parsedFontFamily = parseFontFamily('"Neue Helvetica", Helvetica, system-ui, sans-serif');
console.log('Parsed Font Family:', parsedFontFamily);
/* Expected output:
['Neue Helvetica', 'Helvetica', 'system-ui', 'sans-serif']
*/
// Example with potential error handling (library's error behavior not explicitly defined, so demonstrating usage)
try {
const invalidFont = parseFont('invalid-font-value');
console.log('Parsed Invalid Font (may be incomplete or throw):', invalidFont);
} catch (e) {
console.error('Error parsing invalid font:', e.message);
}