npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-svg-parser

v1.2.6

Published

A robust SVG to JSON parser built with TypeScript

Downloads

24

Readme

next-svg-parser

npm version Test Coverage

A robust and efficient SVG to JSON parser built with TypeScript, leveraging the power of DOMParser for accurate SVG parsing.

Features

  • Parses SVG strings into a structured JSON format
  • Handles complex, nested SVG structures
  • Supports all SVG elements and attributes
  • Processes SVG with comments, CDATA sections, and namespaces
  • High test coverage (90%+) ensuring reliability
  • Lightweight and easy to integrate

Installation

npm install next-svg-parser

or

pnpm install next-svg-parser

or

yarn add next-svg-parser

address

For more information, see the npm package page.

Usage

  1. When building single page applications (SPA) using Vite, you should:

    // react.svg
    <svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 200 200">
      <circle cx="100" cy="100" r="90" fill="#f0f0f0"/>
      <text x="40" y="110" font-family="monospace" font-size="40" fill="#4a4a4a">&lt;svg&gt;</text>
      <path d="M80 100 L120 100 M110 90 L120 100 L110 110" stroke="#2196f3" stroke-width="4" fill="none"/>
      <text x="130" y="110" font-family="monospace" font-size="40" fill="#4a4a4a">{}</text>
      <path d="M30 130 Q100 170 170 130" stroke="#2196f3" stroke-width="2" fill="none" stroke-dasharray="5,5">
        <animate attributeName="stroke-dashoffset" from="10" to="0" dur="2s" repeatCount="indefinite"/>
      </path>
    </svg>
    // App.tsx
    import reactSVG from "./assets/react.svg?raw"
    import {parserSVG} from "next-svg-parser"
       
    function App() {
    	const svgJosn = parserSVG(reactSVG)
    }
       

    result:

    {
        "tagName": "svg",
        "type": "element",
        "attributes": {
            "xmlns": "http://www.w3.org/2000/svg",
            "width": "200",
            "height": "200",
            "viewBox": "0 0 200 200"
        },
        "children": [
            {
                "tagName": "circle",
                "type": "element",
                "attributes": {
                    "cx": "100",
                    "cy": "100",
                    "r": "90",
                    "fill": "#f0f0f0"
                },
                "children": []
            },
            {
                "tagName": "text",
                "type": "element",
                "attributes": {
                    "x": "40",
                    "y": "110",
                    "font-family": "monospace",
                    "font-size": "40",
                    "fill": "#4a4a4a"
                },
                "children": [
                    {
                        "tagName": "text",
                        "type": "text",
                        "attributes": {},
                        "children": [],
                        "content": "<svg>"
                    }
                ]
            },
            {
                "tagName": "path",
                "type": "element",
                "attributes": {
                    "d": "M80 100 L120 100 M110 90 L120 100 L110 110",
                    "stroke": "#2196f3",
                    "stroke-width": "4",
                    "fill": "none"
                },
                "children": []
            },
            {
                "tagName": "text",
                "type": "element",
                "attributes": {
                    "x": "130",
                    "y": "110",
                    "font-family": "monospace",
                    "font-size": "40",
                    "fill": "#4a4a4a"
                },
                "children": [
                    {
                        "tagName": "text",
                        "type": "text",
                        "attributes": {},
                        "children": [],
                        "content": "{}"
                    }
                ]
            },
            {
                "tagName": "path",
                "type": "element",
                "attributes": {
                    "d": "M30 130 Q100 170 170 130",
                    "stroke": "#2196f3",
                    "stroke-width": "2",
                    "fill": "none",
                    "stroke-dasharray": "5,5"
                },
                "children": [
                    {
                        "tagName": "animate",
                        "type": "element",
                        "attributes": {
                            "attributeName": "stroke-dashoffset",
                            "from": "10",
                            "to": "0",
                            "dur": "2s",
                            "repeatCount": "indefinite"
                        },
                        "children": []
                    }
                ]
            }
        ]
    }
  2. Use the cli command line via node:

    • Add script directives to the scripts option of your package.json file

      "scripts": {
          "generate-svg-json": "next-svg-parser input output"
        }
    • Some application scenarios

      • Processing a single file, specifying the output directory
               
        "generate-svg-json": "next-svg-parser src/assets/react.svg src/json/react.json"
        or
        "generate-svg-json": "next-svg-parser src/assets/react.svg src/json"
      • Processing the entire catalogue
        "generate-svg-json": "next-svg-parser src/assets src/json"
      • Process a single file, specify a specific output file.
        "generate-svg-json": "next-svg-parser src/assets/test.svg src/json/test.json"
      • Process individual files to a specified non-existent directory (automatically creates the directory and writes to the file)
               
        "generate-svg-json": "next-svg-parser src/assets/test.svg src/json1/test.svg"
               
        reslut: src/json1/test.svg
      • Dispose of the whole directory, to a non-existent directory (create this directory and write to it)
        "generate-svg-json": "next-svg-parser src/assets src/json3/"
               
        result: src/json3/**.json

API

parseSVG(svgString: string): SVGNode

Parses an SVG string and returns a JSON representation of the SVG structure.

  • svgString: A string containing valid SVG markup.
  • Returns: An SVGNode object representing the parsed SVG structure.

Types

export interface SVGNode {
    type: 'element' | 'text' | 'cdata';
    tagName?: string;
    attributes?: { [key: string]: string };
    children?: SVGNode[];
    content?: string;
    cdataContent?: string;
}

Testing

This library has a test coverage of over 90%. We use Vitest for running tests and generating coverage reports.

To run the tests:

bash

Copy

npm test

To view the test coverage report:

bash

Copy

npm run coverage

Our test suite covers various scenarios including:

  • Simple and complex SVG structures
  • Nested elements
  • Text content
  • Self-closing tags
  • Multiple attributes
  • Whitespace handling
  • SVG with comments and CDATA sections
  • Namespace handling
  • Edge cases and error handling

Contributing

We welcome contributions! If you'd like to contribute, please follow these steps:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Please ensure that your code adheres to the existing style and that all tests pass. If you're adding new functionality, please include appropriate tests.

Feedback and Issues

If you encounter any issues or have suggestions for improvements, please open an issue on our GitHub repository. We appreciate your feedback and contributions to making this library better.

License

This project is licensed under the MIT License - see the LICENSE file for details.


Made with ❤️ by [Your Name/Organization]

We're constantly working to improve next-svg-parser. If you have any suggestions or find any bugs, please don't hesitate to contribute or reach out!