@@ -33,6 +33,37 @@ import '@gitbook/icons/style.css';
33
33
import './globals.css' ;
34
34
import { GITBOOK_ICONS_TOKEN , GITBOOK_ICONS_URL } from '@v2/lib/env' ;
35
35
36
+ const CUSTTOM_FONTS_WORKER_URL = 'https://public-valentino.gitbook.space/' ;
37
+
38
+ interface FontFace {
39
+ weight : number ;
40
+ url : string ;
41
+ }
42
+
43
+ interface CustomFont {
44
+ fontFamily : string ;
45
+ faces : FontFace [ ] ;
46
+ }
47
+
48
+ const regularFontWorkerUrl =
49
+ 'https://public-valentino.gitbook.space/valentino/fonts/3ey8MCM8uEpKWRFnaFp2/nvidia_regular.woff2' ;
50
+ const boldFontWorkerUrl =
51
+ 'https://public-valentino.gitbook.space/valentino/fonts/3ey8MCM8uEpKWRFnaFp2/nvidia_bold.woff2' ;
52
+
53
+ // // This would come from props/API
54
+ // const nvidiaFont: CustomFont = {
55
+ // id: 'custom-font',
56
+ // name: 'CustomFont',
57
+ // links: {
58
+ // regular: regularFontWorkerUrl,
59
+ // bold: boldFontWorkerUrl,
60
+ // },
61
+ // display: 'swap',
62
+ // fallback: ['system-ui', 'arial'],
63
+ // };
64
+ //
65
+ // const customFont = nvidiaFont;
66
+
36
67
/**
37
68
* Layout shared between the content and the PDF renderer.
38
69
* It takes care of setting the theme and the language.
@@ -49,6 +80,18 @@ export async function CustomizationRootLayout(props: {
49
80
const sidebarStyles = getSidebarStyles ( customization ) ;
50
81
const { infoColor, successColor, warningColor, dangerColor } = getSemanticColors ( customization ) ;
51
82
83
+ const hasCustomFont = typeof customization . styling . font !== 'string' ;
84
+ // Add custom font handling
85
+ const customFontCSS = hasCustomFont
86
+ ? generateCustomFontFaces ( customization . styling . font as CustomFont )
87
+ : '' ;
88
+
89
+ const customFontLinks = hasCustomFont
90
+ ? customization . styling . font . faces . map ( ( face ) => face . url )
91
+ : [ ] ;
92
+
93
+ console . log ( 'customFontLinks' , customFontLinks , customization . styling . font ) ;
94
+
52
95
return (
53
96
< html
54
97
suppressHydrationWarning
@@ -74,6 +117,12 @@ export async function CustomizationRootLayout(props: {
74
117
{ customization . privacyPolicy . url ? (
75
118
< link rel = "privacy-policy" href = { customization . privacyPolicy . url } />
76
119
) : null }
120
+ { hasCustomFont && customFontLinks . length > 0
121
+ ? customFontLinks . map ( ( link ) => (
122
+ < link rel = "preload" href = { link } as = "font" crossOrigin = "anonymous" />
123
+ ) )
124
+ : null }
125
+ { hasCustomFont ? < style > { customFontCSS } </ style > : null }
77
126
< style
78
127
nonce = {
79
128
//Since I can't get the nonce to work for inline styles, we need to allow unsafe-inline
@@ -119,6 +168,9 @@ export async function CustomizationRootLayout(props: {
119
168
</ head >
120
169
< body
121
170
className = { tcls (
171
+ fontNotoColorEmoji . className ,
172
+ hasCustomFont ? 'font-sans' : fonts [ customization . styling . font ] . className ,
173
+ `${ ibmPlexMono . variable } ` ,
122
174
'bg-tint-base' ,
123
175
'theme-muted:bg-tint-subtle' ,
124
176
'theme-bold-tint:bg-tint-subtle' ,
@@ -254,6 +306,58 @@ function getSemanticColors(
254
306
} ;
255
307
}
256
308
309
+ /**
310
+ * Define the custom font faces and set the --font-content to the custom font name
311
+ */
312
+ function generateCustomFontFaces ( customFont : CustomFont ) : string {
313
+ const { fontFamily, faces } = customFont ;
314
+
315
+ const regularFont = faces . find ( ( face ) => face . weight === 400 ) ;
316
+ const boldFont = faces . find ( ( face ) => face . weight === 700 ) ;
317
+
318
+ if ( ! regularFont || ! boldFont ) {
319
+ throw new Error ( 'Custom font must have a regular and a bold face' ) ;
320
+ }
321
+
322
+ const regular = `
323
+ @font-face {
324
+ font-family: ${ fontFamily } ;
325
+ font-style: normal;
326
+ font-weight: ${ regularFont . weight } ;
327
+ font-display: swap;
328
+ src: url(${ regularFont . url } );
329
+ }
330
+ ` ;
331
+
332
+ // const semiBold = `
333
+ // @font -face {
334
+ // font-family: ${fontFamily};
335
+ // font-style: normal;
336
+ // font-weight: 600;
337
+ // font-display: swap;
338
+ // src: url(${boldFont.url});
339
+ // }
340
+ // `
341
+ // : "";
342
+
343
+ const bold = `
344
+ @font-face {
345
+ font-family: ${ fontFamily } ;
346
+ font-style: normal;
347
+ font-weight: ${ boldFont . weight } ;
348
+ font-display: swap;
349
+ src: url(${ boldFont . url } );
350
+ }
351
+ ` ;
352
+
353
+ return `
354
+ ${ regular }
355
+ ${ bold }
356
+ :root {
357
+ --font-content: ${ fontFamily } ;
358
+ }
359
+ ` ;
360
+ }
257
361
type ColorInput = string ;
258
362
function generateColorVariable (
259
363
name : string ,
0 commit comments