Skip to content

Commit fda9cd8

Browse files
cnguyen-dekermanx
andauthored
feat: support coollabs as font provider (#1982)
Co-authored-by: _Kerman <[email protected]>
1 parent dbf42f8 commit fda9cd8

File tree

8 files changed

+49
-13
lines changed

8 files changed

+49
-13
lines changed

docs/custom/config-fonts.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fonts:
1818

1919
And that's all.
2020

21-
Fonts will be **imported automatically from [Google Fonts](https://fonts.google.com/)**. That means you can use any fonts available on Google Fonts directly.
21+
Fonts will be **imported automatically from a provider via CDN, by default it is [Google Fonts](https://fonts.google.com/)**. That means you can use any fonts available on Google Fonts directly.
2222

2323
## Local Fonts
2424

@@ -92,10 +92,10 @@ fonts:
9292

9393
## Providers
9494

95-
- Options: `google` | `none`
95+
- Options: `google` | `coollabs` | `none`
9696
- Default: `google`
9797

98-
Currently, only Google Fonts is supported, we are planning to add more providers in the future. Specify to `none` will disable the auto-importing feature entirely and treat all the fonts locally.
98+
Currently, only [Google Fonts](https://fonts.google.com/) and [coolLabs](https://fonts.coollabs.io/) supported, we are planning to add more providers in the future. Specify to `none` will disable the auto-importing feature entirely and treat all the fonts locally.
9999

100100
```yaml
101101
---

packages/slidev/node/setups/indexHtml.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { escapeHtml } from 'markdown-it/lib/common/utils.mjs'
77
import { version } from '../../package.json'
88
import { getSlideTitle } from '../commands/shared'
99
import { toAtFS } from '../resolver'
10-
import { generateGoogleFontsUrl } from '../utils'
10+
import { generateCoollabsFontsUrl, generateGoogleFontsUrl } from '../utils'
1111

1212
function toAttrValue(unsafe: unknown) {
1313
return JSON.stringify(escapeHtml(String(unsafe)))
@@ -49,8 +49,13 @@ export default function setupIndexHtml({ mode, entry, clientRoot, userRoot, root
4949
if (data.features.tweet)
5050
body += '\n<script async src="https://platform.twitter.com/widgets.js"></script>'
5151

52-
if (data.config.fonts.webfonts.length && data.config.fonts.provider !== 'none')
53-
head += `\n<link rel="stylesheet" href="${generateGoogleFontsUrl(data.config.fonts)}" type="text/css">`
52+
if (data.config.fonts.webfonts.length) {
53+
const { provider } = data.config.fonts
54+
if (provider === 'google')
55+
head += `\n<link rel="stylesheet" href="${generateGoogleFontsUrl(data.config.fonts)}" type="text/css">`
56+
else if (provider === 'coollabs')
57+
head += `\n<link rel="stylesheet" href="${generateCoollabsFontsUrl(data.config.fonts)}" type="text/css">`
58+
}
5459

5560
if (data.headmatter.lang)
5661
main = main.replace('<html lang="en">', `<html lang="${data.headmatter.lang}">`)

packages/slidev/node/utils.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,23 @@ export function stringifyMarkdownTokens(tokens: Token[]) {
2121
.join(' ')
2222
}
2323

24-
export function generateGoogleFontsUrl(options: ResolvedFontOptions) {
24+
export function generateFontParams(options: ResolvedFontOptions) {
2525
const weights = options.weights
2626
.flatMap(i => options.italic ? [`0,${i}`, `1,${i}`] : [`${i}`])
2727
.sort()
2828
.join(';')
29-
const fonts = options.webfonts
29+
const fontParams = options.webfonts
3030
.map(i => `family=${i.replace(/^(['"])(.*)\1$/, '$1').replace(/\s+/g, '+')}:${options.italic ? 'ital,' : ''}wght@${weights}`)
3131
.join('&')
32+
return fontParams
33+
}
34+
35+
export function generateGoogleFontsUrl(options: ResolvedFontOptions) {
36+
return `https://fonts.googleapis.com/css2?${generateFontParams(options)}&display=swap`
37+
}
3238

33-
return `https://fonts.googleapis.com/css2?${fonts}&display=swap`
39+
export function generateCoollabsFontsUrl(options: ResolvedFontOptions) {
40+
return `https://api.fonts.coollabs.io/fonts?${generateFontParams(options)}&display=swap`
3441
}
3542

3643
/**

packages/types/src/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface ResolvedFontOptions {
1818
serif: string[]
1919
weights: string[]
2020
italic: boolean
21-
provider: 'none' | 'google'
21+
provider: 'none' | 'google' | 'coollabs'
2222
webfonts: string[]
2323
local: string[]
2424
}

packages/types/src/frontmatter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ export interface FontOptions {
391391
/**
392392
* @default 'google'
393393
*/
394-
provider?: 'none' | 'google'
394+
provider?: 'none' | 'google' | 'coollabs'
395395
/**
396396
* Specify web fonts names, will detect from `sans`, `mono`, `serif` if not provided
397397
*/

packages/vscode/schema/headmatter.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,8 @@
706706
"type": "string",
707707
"enum": [
708708
"none",
709-
"google"
709+
"google",
710+
"coollabs"
710711
],
711712
"default": "google"
712713
},

test/__snapshots__/utils.test.ts.snap

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`utils > coollabs-fonts 1`] = `"https://api.fonts.coollabs.io/fonts?family=Fira+Code:wght@200;400;600&family=PT+Serif:wght@200;400;600&display=swap"`;
4+
5+
exports[`utils > coollabs-fonts 2`] = `"https://api.fonts.coollabs.io/fonts?family=Fira+Code:ital,wght@0,200;0,400;0,600;1,200;1,400;1,600&family=PT+Serif:ital,wght@0,200;0,400;0,600;1,200;1,400;1,600&display=swap"`;
6+
37
exports[`utils > google-fonts 1`] = `"https://fonts.googleapis.com/css2?family=Fira+Code:wght@200;400;600&family=PT+Serif:wght@200;400;600&display=swap"`;
48

59
exports[`utils > google-fonts 2`] = `"https://fonts.googleapis.com/css2?family=Fira+Code:ital,wght@0,200;0,400;0,600;1,200;1,400;1,600&family=PT+Serif:ital,wght@0,200;0,400;0,600;1,200;1,400;1,600&display=swap"`;

test/utils.test.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { describe, expect, it } from 'vitest'
66
import YAML from 'yaml'
77
import { parseAspectRatio, parseRangeString } from '../packages/parser/src'
88
import { getRoots } from '../packages/slidev/node/resolver'
9-
import { generateGoogleFontsUrl, stringifyMarkdownTokens, updateFrontmatterPatch } from '../packages/slidev/node/utils'
9+
import { generateCoollabsFontsUrl, generateGoogleFontsUrl, stringifyMarkdownTokens, updateFrontmatterPatch } from '../packages/slidev/node/utils'
1010

1111
describe('utils', () => {
1212
it('page-range', () => {
@@ -61,6 +61,25 @@ describe('utils', () => {
6161
).toMatchSnapshot()
6262
})
6363

64+
it('coollabs-fonts', () => {
65+
expect(
66+
generateCoollabsFontsUrl({
67+
webfonts: ['Fira Code', 'PT Serif'],
68+
weights: ['200', '400', '600'],
69+
provider: 'google',
70+
} as ResolvedFontOptions),
71+
).toMatchSnapshot()
72+
73+
expect(
74+
generateCoollabsFontsUrl({
75+
webfonts: ['Fira Code', 'PT Serif'],
76+
weights: ['200', '400', '600'],
77+
italic: true,
78+
provider: 'google',
79+
} as ResolvedFontOptions),
80+
).toMatchSnapshot()
81+
})
82+
6483
it('roots', async () => {
6584
const { cliRoot, clientRoot, userRoot, userWorkspaceRoot } = await getRoots(resolve('slides.md'))
6685
const expectRelative = (v: string) => expect(slash(relative(__dirname, v)))

0 commit comments

Comments
 (0)