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
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,50 @@ downloader.hook('download:complete', () => {
await downloader.execute()
```

### `getFontInfo(url: string, options?: DownloadOptions): [ Map<string, string>, 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
// fontsPath is the path prepended to the local font name in the @font-face url()
const fontsPath = './fonts'
const cssPath = './css'

const [fontsMap, localCSS] = await getFontInfo('https://fonts.googleapis.com/css2?family=Roboto', {
base64: false,
fontsPath: fontsPath
})

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 result = async function () {
let success: boolean = true

for (const [url, filename] of fontsMap) {
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

[MIT License](./LICENSE)
Expand Down
16 changes: 16 additions & 0 deletions src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,19 @@ import { Downloader, DownloadOptions } from './downloader'
export function download (url: string, options?: Partial<DownloadOptions>) {
return new Downloader(url, options)
}

export async function getFontInfo (url: string, options?: Partial<DownloadOptions>): Promise<[Map<string, string>, string]> {
const info = new Downloader(url, options)
const { fonts, css } = await info.extractFontInfo()

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

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

return [fontsMap, localCSS]
}
15 changes: 14 additions & 1 deletion src/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,23 @@ export class Downloader extends Hookable<DownloaderHooks> {
return true
}

async extractFontInfo (): Promise<{ fonts: FontInputOutput[], css: string }> {
if (!isValidURL(this.url)) {
throw new Error('Invalid Google Fonts URL')
}

const { headers, fontsPath } = this.config

const _css = await ofetch(this.url, { headers })
const { fonts, css } = parseFontsFromCss(_css, fontsPath)

return { fonts, css }
}

private async downloadFonts (fonts: FontInputOutput[]) {
const { headers, base64, outputDir, fontsDir } = this.config
const downloadedFonts: FontInputOutput[] = []
const _fonts:FontInputOutput[] = []
const _fonts: FontInputOutput[] = []

for (const font of fonts) {
const downloadedFont = downloadedFonts.find(f => f.inputFont === font.inputFont)
Expand Down