From cfc23e6441f14c9be6a6fc890e673ae918430ee6 Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Tue, 28 Nov 2023 10:19:03 -0300 Subject: [PATCH] fix: parse fonts --- src/downloader.ts | 20 +++++++++----------- test/download.test.ts | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/downloader.ts b/src/downloader.ts index 5da0c10..8ec866f 100644 --- a/src/downloader.ts +++ b/src/downloader.ts @@ -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' @@ -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(/\.$/, '') diff --git a/test/download.test.ts b/test/download.test.ts index a9203ac..49f99ca 100644 --- a/test/download.test.ts +++ b/test/download.test.ts @@ -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'