@mithray/font-cacher
v0.0.130
Published
For loading and downloading Fonts locally, only Google Fonts is supported for now but this may change. Fonts are retrieved from a font url, and returned. If already present on disk they will be loaded from disk and returned. If not present on disk they wi
Downloads
11
Readme
Font Cacher
For loading and downloading Fonts locally, only Google Fonts is supported for now but this may change. Fonts are retrieved from a font url, and returned. If already present on disk they will be loaded from disk and returned. If not present on disk they will be downloaded, saved to disk, and returned. Useful for integrating into SSGs or other site builders.
Usage
import { getFont } from '@mithray/font-cacher
const options = {cachePath: './fonts'}
const url = 'https://fonts.googleapis.com/css2?family=Playfair+Display'
const fontFiles = await getFont(options)(url)
console.log(fontFiles)
/*
Output is an array of files, which will be saved to disk
[
{
name: 'https!fonts.googleapis.com!css2!family=Playfair+Display',
filename: 'https!fonts.googleapis.com!css2!family=Playfair+Display',
url: 'https://fonts.googleapis.com/css2?family=Playfair+Display',
mimetype: 'text/css',
ext: 'css',
data: <Buffer 2f 2a 20 63 79 72 69 6c 6c 69 63 20 2a 2f 0a 40 66 6f 6e 74 2d 66 61 63 65 20 7b 0a 20 20 66 6f 6e 74 2d 66 61 6d 69 6c 79 3a 20 27 50 6c 61 79 66 61 ... 1535 more bytes>,
digest: 'qBv0a2kEUGwSh+7GjUAk6neS0kM9CK6tQxkFHvt1NPd09td0FnwUNOX/RrMjZK4W0+Rl3Wqw1hxkCH8LA5+QDw=='
},
...
{
name: 'https!fonts.gstatic.com!s!playfairdisplay!v37!nuFvD-vYSZviVYUb_rj3ij__anPXJzDwcbmjWBN2PKdFvXDT.woff2',
filename: 'https!fonts.gstatic.com!s!playfairdisplay!v37!nuFvD-vYSZviVYUb_rj3ij__anPXJzDwcbmjWBN2PKdFvXDT.woff2',
url: 'https://fonts.gstatic.com/s/playfairdisplay/v37/nuFvD-vYSZviVYUb_rj3ij__anPXJzDwcbmjWBN2PKdFvXDTbtPK_l2qI0s.woff2',
mimetype: 'font/woff2',
ext: 'woff2',
data: <Buffer 77 4f 46 32 00 01 00 00 00 00 2c 5c 00 0f 00 00 00 00 6d 98 00 00 2b fd 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1a 81 ... 11306 more bytes>,
digest: '9nkMIqw3pwNBKI7O0umHwYJJOBCsmZS+nsEPsBRKI4XKBvjKPQlORTYoweGPwrttT9Qzstkczc883FKOAB3s7g=='
},
]
*/
By default, the files will be saved in './fonts'. If you want to change the directory, pass `{cachePath: './some_path_here'}` as first argument to getFont. If you want to disable saving to disk,because you want to integrate it into some other build step, you can pass `{cachePath: null}`.
Options
opts =
{ cachePath: './fonts' // Any Path
, verbosity: 'silent' // 'silent', 'debug', 'info'
}