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

@shgysk8zer0/svg-use-symbols

v1.1.3

Published

An npm package for use with `<svg><use xlink:href=""></use></svg>`

Downloads

247

Readme

@shgysk8zer0/svg-use-symbols

An npm package for use with <svg><use xlink:href=""></use></svg>


CodeQL Node CI Lint Code Base

GitHub license GitHub last commit GitHub release GitHub Sponsors

npm node-current npm bundle size gzipped npm

GitHub followers GitHub forks GitHub stars Twitter Follow

Donate using Liberapay


Installation

npm i @shgysk8zer0/svg-use-symbols

Supports

  • Parsing from JSON and YAML files
    • Creating multiple outputs by using an array of objects with output and icons
    • Creating a single output file by using simple id and path object
  • Creating from directories
  • Fetching SVGs from URLs
  • Migrating from svg-sprite-generate (converts CSV to JSON or YAML)

Usage

In HTML

This generates a series of <symbol>s, which have the counterpart of <use> for using the icons on a webiste:

<svg>
  <use xlink:href="/path/to/icons.svg#id"></use>
</svg>

CLI

Usage: @shgysk8zer0/svg-use-symbols [options]

An npm package for use with `<svg><use xlink:href=""></use></svg>`

Options:
  -V, --version                output the version number
  -c, --config [config]        JSON or YAML config file
  -d, --directory [directory]  path to directory of SVGs
  -e, --encoding [encoding]    encoding (default: "utf8")
  -f, --format [format]        output format for migrating from CSV (default: "json")
  -l, --list [list]            comma separated list of SVGs
  -m, --migrate [migrate]      path to deprecated CSV config file
  -o, --output [output]        output file
  -h, --help                   display help for command
svg-use-symbols -c path/to/config.yml -o img/icons.svg
svg use-symbols -d path/to/svgs/ -o img/icons.svg
svg-use-symbols -l icons/1.svg,icons/2.svg -o img/icons.svg

Node

Simple usage

import { generateSymbols } from '@shgysk8zer0/svg-use-symbols';

await generateSymbols('config.json');

Using require()

const { generateSymbols } = require('@shgysk8zer0/svg-use-symbols');

// Top-level await not allowed
generateSymbols('config.json').catch(console.error);

Advanced usage

import { generateSymbol, writeSVG } from '@shgysk8zer0/svg-use-symbols';
const controller = new AbortController();

const config = {
  "file-media": "octicons/file-media.svg",
  "external": "https://example.com/external.svg",
};

const symbols = await Promise.all(
  Object.entries(config).map(async ([id, path]) => {
    try {
      const symbol = await generateSymbol(id, path, { encoding: 'utf8', signal });
      // Mutate symbol, perhaps?
      return symbol;
    } catch(err) {
      console.error(err);
      controller.abort();
    }
  })
);

await writeSVG('output.svg', symbols, { encoding: 'utf8' });

Config files

This supports YAML, JSON, and to a limited extent, CSV. CSV is temporarily offered for those migrating from svg-sprite-generator and will be removed in v2. Please see the section on migrating.

For JSON and YAML, two different formats are supported:

{
  "id": "path/to/icon.svg",
  "another": "https://example.com/path/to/icon.svg"
}

This will create <symbol id=":id">s with id taken from the key and contents from the SVG file at the given path/URL. This MUST have output (-o or --output) set. This also applies for the temporarily supported CSV config files.

It may also use:

[
  {
    "output": "path/to/output.svg",
    "icons": {
      "id": "path/to/icon.svg",
      "another": "https://example.com/path/to/icon.svg"
    }
  },
  {
    "output": "path/to/output2.svg",
    "icons": {
      "id": "path/to/another-icon.svg",
      "another": "https://example.com/path/to/another-icon.svg"
    }
  }
]

This will create output files output.svg and output2.svg, each containing there respective <symbol>s.

Migrating from svg-sprite-generator

svg-use-symbols -m path/to/config.csv -o path/to/config.json
# Or
svg-use-symbols --migrate path/to/config.csv --format json --output /path/to/config.json
# Or
svg-use-symbols --migrate path/to/config.csv --format yaml --output /path/to/config.yaml