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.
npm install css-font-parserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `parseFont` and `parseFontFamily` to extract structured data from CSS font strings, including handling complex font declarations and showing basic error handling for malformed input.
Update your import statements to use ESM syntax: `import { parseFont, parseFontFamily } from 'css-font-parser';`. If using Node.js, ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`) or use a bundler that handles ESM-to-CJS transpilation.Ensure that the input strings provided to `parseFont` and `parseFontFamily` strictly adhere to valid CSS `font` or `font-family` syntax, respectively. For broader CSS parsing, consider a more comprehensive CSS parser library.
Validate input CSS strings before passing them to the parser where possible. Wrap parsing calls in `try...catch` blocks to gracefully handle potential errors and provide user-friendly feedback for invalid CSS input. Inspect the output object for missing properties to detect incomplete parses.
Ensure 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.Always 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 */ }`.No dependency data recorded yet.