-
Notifications
You must be signed in to change notification settings - Fork 4k
Improve default site icon #3679
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
Merged
+249
−117
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"gitbook": patch | ||
--- | ||
|
||
Improve default site icon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { CustomizationDefaultFont } from '@gitbook/api'; | ||
import { type FontWeight, getDefaultFont } from '@gitbook/fonts'; | ||
|
||
import { getFontSourcesToPreload } from '@/fonts/custom'; | ||
import type { GitBookSiteContext } from '@/lib/context'; | ||
import { filterOutNullable } from '@/lib/typescript'; | ||
|
||
type ComputeFontsInput = { | ||
regularText: string; | ||
boldText: string; | ||
}; | ||
|
||
export async function computeImageFonts( | ||
customization: GitBookSiteContext['customization'], | ||
input: ComputeFontsInput | ||
) { | ||
// Google fonts | ||
if (typeof customization.styling.font === 'string') { | ||
const fontFamily = customization.styling.font ?? CustomizationDefaultFont.Inter; | ||
|
||
const fonts = ( | ||
await Promise.all([ | ||
loadGoogleFont({ font: fontFamily, text: input.regularText, weight: 400 }), | ||
loadGoogleFont({ font: fontFamily, text: input.boldText, weight: 700 }), | ||
]) | ||
).filter(filterOutNullable); | ||
|
||
return { fontFamily, fonts } as const; | ||
} | ||
|
||
// Custom fonts | ||
// We only load the primary font weights for now | ||
const primaryFontWeights = getFontSourcesToPreload(customization.styling.font); | ||
|
||
const fonts = ( | ||
await Promise.all( | ||
primaryFontWeights.map((face) => { | ||
const { weight, sources } = face; | ||
const source = sources[0]; | ||
|
||
// Satori doesn't support WOFF2, so we skip it | ||
// https://github.com/vercel/satori?tab=readme-ov-file#fonts | ||
if (!source || source.format === 'woff2' || source.url.endsWith('.woff2')) { | ||
return null; | ||
} | ||
|
||
return loadCustomFont({ url: source.url, weight: weight as 400 | 700 }); | ||
}) | ||
) | ||
).filter(filterOutNullable); | ||
|
||
return { fontFamily: 'CustomFont', fonts } as const; | ||
} | ||
|
||
async function loadGoogleFont(input: { | ||
font: CustomizationDefaultFont; | ||
text: string; | ||
weight: FontWeight; | ||
}) { | ||
const lookup = getDefaultFont({ | ||
font: input.font, | ||
text: input.text, | ||
weight: input.weight, | ||
}); | ||
|
||
// If we found a font file, load it | ||
if (lookup) { | ||
return getWithCache(`google-font-files:${lookup.url}`, async () => { | ||
const response = await fetch(lookup.url); | ||
if (response.ok) { | ||
const data = await response.arrayBuffer(); | ||
return { | ||
name: lookup.font, | ||
data, | ||
style: 'normal' as const, | ||
weight: input.weight, | ||
}; | ||
} | ||
}); | ||
} | ||
|
||
// If for some reason we can't load the font, we'll just use the default one | ||
return null; | ||
} | ||
|
||
async function loadCustomFont(input: { url: string; weight: 400 | 700 }) { | ||
const { url, weight } = input; | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
return null; | ||
} | ||
|
||
const data = await response.arrayBuffer(); | ||
|
||
return { | ||
name: 'CustomFont', | ||
data, | ||
style: 'normal' as const, | ||
weight, | ||
} as const; | ||
} | ||
|
||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
const staticCache = new Map<string, any>(); | ||
|
||
async function getWithCache<T>(key: string, fn: () => Promise<T>) { | ||
const cached = staticCache.get(key) as Promise<T>; | ||
if (cached) { | ||
return cached; | ||
} | ||
|
||
const promise = fn(); | ||
staticCache.set(key, promise); | ||
|
||
try { | ||
const result = await promise; | ||
return result; | ||
} catch (error) { | ||
// Remove the failed promise from cache so it can be retried | ||
staticCache.delete(key); | ||
throw error; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.