Skip to content
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

fix: parse fonts #56

Merged
merged 2 commits into from
Dec 5, 2023
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
15 changes: 5 additions & 10 deletions src/downloader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync, readFileSync, mkdirSync, writeFileSync, rmSync } from 'node:fs'
import { basename, extname, posix, resolve, dirname } from 'node:path'
import { extname, posix, resolve, dirname } from 'node:path'
import { ofetch } from 'ofetch'
import { Hookable } from 'hookable'
import { isValidURL } from './is-valid-url'
Expand Down Expand Up @@ -187,18 +187,13 @@ function parseFontsFromCss (content: string, fontsPath: string): FontInputOutput
let match2
while ((match2 = re.url.exec(fontface)) !== null) {
const [forReplace, url] = match2
const urlPathname = new URL(url).pathname
const ext = extname(urlPathname)
if (ext.length < 2) { continue }
const ext = extname(url).replace(/^\./, '') || 'woff2'

const filename = basename(urlPathname, ext) || ''
const newFilename = formatFontFileName('{_family}-{weight}-{i}.{ext}', {
const newFilename = formatFontFileName('{family}-{weight}-{i}.{ext}', {
comment: comment || '',
family,
family: family.replace(/\s+/g, '_'),
weight: weight || '',
filename,
_family: family.replace(/\s+/g, '_'),
ext: ext.replace(/^\./, '') || '',
ext,
i: String(i++)
}).replace(/\.$/, '')

Expand Down
34 changes: 34 additions & 0 deletions test/download.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,40 @@ describe('download', () => {
rmSync(outputDir, { recursive: true, force: true })
}, 60000)

test('with a text', async () => {
const outputDir = temporaryDirectory()
const stylePath = 'font.css'
const fontsDir = 'fonts'

await download('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&text=example', {
outputDir,
stylePath,
fontsDir
}).execute()

expect(existsSync(join(outputDir, stylePath))).toBe(true)
expect(existsSync(join(outputDir, fontsDir))).toBe(true)

rmSync(outputDir, { recursive: true, force: true })
}, 60000)

test('complex text', async () => {
const outputDir = temporaryDirectory()
const stylePath = 'font.css'
const fontsDir = 'fonts'

await download('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&text=A%C4%84BC%C4%86DE%C4%98FGHIJKL%C5%81MN%C5%83O%C3%93PRSTUWXYZ%C5%BB%C5%B9a%C4%85bc%C4%87de%C4%99fghijkl%C5%82mn%C5%84o%C3%B3prstuwxyz%C5%BC%C5%BA1234567890+`!@%23$%25^%26*()-=_%2B%5B%5D%7B%7D%5C|;\':%22,./%3C%3E?', {
outputDir,
stylePath,
fontsDir
}).execute()

expect(existsSync(join(outputDir, stylePath))).toBe(true)
expect(existsSync(join(outputDir, fontsDir))).toBe(true)

rmSync(outputDir, { recursive: true, force: true })
}, 60000)

test('force overwriting when a different url', async () => {
const outputDir = temporaryDirectory()
const stylePath = 'font.css'
Expand Down