Skip to content

Commit

Permalink
feat(config): font
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Nov 25, 2023
1 parent 0d72edf commit 9517f94
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
28 changes: 26 additions & 2 deletions src/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,29 @@ export function Root({
function Head({ frontmatter }: { frontmatter: Module['frontmatter'] }) {
const config = useConfig()

const { iconUrl } = config
const { font, iconUrl } = config
const { title, description = config.description } = frontmatter || {}

const enableTitleTemplate = config.title && config.title.toLowerCase() !== title?.toLowerCase()

return (
<>
{/* Title */}
<Helmet
defaultTitle={config.title}
titleTemplate={enableTitleTemplate ? config.titleTemplate : undefined}
>
{title && <title>{title}</title>}
{description && <meta name="description" content={description} />}
</Helmet>

{/* Description */}
{description && (
<Helmet>
<meta name="description" content={description} />
</Helmet>
)}

{/* Icons */}
{iconUrl && (
<>
{typeof iconUrl === 'string' ? (
Expand All @@ -63,6 +71,22 @@ function Head({ frontmatter }: { frontmatter: Module['frontmatter'] }) {
)}
</>
)}

{/* Fonts */}
{font && (
<>
{font.google && (
<Helmet>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossOrigin="" />
<link
href={`https://fonts.googleapis.com/css2?family=${font.google}:wght@300;400;500&display=swap`}
rel="stylesheet"
/>
</Helmet>
)}
</>
)}
</>
)
}
Expand Down
15 changes: 14 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ import type { ReactElement } from 'react'

type RequiredBy<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>

type RequiredProperties = 'root' | 'title' | 'titleTemplate'
type RequiredProperties = 'font' | 'root' | 'title' | 'titleTemplate'

export type Config<parsed extends boolean = false> = RequiredBy<
{
/**
* General description for the documentation.
*/
description?: string
/**
* Base font face.
*
* @default { google: "Inter" }
*/
font?: Font
/**
* Additional tags to include in the `<head>` tag of the page HTML.
*/
Expand Down Expand Up @@ -55,13 +61,15 @@ export type Config<parsed extends boolean = false> = RequiredBy<
export type ParsedConfig = Config<true>

export function defineConfig({
font = { google: 'Inter' },
head,
root = 'docs',
title = 'Docs',
titleTemplate = `%s – ${title}`,
...config
}: Config): ParsedConfig {
return {
font,
head,
root,
title,
Expand Down Expand Up @@ -95,6 +103,11 @@ function parseSocials(socials: Socials): ParsedSocials {
//////////////////////////////////////////////////////
// Types

export type Font = {
/** Name of the Google Font to use. */
google?: string
}

export type IconUrl = string | { light: string; dark: string }

export type LogoUrl = string | { light: string; dark: string }
Expand Down
3 changes: 0 additions & 3 deletions src/vite/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500&display=swap" rel="stylesheet">
<script src="../app/utils/initializeTheme.ts"></script>
<!--head-->
</head>
Expand Down

0 comments on commit 9517f94

Please sign in to comment.