|
| 1 | +export type BodyFont = 'Fira Mono' | 'Fira Sans' | 'IBM Plex Serif' | 'Inter' | 'JetBrains Mono' | 'Lora' | 'Manrope' | 'Merriweather' | 'Nunito' | 'Noto Sans' | 'Noto Serif' | 'Poppins' | 'Roboto' | 'Space Mono'; |
| 2 | +export type HeadingFont = 'Cardo' | 'Chakra Petch' | 'Old Standard TT' | 'Prata' | 'Rufina' | 'Space Grotesk' | 'Tenor Sans' | BodyFont; |
| 3 | +export type CustomFonts = {heading: HeadingFont[], body: BodyFont[]}; |
| 4 | + |
| 5 | +export type FontSelection = { |
| 6 | + heading?: HeadingFont, |
| 7 | + body?: BodyFont |
| 8 | +}; |
| 9 | + |
| 10 | +export const CUSTOM_FONTS: CustomFonts = { |
| 11 | + heading: [ |
| 12 | + 'Cardo', |
| 13 | + 'Chakra Petch', |
| 14 | + 'Fira Mono', |
| 15 | + 'Fira Sans', |
| 16 | + 'IBM Plex Serif', |
| 17 | + 'Inter', |
| 18 | + 'JetBrains Mono', |
| 19 | + 'Lora', |
| 20 | + 'Manrope', |
| 21 | + 'Merriweather', |
| 22 | + 'Noto Sans', |
| 23 | + 'Noto Serif', |
| 24 | + 'Nunito', |
| 25 | + 'Old Standard TT', |
| 26 | + 'Poppins', |
| 27 | + 'Prata', |
| 28 | + 'Roboto', |
| 29 | + 'Rufina', |
| 30 | + 'Space Grotesk', |
| 31 | + 'Space Mono', |
| 32 | + 'Tenor Sans' |
| 33 | + ], |
| 34 | + body: [ |
| 35 | + 'Fira Mono', |
| 36 | + 'Fira Sans', |
| 37 | + 'IBM Plex Serif', |
| 38 | + 'Inter', |
| 39 | + 'JetBrains Mono', |
| 40 | + 'Lora', |
| 41 | + 'Manrope', |
| 42 | + 'Merriweather', |
| 43 | + 'Noto Sans', |
| 44 | + 'Noto Serif', |
| 45 | + 'Nunito', |
| 46 | + 'Poppins', |
| 47 | + 'Roboto', |
| 48 | + 'Space Mono' |
| 49 | + ] |
| 50 | +}; |
| 51 | + |
| 52 | +const classFontNames = { |
| 53 | + Cardo: 'cardo', |
| 54 | + Manrope: 'manrope', |
| 55 | + Merriweather: 'merriweather', |
| 56 | + Nunito: 'nunito', |
| 57 | + 'Old Standard TT': 'old-standard-tt', |
| 58 | + Prata: 'prata', |
| 59 | + Roboto: 'roboto', |
| 60 | + Rufina: 'rufina', |
| 61 | + 'Tenor Sans': 'tenor-sans', |
| 62 | + 'Space Grotesk': 'space-grotesk', |
| 63 | + 'Chakra Petch': 'chakra-petch', |
| 64 | + 'Noto Sans': 'noto-sans', |
| 65 | + Poppins: 'poppins', |
| 66 | + 'Fira Sans': 'fira-sans', |
| 67 | + Inter: 'inter', |
| 68 | + 'Noto Serif': 'noto-serif', |
| 69 | + Lora: 'lora', |
| 70 | + 'IBM Plex Serif': 'ibm-plex-serif', |
| 71 | + 'Space Mono': 'space-mono', |
| 72 | + 'Fira Mono': 'fira-mono', |
| 73 | + 'JetBrains Mono': 'jetbrains-mono' |
| 74 | +}; |
| 75 | + |
| 76 | +export function generateCustomFontCss(fonts: FontSelection) { |
| 77 | + let fontImports: string = ''; |
| 78 | + let fontCSS: string = ''; |
| 79 | + |
| 80 | + const importStrings = { |
| 81 | + Cardo: { |
| 82 | + url: '@import url(https://fonts.bunny.net/css?family=cardo:400,700)' |
| 83 | + }, |
| 84 | + Manrope: { |
| 85 | + url: '@import url(https://fonts.bunny.net/css?family=manrope:300,500,700)' |
| 86 | + }, |
| 87 | + Merriweather: { |
| 88 | + url: '@import url(https://fonts.bunny.net/css?family=merriweather:300,700)' |
| 89 | + }, |
| 90 | + Nunito: { |
| 91 | + url: '@import url(https://fonts.bunny.net/css?family=nunito:400,600,700)' |
| 92 | + }, |
| 93 | + 'Old Standard TT': { |
| 94 | + url: '@import url(https://fonts.bunny.net/css?family=old-standard-tt:400,700)' |
| 95 | + }, |
| 96 | + Prata: { |
| 97 | + url: '@import url(https://fonts.bunny.net/css?family=prata:400)' |
| 98 | + }, |
| 99 | + Roboto: { |
| 100 | + url: '@import url(https://fonts.bunny.net/css?family=roboto:400,500,700)' |
| 101 | + }, |
| 102 | + Rufina: { |
| 103 | + url: '@import url(https://fonts.bunny.net/css?family=rufina:400,500,700)' |
| 104 | + }, |
| 105 | + 'Tenor Sans': { |
| 106 | + url: '@import url(https://fonts.bunny.net/css?family=tenor-sans:400)' |
| 107 | + }, |
| 108 | + 'Space Grotesk': { |
| 109 | + url: '@import url(https://fonts.bunny.net/css?family=space-grotesk:700)' |
| 110 | + }, |
| 111 | + 'Chakra Petch': { |
| 112 | + url: '@import url(https://fonts.bunny.net/css?family=chakra-petch:400)' |
| 113 | + }, |
| 114 | + 'Noto Sans': { |
| 115 | + url: '@import url(https://fonts.bunny.net/css?family=noto-sans:400,700)' |
| 116 | + }, |
| 117 | + Poppins: { |
| 118 | + url: '@import url(https://fonts.bunny.net/css?family=poppins:400,500,600)' |
| 119 | + }, |
| 120 | + 'Fira Sans': { |
| 121 | + url: '@import url(https://fonts.bunny.net/css?family=fira-sans:400,500,600)' |
| 122 | + }, |
| 123 | + Inter: { |
| 124 | + url: '@import url(https://fonts.bunny.net/css?family=inter:400,500,600)' |
| 125 | + }, |
| 126 | + 'Noto Serif': { |
| 127 | + url: '@import url(https://fonts.bunny.net/css?family=noto-serif:400,700)' |
| 128 | + }, |
| 129 | + Lora: { |
| 130 | + url: '@import url(https://fonts.bunny.net/css?family=lora:400,700)' |
| 131 | + }, |
| 132 | + 'IBM Plex Serif': { |
| 133 | + url: '@import url(https://fonts.bunny.net/css?family=ibm-plex-serif:400,500,600)' |
| 134 | + }, |
| 135 | + 'Space Mono': { |
| 136 | + url: '@import url(https://fonts.bunny.net/css?family=space-mono:400,700)' |
| 137 | + }, |
| 138 | + 'Fira Mono': { |
| 139 | + url: '@import url(https://fonts.bunny.net/css?family=fira-mono:400,700)' |
| 140 | + }, |
| 141 | + 'JetBrains Mono': { |
| 142 | + url: '@import url(https://fonts.bunny.net/css?family=jetbrains-mono:400,700)' |
| 143 | + } |
| 144 | + }; |
| 145 | + |
| 146 | + if (fonts?.heading && fonts?.body && fonts?.heading === fonts?.body) { |
| 147 | + fontImports = `${importStrings[fonts?.heading]?.url};`; |
| 148 | + } else { |
| 149 | + fontImports = ''; |
| 150 | + |
| 151 | + if (fonts?.heading) { |
| 152 | + fontImports += `${importStrings[fonts?.heading]?.url};`; |
| 153 | + } |
| 154 | + |
| 155 | + if (fonts?.body) { |
| 156 | + fontImports += `${importStrings[fonts?.body]?.url};`; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + if (fonts?.body || fonts?.heading) { |
| 161 | + fontCSS = ':root {'; |
| 162 | + |
| 163 | + if (fonts?.heading) { |
| 164 | + fontCSS += `--gh-font-heading: ${fonts.heading};`; |
| 165 | + } |
| 166 | + |
| 167 | + if (fonts?.body) { |
| 168 | + fontCSS += `--gh-font-body: ${fonts.body};`; |
| 169 | + } |
| 170 | + |
| 171 | + fontCSS += '}'; |
| 172 | + } |
| 173 | + |
| 174 | + return `<style>${fontImports}${fontCSS}</style>`; |
| 175 | +} |
| 176 | + |
| 177 | +export function generateCustomFontBodyClass(fonts: FontSelection) { |
| 178 | + let bodyClass = ''; |
| 179 | + |
| 180 | + if (fonts?.heading) { |
| 181 | + bodyClass += getCustomFontClassName({font: fonts.heading, heading: true}); |
| 182 | + if (fonts?.body) { |
| 183 | + bodyClass += ' '; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + if (fonts?.body) { |
| 188 | + bodyClass += getCustomFontClassName({font: fonts.body, heading: false}); |
| 189 | + } |
| 190 | + |
| 191 | + return bodyClass; |
| 192 | +} |
| 193 | + |
| 194 | +export function getCSSFriendlyFontClassName(font: string) { |
| 195 | + return classFontNames[font as keyof typeof classFontNames] || ''; |
| 196 | +} |
| 197 | + |
| 198 | +export function getCustomFontClassName({font, heading}: {font: string, heading: boolean}) { |
| 199 | + const cssFriendlyFontClassName = getCSSFriendlyFontClassName(font); |
| 200 | + |
| 201 | + if (!cssFriendlyFontClassName) { |
| 202 | + return ''; |
| 203 | + } |
| 204 | + |
| 205 | + return `gh-font-${heading ? 'heading' : 'body'}-${cssFriendlyFontClassName}`; |
| 206 | +} |
| 207 | + |
| 208 | +export function getCustomFonts(): CustomFonts { |
| 209 | + return CUSTOM_FONTS; |
| 210 | +} |
| 211 | + |
| 212 | +export function isValidCustomFont(font: string): font is BodyFont { |
| 213 | + return CUSTOM_FONTS.body.includes(font as BodyFont); |
| 214 | +} |
| 215 | + |
| 216 | +export function isValidCustomHeadingFont(font: string): font is HeadingFont { |
| 217 | + return CUSTOM_FONTS.heading.includes(font as HeadingFont); |
| 218 | +} |
0 commit comments