Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
with:
fetch-depth: 0
- run: corepack enable
- uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0
- uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
with:
node-version: latest

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "unifont",
"type": "module",
"version": "0.7.4",
"packageManager": "pnpm@10.30.1",
"packageManager": "pnpm@10.32.1",
"description": "Framework agnostic tools for accessing data from font CDNs and providers",
"license": "MIT",
"repository": {
Expand Down Expand Up @@ -37,16 +37,16 @@
"ohash": "^2.0.11"
},
"devDependencies": {
"@antfu/eslint-config": "7.4.3",
"@antfu/eslint-config": "7.7.2",
"@types/css-tree": "2.3.11",
"@types/node": "24.10.13",
"@types/node": "24.12.0",
"@vitest/coverage-v8": "4.0.18",
"bumpp": "10.4.1",
"changelogithub": "14.0.0",
"eslint": "10.0.3",
"lint-staged": "16.2.7",
"lint-staged": "16.3.3",
"simple-git-hooks": "2.13.1",
"tsdown": "0.20.3",
"tsdown": "0.21.2",
"typescript": "5.9.3",
"unstorage": "1.17.4",
"vite": "7.3.1",
Expand Down
964 changes: 485 additions & 479 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ function createCacheKey(...fragments: unknown[]): string {
return parts.join(':')
}

const RE = /[^\w.-]/g

function sanitize(input: string): string {
if (!input)
return ''

return input.replace(/[^\w.-]/g, '_')
return input.replace(RE, '_')
}
4 changes: 3 additions & 1 deletion src/css/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ export function extractFontFaceData(css: string, family?: string): FontFaceData[
return mergeFontSources(fontFaces)
}

const RE = /^(?<quote>['"])(.*)\k<quote>$/

function processRawValue(value: string) {
return value.split(',').map(v => v.trim().replace(/^(?<quote>['"])(.*)\k<quote>$/, '$2'))
return value.split(',').map(v => v.trim().replace(RE, '$2'))
}

function extractCSSValue(node: Declaration) {
Expand Down
2 changes: 1 addition & 1 deletion src/providers/bunny.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default defineFontProvider('bunny', async (_options, ctx) => {
if (weights.length === 0 || styles.size === 0)
return []

const resolvedVariants = weights.flatMap(w => [...styles].map(s => `${w.weight}${s}`))
const resolvedVariants = weights.flatMap(w => Array.from(styles, s => `${w.weight}${s}`))

const css = await fontAPI<string>('/css', {
query: {
Expand Down
4 changes: 3 additions & 1 deletion src/providers/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export default defineFontProvider('google', async (providerOptions: GoogleProvid

async function getFontDetails(family: string, options: ResolveFontOptions<GoogleFamilyOptions>) {
const font = googleFonts.find(font => font.family === family)!
// https://github.com/e18e/eslint-plugin/issues/69
// eslint-disable-next-line e18e/prefer-array-to-sorted
const styles = [...new Set(options.styles.map(i => styleMap[i]))].sort()
const glyphs = (options.options?.experimental?.glyphs ?? providerOptions.experimental?.glyphs?.[family])?.join('')
const weights = prepareWeights({
Expand Down Expand Up @@ -93,7 +95,7 @@ export default defineFontProvider('google', async (providerOptions: GoogleProvid
resolvedVariants = axisValue
}
else {
resolvedVariants = resolvedVariants.flatMap(v => [...axisValue].map(o => [v, o].join(','))).sort()
resolvedVariants = resolvedVariants.flatMap(v => Array.from(axisValue, o => [v, o].join(','))).sort()
}
resolvedAxes.push(axis)
}
Expand Down
10 changes: 7 additions & 3 deletions src/providers/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,22 @@ function slugToFamily(slug: string): string {
return slug.split('-').map(s => s.charAt(0).toUpperCase() + s.slice(1)).join(' ')
}

const SPACE_RE = /\s+/g

/** Convert a family name like "Geist Sans" to a fontsource slug "geist-sans" */
function familyToSlug(family: string): string {
return family.toLowerCase().replace(/\s+/g, '-')
return family.toLowerCase().replace(SPACE_RE, '-')
}

const VARIABLE_RE = / Variable$/

/**
* Guess the npm package name and CSS file for a font family that wasn't
* found in the auto-detected packages. Uses fontsource conventions as fallback.
*/
function guessPackageForFamily(family: string): { pkgName: string, file: string } {
if (family.endsWith(' Variable')) {
return { pkgName: `@fontsource-variable/${familyToSlug(family.replace(/ Variable$/, ''))}`, file: 'index.css' }
return { pkgName: `@fontsource-variable/${familyToSlug(family.replace(VARIABLE_RE, ''))}`, file: 'index.css' }
}
return { pkgName: `@fontsource/${familyToSlug(family)}`, file: 'index.css' }
}
Expand Down Expand Up @@ -282,7 +286,7 @@ export default defineFontProvider('npm', (providerOptions: NpmProviderOptions, c
if (fonts.size === 0) {
return undefined
}
return [...fonts.values()].map(f => f.family)
return Array.from(fonts.values(), f => f.family)
},

async resolveFont(family: string, options: ResolveFontOptions<NpmFamilyOptions>) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function prepareWeights({
}
}

return [...new Set(collectedWeights)].map(weight => ({
return Array.from(new Set(collectedWeights), weight => ({
weight,
variable: weight.includes(' '),
}))
Expand Down
8 changes: 4 additions & 4 deletions test/providers/bunny.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('bunny', () => {
const { fonts } = await unifont.resolveFont('Alef', {
weights: ['400 1100'],
})
expect(fonts.length).toBe(4)
expect(fonts.length).toBe(2)
})

it('filters subsets correctly', async () => {
Expand All @@ -83,7 +83,7 @@ describe('bunny', () => {
weights: ['400'],
})
expect(fonts.length).toBe(1)
expect(Array.from(new Set(fonts.flatMap(font => font.src.map(source => 'name' in source ? source.name : source.format))))).toStrictEqual(['woff2'])
expect([...new Set(fonts.flatMap(font => font.src.map(source => 'name' in source ? source.name : source.format)))]).toStrictEqual(['woff2'])
})

it('woff', async () => {
Expand All @@ -95,7 +95,7 @@ describe('bunny', () => {
weights: ['400'],
})
expect(fonts.length).toBe(1)
expect(Array.from(new Set(fonts.flatMap(font => font.src.map(source => 'name' in source ? source.name : source.format))))).toStrictEqual(['woff'])
expect([...new Set(fonts.flatMap(font => font.src.map(source => 'name' in source ? source.name : source.format)))]).toStrictEqual(['woff'])
})

it('ttf', async () => {
Expand Down Expand Up @@ -140,7 +140,7 @@ describe('bunny', () => {
weights: ['400'],
})
expect(fonts.length).toBe(1)
expect(Array.from(new Set(fonts.flatMap(font => font.src.map(source => 'name' in source ? source.name : source.format))))).toStrictEqual(['woff2', 'woff'])
expect([...new Set(fonts.flatMap(font => font.src.map(source => 'name' in source ? source.name : source.format)))]).toStrictEqual(['woff2', 'woff'])
})
})
})
8 changes: 4 additions & 4 deletions test/providers/googleicons.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('googleicons', () => {
weights: ['400'],
})
expect(fonts.length).toBe(7)
expect(Array.from(new Set(fonts.flatMap(font => font.src.map(source => 'name' in source ? source.name : source.format))))).toStrictEqual(['woff'])
expect([...new Set(fonts.flatMap(font => font.src.map(source => 'name' in source ? source.name : source.format)))]).toStrictEqual(['woff'])
})

it('ttf', async () => {
Expand All @@ -161,7 +161,7 @@ describe('googleicons', () => {
weights: ['400'],
})
expect(fonts.length).toBe(7)
expect(Array.from(new Set(fonts.flatMap(font => font.src.map(source => 'name' in source ? source.name : source.format))))).toStrictEqual(['truetype'])
expect([...new Set(fonts.flatMap(font => font.src.map(source => 'name' in source ? source.name : source.format)))]).toStrictEqual(['truetype'])
})

it('eot', async () => {
Expand All @@ -173,7 +173,7 @@ describe('googleicons', () => {
weights: ['400'],
})
expect(fonts.length).toBe(7)
expect(Array.from(new Set(fonts.flatMap(font => font.src.map(source => 'name' in source ? source.name : source.format))))).toStrictEqual([undefined])
expect([...new Set(fonts.flatMap(font => font.src.map(source => 'name' in source ? source.name : source.format)))]).toStrictEqual([undefined])
})

it('otf', async () => {
Expand All @@ -196,7 +196,7 @@ describe('googleicons', () => {
weights: ['400'],
})
expect(fonts.length).toBe(8)
expect(Array.from(new Set(fonts.flatMap(font => font.src.map(source => 'name' in source ? source.name : source.format))))).toStrictEqual(['woff2', undefined, 'woff', 'truetype'])
expect([...new Set(fonts.flatMap(font => font.src.map(source => 'name' in source ? source.name : source.format)))]).toStrictEqual(['woff2', undefined, 'woff', 'truetype'])
})
})
})
4 changes: 3 additions & 1 deletion test/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ export function groupBy<T, K extends PropertyKey>(arr: T[], by: (arg: T) => K):
}, {} as Record<K, T[]>)
}

const RE = /^((https?:)?\/\/[^/]+)\/.*(\.[^.]+)?$/

export function sanitizeFontSource(data: FontFaceData[]) {
return data.map(d => ({
...d,
src: d.src.map(s => ({
...s,
url: 'url' in s ? s.url.replace(/^((https?:)?\/\/[^/]+)\/.*(\.[^.]+)?$/, '$1/font$3') : undefined,
url: 'url' in s ? s.url.replace(RE, '$1/font$3') : undefined,
})),
}))
}
Expand Down
Loading