Skip to content

Commit

Permalink
fix: parse fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza committed Nov 28, 2023
1 parent 1c1a0b9 commit cfc23e6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
20 changes: 9 additions & 11 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 @@ -168,19 +168,17 @@ 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 }
if (fonts.find(font => font.inputFont === url)) { continue }
const ext = extname(url).replace(/^\./, '') || 'woff2'

const filename = basename(urlPathname, ext) || ''
const newFilename = formatFontFileName('{_family}-{weight}-{i}.{ext}', {
if (fonts.find(font => font.inputFont === url)) {
continue
}

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: ext || 'woff2',
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

0 comments on commit cfc23e6

Please sign in to comment.