@@ -3,13 +3,17 @@ import {
33 type GoogleFamilyOptions ,
44 type GoogleiconsFamilyOptions ,
55 type InitializedProvider ,
6+ type NpmProviderOptions ,
7+ type NpmFamilyOptions ,
68 providers ,
79} from 'unifont' ;
810import { FontaceFontFileReader } from '../infra/fontace-font-file-reader.js' ;
911import type { FontProvider } from '../types.js' ;
1012import { type LocalFamilyOptions , LocalFontProvider } from './local.js' ;
13+ import { fileURLToPath } from 'node:url' ;
14+ import { readFile } from 'node:fs/promises' ;
1115
12- /** [Adobe](https://fonts.adobe.com/ ) */
16+ /** [Adobe](https://v6.docs.astro.build/en/reference/font-provider-reference/#adobe ) */
1317function adobe ( config : AdobeProviderOptions ) : FontProvider {
1418 const provider = providers . adobe ( config ) ;
1519 let initializedProvider : InitializedProvider | undefined ;
@@ -28,7 +32,7 @@ function adobe(config: AdobeProviderOptions): FontProvider {
2832 } ;
2933}
3034
31- /** [Bunny](https://fonts.bunny.net/ ) */
35+ /** [Bunny](https://v6.docs.astro.build/en/reference/font-provider-reference/#bunny ) */
3236function bunny ( ) : FontProvider {
3337 const provider = providers . bunny ( ) ;
3438 let initializedProvider : InitializedProvider | undefined ;
@@ -46,7 +50,7 @@ function bunny(): FontProvider {
4650 } ;
4751}
4852
49- /** [Fontshare](https://www.fontshare.com/ ) */
53+ /** [Fontshare](https://v6.docs.astro.build/en/reference/font-provider-reference/#fontshare ) */
5054function fontshare ( ) : FontProvider {
5155 const provider = providers . fontshare ( ) ;
5256 let initializedProvider : InitializedProvider | undefined ;
@@ -64,7 +68,7 @@ function fontshare(): FontProvider {
6468 } ;
6569}
6670
67- /** [Fontsource](https://fontsource.org/ ) */
71+ /** [Fontsource](https://v6.docs.astro.build/en/reference/font-provider-reference/#fontsource ) */
6872function fontsource ( ) : FontProvider {
6973 const provider = providers . fontsource ( ) ;
7074 let initializedProvider : InitializedProvider | undefined ;
@@ -82,7 +86,7 @@ function fontsource(): FontProvider {
8286 } ;
8387}
8488
85- /** [Google](https://fonts.google.com/ ) */
89+ /** [Google](https://v6.docs.astro.build/en/reference/font-provider-reference/#google ) */
8690function google ( ) : FontProvider < GoogleFamilyOptions | undefined > {
8791 const provider = providers . google ( ) ;
8892 let initializedProvider : InitializedProvider < GoogleFamilyOptions > | undefined ;
@@ -100,7 +104,7 @@ function google(): FontProvider<GoogleFamilyOptions | undefined> {
100104 } ;
101105}
102106
103- /** [Google Icons](https://fonts.google.com/ icons) */
107+ /** [Google Icons](https://v6.docs.astro.build/en/reference/font-provider-reference/#google- icons) */
104108function googleicons ( ) : FontProvider < GoogleiconsFamilyOptions | undefined > {
105109 const provider = providers . googleicons ( ) ;
106110 let initializedProvider : InitializedProvider < GoogleiconsFamilyOptions > | undefined ;
@@ -118,22 +122,46 @@ function googleicons(): FontProvider<GoogleiconsFamilyOptions | undefined> {
118122 } ;
119123}
120124
121- /** A provider that handles local files. */
125+ /** [Local](https://v6.docs.astro.build/en/reference/font- provider-reference/# local) */
122126function local ( ) : FontProvider < LocalFamilyOptions > {
123127 return new LocalFontProvider ( {
124128 fontFileReader : new FontaceFontFileReader ( ) ,
125129 } ) ;
126130}
127131
132+ /** [NPM](https://v6.docs.astro.build/en/reference/font-provider-reference/#npm) */
133+ function npm (
134+ options ?: Omit < NpmProviderOptions , 'root' | 'readFile' > ,
135+ ) : FontProvider < NpmFamilyOptions | undefined > {
136+ let initializedProvider : InitializedProvider < NpmFamilyOptions > | undefined ;
137+ return {
138+ name : providers . npm ( ) . _name ,
139+ async init ( context ) {
140+ initializedProvider = await providers . npm ( {
141+ ...options ,
142+ root : fileURLToPath ( context . root ) ,
143+ readFile : ( path ) => readFile ( path , 'utf-8' ) . catch ( ( ) => null ) ,
144+ } ) ( context ) ;
145+ } ,
146+ async resolveFont ( { familyName, ...rest } ) {
147+ return await initializedProvider ?. resolveFont ( familyName , rest ) ;
148+ } ,
149+ async listFonts ( ) {
150+ return await initializedProvider ?. listFonts ?.( ) ;
151+ } ,
152+ } ;
153+ }
154+
128155/**
129156 * Astro exports a few built-in providers:
130- * - [Adobe](https://fonts.adobe.com/)
131- * - [Bunny](https://fonts.bunny.net/)
132- * - [Fontshare](https://www.fontshare.com/)
133- * - [Fontsource](https://fontsource.org/)
134- * - [Google](https://fonts.google.com/)
135- * - [Google Icons](https://fonts.google.com/icons)
136- * - Local
157+ * - [Adobe](https://v6.docs.astro.build/en/reference/font-provider-reference/#adobe)
158+ * - [Bunny](https://v6.docs.astro.build/en/reference/font-provider-reference/#bunny)
159+ * - [Fontshare](https://v6.docs.astro.build/en/reference/font-provider-reference/#fontshare)
160+ * - [Fontsource](https://v6.docs.astro.build/en/reference/font-provider-reference/#fontsource)
161+ * - [Google](https://v6.docs.astro.build/en/reference/font-provider-reference/#google)
162+ * - [Google Icons](https://v6.docs.astro.build/en/reference/font-provider-reference/#google-icons)
163+ * - [Local](https://v6.docs.astro.build/en/reference/font-provider-reference/#local)
164+ * - [NPM](TODO:)
137165 */
138166export const fontProviders = {
139167 adobe,
@@ -143,4 +171,5 @@ export const fontProviders = {
143171 google,
144172 googleicons,
145173 local,
174+ npm,
146175} ;
0 commit comments