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

metalsmith-icons

v0.6.0

Published

Generate an icon font file just for the icons your site uses, using Fontello

Downloads

3

Readme

Metalsmith Icons

A Metalsmith plugin for automatically building font icon sets

Overview

Font-based icon sets like Font Awesome are a great way to add icons to your UI, but rely on relatively large (often 50kb+) font files, even if you're only using a few icons across your site.

Fontello is an awesome service that allows you to manually build an icon set from. However, this is annoying to redo manually every time you add or remove icons from your site.

metalsmith-icons finds all the icons you're using, automates your Fontello build, and automatically adds the generated font files and CSS markup to your build. The result is a tiny font file, meaning your UI loads much faster!

The plugin:

  • scans all your HTML files for CSS classes corresponding to icon fonts
  • changes the CSS classes in your HTML files to correspond to the incoming CSS markup from Fontello
  • builds a config file for the Fontello API
  • if you've run the plugin before, the plugin checks a local cache of previously-downloaded font packs against the config file — if they match, it uses the local copy...
  • ...otherwise, it downloads a font pack from Fontello with just the icons you need for your site
  • finally, it adds the CSS and font files from the font pack to your Metalsmith build chain

Installation

$ npm install --save metalsmith-icons

Usage


var Metalsmith = require('metalsmith');
var icons = require('metalsmith-icons')

Metalsmith(__dirname)
.source('./src')
.destination('./dest')
.use(icons({
	sets: 		{	fa:'fontawesome'},
	fontello:	{	name: 'icons'	},
	fontDir: 	'fonts'
}))
.build();

Options

Default options:

var defaults = {
    sets : { fa: fontawesome },
    fontello: {
          name: 'icons',
          css_prefix_text: "icon-",
          css_use_suffix: false,
          hinting: true,
          units_per_em: 1000,
          ascent: 850
    },
    cache: './.icon_cache',
    fontDir: 'fonts',
    CSSDir: 'styles'
};

Any options passed to metalsmith-icons will be merged with the default configuration object.

sets [Object / Boolean]

A mapping of the CSS class you're using for your icons to the underlying font set.

Available sets:

  • Font Awesome — fontawesome

(more sets coming soon, see below...)

Example:

If you're used to the default Font Awesome markup, you're using something like this to declare your icons:

<p><a href="file.pdf"><i class="fa fa-download"></i> Download</a><p>

So, you want to map the CSS class fa to the fontawesome font set.

sets: {	fa:'fontawesome'}

fontello [Object]

Options recognised by the Fontello API. The defaults should be fine unless you want to change the name of the font files, or the CSS class that will end up in the markup.

Don't add a glyphs key, as the plugin does this for you!

cache [String / Boolean]

A path to a folder that will be used to cache font files to save the HTTP lookup for subsequent builds that use the same set of icons. Set to false to disable caching. Defaults to ./.icon_cache.

fontDir [String]

Path within your build to put fonts. Defaults to font (as per Fontello's default)

CSSDir [String]

Path within your build to put the CSS file. Defaults to styles

substitutions [Object]

For some reason, some of the icons in the Font Awesome set use a different CSS class name on Fontello.

If you'd prefer to use the original Font Awesome CSS classes, the plugin will do the substitution for you.

A default set of substitutions are read from substitutions.yml. This page has the classes the Fontello version of Font Awesome is using.

If you find a substitution, it would be amazing if you could submit a pull request on substitutions.yml. However, in the meantime you can just map the substitution in the options:

.use(icons({
	substitutions: {
		fontawesome: {
			chevron-down: down-open
		}
	}
}))

Demo

To see the plugin working, run the demo script:

$ npm run demo

This builds to ./demo/build.

Caveats

This is still in very early stages of development, so currently only supports the Font Awesome font set. More to come soon. Feel free to open an issue or submit a pull request!

To Do

This would also work great as a Gulp plugin — if someone wants to help refactor the source and make it more generic, please get in touch by submitting a pull request.