Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: New getFontInfo function #70

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Bug fix; updated README
  • Loading branch information
alexmensch committed Jul 24, 2024
commit baacf91f99e3dc42940318b765f2f351485cd0f1
45 changes: 33 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -138,27 +138,48 @@ downloader.hook('download:complete', () => {
await downloader.execute()
```

### `getFontInfo(url: string, option?: DownloadOptions): Map<string, string>, string`
### `getFontInfo(url: string, option?: DownloadOptions): fontMaps: Map<string, string>, localCSS: string`

Use this function if you'd like more control over font download caching for your project. For example, using [Eleventy Fetch](https://www.11ty.dev/docs/plugins/fetch/#fetch), which incorporates local caching.

```ts
const { css, fonts } = getFontInfo('https://fonts.googleapis.com/css2?family=Roboto', {
// fontsPath is the path prepended to the local font name in the @font-face url()
const fontsPath = './fonts'
const cssPath = './css'

const { localCSS, fontMaps } = await getFontInfo('https://fonts.googleapis.com/css2?family=Roboto', {
base64: false,
overwriting: false,
outputDir: './',
stylePath: 'fonts.css',
fontsDir: 'fonts',
fontsPath: './fonts'
fontsPath: fontsPath
})

for (const [url, filename] of fonts) {
// Save locally to `fontsPath/filename`
await yourDownloadFunction(url, `${fontsPath}/${filename}`)
const result = async function () {
let success: boolean = true

try {
await fs.writeFile(cssPath, localCSS)
} catch (error) {
success = false
console.error(`Failed to save CSS`, error)
}

return success
}

const fs = require('fs')
fs.writeFileSync(`${outputDir}/${stylePath}`, css)
const result = async function () {
let success: boolean = true

for (const [url, filename] of fontMaps) {
try {
// Save locally to `fontsPath/filename`
await yourDownloadFunction(url, `${fontsPath}/${filename}`)
} catch (error) {
success = false
console.error(`Failed to save font: ${filename}`, error)
}
}

return success
}
```

## License
4 changes: 2 additions & 2 deletions src/download.ts
Original file line number Diff line number Diff line change
@@ -8,12 +8,12 @@ export async function getFontInfo (url: string, options?: Partial<DownloadOption
const info = new Downloader(url, options)
const { fonts, css } = await info.extractFontInfo()

let localCSS: string = ''
let localCSS: string = css
const fontMaps: Map<string, string> = new Map()

// Replace remote with local font url() paths
for (const font of fonts) {
localCSS = css.replace(font.inputText, font.outputText)
localCSS = localCSS.replace(font.inputText, font.outputText)
fontMaps.set(font.inputFont, font.outputFont)
}