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

node-font2base64

v0.9.2

Published

Convert font to base64 url or src, and to inject src into style files

Downloads

7,564

Readme

node-font2base64

Convert font to base64 url or src, and to inject src into style files

Installation

$ npm install node-font2base64
# or
$ yarn add node-font2base64 --dev

Import

CommonJS modules

const font2base64 = require('node-font2base64');
const {
  encodeToDataUrl,
  encodeToDataSrc,
  encodeToDataUrlSync,
  encodeToDataSrcSync,
  injectBase64,
  injectBase64Sync,
} = require('node-font2base64');

ECMAScript modules

import {
  encodeToDataUrl,
  encodeToDataSrc,
  encodeToDataUrlSync,
  encodeToDataSrcSync,
  injectBase64,
  injectBase64Sync,
} from 'node-font2base64';

API

encodeToDataUrl (fontpath)

  • Convert font file(s) to font-media base64 data url asynchronously
  • fontpath: {string} | {Array{string}}
  • Returns: {Promise} containing {string} | {Array{string}}
const dataUrl = await encodeToDataUrl('fonts/myfont-regular.ttf');
// => data:font/truetype;charset=utf-8;base64,<base64>

encodeToDataUrlSync (fontpath)

  • Convert font file(s) to font-media base64 data url synchronously
  • fontpath: {string} | {Array{string}}
  • Returns: {string} | {Array{string}}
const dataUrl = encodeToDataUrlSync('fonts/myfont-regular.woff');
// => data:application/font-woff;charset=utf-8;base64,<base64>

encodeToDataSrc (fontpath)

  • Convert font file(s) to font-media base64 data src asynchronously
  • fontpath: {string} | {Array{string}}
  • Returns: {Promise} containing {string} | {Array{string}}
const dataSrc = await encodeToDataSrc('fonts/myfont-regular.ttf');
// => url(data:font/truetype;charset=utf-8;base64,<base64>) format('truetype')

encodeToDataSrcSync (fontpath)

  • Convert font file(s) to font-media base64 data src synchronously
  • fontpath: {string} | {Array{string}}
  • Returns: {string} | {Array{string}}
const dataSrc = encodeToDataSrcSync('fonts/myfont-regular.woff');
// => url(data:application/font-woff;charset=utf-8;base64,<base64>) format('woff')

injectBase64 (fontpath, stylepath[, options])

  • Replace font url(s) in style file(s) with font-media base64 data src asynchronously
  • fontpath: {string} | {Array{string}}
  • stylepath: {string} | {Array{string}}
  • options: {Object}
    • validator: {Function}: (abs|base_font_url, abs|base_font_path, original_font_url, original_font_path) => boolean
      • check if font url in a style file matches to font file's basename
      • default to comparing both paths' basenames
    • fontTypes: {Array{string}}
      • allowed font ext names
      • default to ['.svg', '.ttf', '.otf', '.eot', '.sfnt', '.woff2', '.woff']
    • cssTypes: {Array{string}}
      • allowed style ext names
      • default to ['.css', '.scss', '.less']
    • resave: {boolean}
      • resave modified style files
      • default to true
    • fullpathMatch: {boolean}
      • match full url / path to validate paths
      • default to false
  • Returns: {Promise} containing {true | Error} | {Array{result} | Error}
const result = await injectBase64('./fonts', './styles');
// => true

const result = await injectBase64('./fonts', './styles', { resave: false });
// => [{ modified: true, filepath: './styles/example.css', content: '...' }]

injectBase64Sync (fontpath, stylepath[, options])

  • Replace font url(s) in style file(s) with font-media base64 data src synchronously
  • fontpath: {string} | {Array{string}}
  • stylepath: {string} | {Array{string}}
  • options: {Object}
    • validator: {Function}: (abs|base_font_url, abs|base_font_path, original_font_url, original_font_path) => boolean
      • check if font url in a style file matches to font file's basename
      • default to comparing both paths' basenames
    • fontTypes: {Array{string}}
      • allowed font ext names
      • default to ['.svg', '.ttf', '.otf', '.eot', '.sfnt', '.woff2', '.woff']
    • cssTypes: {Array{string}}
      • allowed style ext names
      • default to ['.css', '.scss', '.less']
    • resave: {boolean}
      • resave modified style files
      • default to true
    • fullpathMatch: {boolean}
      • match full url / path to validate paths
      • default to false
  • Returns: {true | Error} | {Array{result} | Error}
const result = injectBase64Sync('./fonts', './styles');
// => true

const result = injectBase64Sync('./fonts', './styles', { resave: false });
// => [{ modified: true, filepath: './styles/example.css', content: '...' }]

injectBase64.fromContent (fontpath, content[, options])

  • Replace font url(s) in style content with font-media base64 data src asynchronously
  • fontpath: {string} | {Array{string}}
  • content: {string}
  • options: {Object}
    • validator: {Function}: (abs|base_font_url, abs|base_font_path, original_font_url, original_font_path) => boolean
      • check if font url in a style file matches to font file's basename
      • default to comparing both paths' basenames
    • fontTypes: {Array{string}}
      • allowed font ext names
      • default to ['.svg', '.ttf', '.otf', '.eot', '.sfnt', '.woff2', '.woff']
    • fullpathMatch: {boolean}
      • match full url / path to validate paths
      • default to false
    • root: {string}
      • directory path which the content originated from
      • use basenames to compare font urls in style files if omitted
  • Returns: {Promise} containing {result | Error}
const result = await injectBase64.fromContent('./fonts', '...@font-face {...');
// => [{ modified: true, content: '...' }]

injectBase64Sync.fromContent (fontpath, content[, options])

  • Replace font url(s) in style content with font-media base64 data src synchronously
  • fontpath: {string} | {Array{string}}
  • content: {string}
  • options: {Object}
    • validator: {Function}: (abs|base_font_url, abs|base_font_path, original_font_url, original_font_path) => boolean
      • check if font url in a style file matches to font file's basename
      • default to comparing both paths' basenames
    • fontTypes: {Array{string}}
      • allowed font ext names
      • default to ['.svg', '.ttf', '.otf', '.eot', '.sfnt', '.woff2', '.woff']
    • fullpathMatch: {boolean}
      • match full url / path to validate paths
      • default to false
    • root: {string}
      • directory path which the content originated from
      • use basenames to compare font urls in style files if omitted
  • Returns: {result | Error}
const result = injectBase64Sync.fromContent('./fonts', '...@font-face {...');
// => [{ modified: true, content: '...' }]

injectBase64.fromBuffer (fontpath, buffer[, options])

  • Replace font url(s) in style buffer with font-media base64 data src asynchronously
  • fontpath: {string} | {Array{string}}
  • buffer: {Buffer}
  • options: {Object}
    • validator: {Function}: (abs|base_font_url, abs|base_font_path, original_font_url, original_font_path) => boolean
      • check if font url in a style file matches to font file's basename
      • default to comparing both paths' basenames
    • fontTypes: {Array{string}}
      • allowed font ext names
      • default to ['.svg', '.ttf', '.otf', '.eot', '.sfnt', '.woff2', '.woff']
    • fullpathMatch: {boolean}
      • match full url / path to validate paths
      • default to false
    • root: {string}
      • directory path which the buffer originated from
      • use basenames to compare font urls in style files if omitted
  • Returns: {Promise} containing {result | Error}
const result = await injectBase64.fromBuffer('./fonts', Buffer 54 65 73 74...)
// => [{ modified: true, content: '...' }]

injectBase64Sync.fromBuffer (fontpath, content[, options])

  • Replace font url(s) in style buffer with font-media base64 data src synchronously
  • fontpath: {string} | {Array{string}}
  • buffer: {Buffer}
  • options: {Object}
    • validator: {Function}: (abs|base_font_url, abs|base_font_path, original_font_url, original_font_path) => boolean
      • check if font url in a style file matches to font file's basename
      • default to comparing both paths' basenames
    • fontTypes: {Array{string}}
      • allowed font ext names
      • default to ['.svg', '.ttf', '.otf', '.eot', '.sfnt', '.woff2', '.woff']
    • fullpathMatch: {boolean}
      • match full url / path to validate paths
      • default to false
    • root: {string}
      • directory path which the buffer originated from
      • use basenames to compare font urls in style files if omitted
  • Returns: {result | Error}
const result = injectBase64Sync.fromBuffer('./fonts', Buffer 54 65 73 74...)
// => [{ modified: true, content: '...' }]

MIT Licensed