-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This reverts commit 436a7b0.
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const https = require('https') | ||
|
||
const UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36' | ||
|
||
const isValidURL = url => { | ||
return /fonts.googleapis.com/.test(url) | ||
} | ||
|
||
const downloadFont = url => { | ||
return new Promise((resolve) => { | ||
let rawData = '' | ||
https.get( | ||
url, | ||
{ | ||
headers: { | ||
'user-agent': UA, | ||
}, | ||
}, | ||
res => { | ||
res.on('data', chunk => { | ||
rawData += chunk | ||
}) | ||
res.on('end', () => { | ||
resolve(rawData.toString('utf8')) | ||
}) | ||
} | ||
) | ||
}) | ||
} | ||
|
||
const createInlineCss = async url => { | ||
if (!isValidURL(url)) { | ||
throw new Error('Invalid Google Fonts URL') | ||
} | ||
|
||
const content = await downloadFont(url) | ||
|
||
return ( | ||
`<link rel="preconnect" href="https://fonts.gstatic.com">`+ | ||
`<link data-href="${url}" rel="stylesheet">`+ | ||
`<style data-href='${url}'>${content.replace(/(\n|\s)/g, '')}</style>` | ||
) | ||
} | ||
|
||
module.exports = { | ||
isValidURL: isValidURL, | ||
createInlineCss: createInlineCss | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "eleventy-google-fonts", | ||
"version": "0.0.1", | ||
"main": "index.js", | ||
"repository": "https://github.com/takanorip/eleventy-google-fonts.git", | ||
"author": "takanorip <[email protected]>", | ||
"license": "MIT", | ||
"scripts": { | ||
"type-check": "tsc --noEmit", | ||
"type-check:watch": "npm run type-check -- --watch", | ||
"build": "npm run build:types && npm run build:js", | ||
"build:types": "tsc --emitDeclarationOnly", | ||
"build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline" | ||
}, | ||
"dependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|