# inkscape-parser

> Reads the content of any inkscape SVG

Latest version **1.0.8** (published 2023-07-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install inkscape-parser
pnpm add inkscape-parser
yarn add inkscape-parser
bun add inkscape-parser
```

Provides the command `inkscape-parser`.

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.8 |
| Published | 2023-07-28 |
| First published | 2023-07-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 13.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | imivi |
| Maintainers | minv |
| Keywords | svg, inkscape, parser, cli |

## Links

- npm: https://www.npmjs.com/package/inkscape-parser
- Repository: https://github.com/imivi/inkscape-parser
- Homepage: https://github.com/imivi/inkscape-parser#readme
- Issues: https://github.com/imivi/inkscape-parser/issues
- npm.io page: https://npm.io/package/inkscape-parser

## Dependencies (3)

- [he](https://npm.io/package/he.md) ^1.2.0
- [@types/he](https://npm.io/package/@types/he.md) ^1.2.0
- [svg-parser](https://npm.io/package/svg-parser.md) ^2.0.4

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.0.8 (latest) — 2023-07-28
- 1.0.7 — 2023-07-28
- 1.0.6 — 2023-07-27
- 1.0.5 — 2023-07-27
- 1.0.3 — 2023-07-27
- 1.0.2 — 2023-07-27
- 1.0.1 — 2023-07-27
- 1.0.0 — 2023-07-27

## README

![Inkscape-parser banner](https://github.com/xmuni/inkscape-parser/blob/main/docs/banner.jpg?raw=true)

# Inkscape-parser

Inkscape-parser parses the elements of an Inkscape SVG file (shapes, text, paths, etc) into an array of objects.

Comes with full Typescript support.

[Get it on NPM](https://www.npmjs.com/package/inkscape-parser): `npm install inkscape-parser`

Here is an example output:

```js
    // Inkscape SVG elements
    {
        "type": "text",
        "layer": "text",
        "id": "text3769",
        "x": 117.16331,
        "y": 137.12299,
        "value": "circle"
    },
    {
        "type": "path",
        "layer": "paths.subpaths",
        "id": "path4965"
    },
    {
        "type": "rect",
        "layer": "shapes",
        "id": "rect846",
        "label": "rect846-brown",
        "width": 36.977692,
        "height": 23.993158,
        "x": 28.509518,
        "y": 40.647236
    },
    {
        "type": "rect",
        "layer": "shapes",
        "id": "rect1000",
        "label": "rect1000-blue",
        "width": 36.066158,
        "height": 64.996948,
        "x": 43.635647,
        "y": 82.519333
    },
    // [...]
```

## Features

This package is built on top of [svg-parser](https://github.com/Rich-Harris/svg-parser) and handles a few extra things for you:

* The output is a simple array rather than a tree, making it easy to display and convert into CSV or other formats.
* Any multi-line text is merged into a single element from multiple <tspan> tags.
* Additionally, any html entities (\&gt; \&lt; etc) in text elements are automatically unescaped using [`he`](https://www.npmjs.com/package/he)
* The inkscape label for each element is included.
* Each element lists all of its parent layers (delimited by a dot: for example "root_layer.sub_layer.sub_sub_layer") according to their inkscape labels.
* Includes a CLI for convenience.

## How to use

You can install it [as an NPM package](https://www.npmjs.com/package/inkscape-parser) or run it directly via the included CLI tool.

**Install from NPM:** `npm install inkscape-parser`

Then import the `parseInkscape` function from this package:

```js
import { parseInkscape } from "inkscape-parser";

// You should provide the inkscape svg as a string
const svgString = `
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!-- Created with Inkscape (http://www.inkscape.org/) -->

    <svg
    width="297mm"
    height="210mm"
    viewBox="0 0 297 210"
    version="1.1"
    id="svg5"

    [...]
`

// parseInkscape() returns an object with the parsed SVG elements
const { elements } = parseInkscape(svgString)

console.log(elements)

/* Prints something like the following:
[
    {
        "type": "text",
        "layer": "text",
        "id": "text3769",
        "x": 117.16331,
        "y": 137.12299,
        "value": "circle"
    },
    [...]
]
*/
```

### Options

`parseInkscape` also accepts an **Options** object:

```js
const defaultOptions = {
    include: [], // Only the listed inkscape tag names will be returned
    exclude: [], // None of the listed inkscape tag names will be returned
    unescape: true, // Escape any HTML entities inside text elements (default: true)
    layerSeparator: ".", // Used to delimit the names of parent layers (e.g.: parent.child.subchild)
}
const { elements } = parseInkscape(svgString, defaultOptions)
```

### Page properties and unprocessed elements

`parseInkscape` also returns the page properties (width, height, etc) and the tree of unprocessed elements as parsed by [svg-parser](https://github.com/Rich-Harris/svg-parser).

```js
const { pageProperties, unprocessed } = parseInkscape(svgString)

// Print the Inkscape page properties
console.log(pageProperties)
```

Output:

```js
"pageProperties": {
    "width": "297mm",
    "height": "210mm",
    "viewBox": "0 0 297 210",
    "version": 1.1,
    "id": "svg5",
    "inkscape:version": "1.1.2 (b8e25be833, 2022-02-05)",
    "sodipodi:docname": "inkscape2.svg",
    "xmlns:inkscape": "http://www.inkscape.org/namespaces/inkscape",
    "xmlns:sodipodi": "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd",
    "xmlns": "http://www.w3.org/2000/svg",
    "xmlns:svg": "http://www.w3.org/2000/svg"
}
```

## CLI usage

`npx inkscape-parser ./drawing.svg ./output.json`

* the first argument is the path to any inkscape file
* the second argument is the path where the parsed data will be saved (JSON format)

---
_Source: https://npm.io/package/inkscape-parser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
