svg-parser is a JavaScript library designed for converting SVG (Scalable Vector Graphics) document strings or fragments into a HAST (Hypertext Abstract Syntax Tree) compliant JavaScript object. This transformation provides a highly structured, JSON-friendly representation of the SVG, enabling programmatic manipulation, analysis, or further transformation within the unified syntax-tree ecosystem. The current stable version, 2.0.4, was last published in early 2020, indicating a maintenance-focused development cycle. It stands out by integrating directly with the HAST specification, making it a suitable choice for projects that already leverage unified or rehype for content processing, offering a consistent AST format across various document types like HTML and SVG. It boasts zero runtime dependencies.
npm install svg-parserVerified import paths — ran on the pinned version, not inferred.
This code demonstrates how to parse an SVG string into a HAST object using `svg-parser`, then pretty-prints the result and shows how to access properties of the root SVG element.
Ensure SVG input strictly adheres to W3C SVG specifications. Consider validating SVG strings using dedicated SVG validation tools before feeding them to the parser if robustness is critical.
Always use `import { parse } from 'svg-parser';` for projects using ES Modules. For legacy CommonJS projects, dynamic `await import('svg-parser')` can be used, or consider explicit CommonJS builds if available (though not officially provided for `svg-parser`).Ensure you are using the correct named import: `import { parse } from 'svg-parser';`. If in a CommonJS context that doesn't support ESM, this package might not be directly compatible without bundling/transpilation.Add `import { parse } from 'svg-parser';` at the top of your file to import the named export. If in a CommonJS environment, and if the package provides a CJS export, use `const { parse } = require('svg-parser');`.No dependency data recorded yet.