Skip to content

Commit 7cbe8d4

Browse files
committed
wip
1 parent 99da8df commit 7cbe8d4

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

packages/gitbook/src/components/RootLayout/CustomizationRootLayout.tsx

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,37 @@ import '@gitbook/icons/style.css';
3333
import './globals.css';
3434
import { GITBOOK_ICONS_TOKEN, GITBOOK_ICONS_URL } from '@v2/lib/env';
3535

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+
3667
/**
3768
* Layout shared between the content and the PDF renderer.
3869
* It takes care of setting the theme and the language.
@@ -49,6 +80,18 @@ export async function CustomizationRootLayout(props: {
4980
const sidebarStyles = getSidebarStyles(customization);
5081
const { infoColor, successColor, warningColor, dangerColor } = getSemanticColors(customization);
5182

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+
5295
return (
5396
<html
5497
suppressHydrationWarning
@@ -74,6 +117,12 @@ export async function CustomizationRootLayout(props: {
74117
{customization.privacyPolicy.url ? (
75118
<link rel="privacy-policy" href={customization.privacyPolicy.url} />
76119
) : 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}
77126
<style
78127
nonce={
79128
//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: {
119168
</head>
120169
<body
121170
className={tcls(
171+
fontNotoColorEmoji.className,
172+
hasCustomFont ? 'font-sans' : fonts[customization.styling.font].className,
173+
`${ibmPlexMono.variable}`,
122174
'bg-tint-base',
123175
'theme-muted:bg-tint-subtle',
124176
'theme-bold-tint:bg-tint-subtle',
@@ -254,6 +306,58 @@ function getSemanticColors(
254306
};
255307
}
256308

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+
}
257361
type ColorInput = string;
258362
function generateColorVariable(
259363
name: string,

packages/gitbook/tailwind.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const config: Config = {
7272
'current-page': 'current="page"',
7373
},
7474
fontFamily: {
75-
sans: ['var(--font-content)'],
75+
sans: ['var(--font-content)', 'arial', 'sans-serif'],
7676
mono: ['var(--font-mono)'],
7777
emoji: [
7878
'Apple Color Emoji',

0 commit comments

Comments
 (0)