Skip to content

Commit

Permalink
Revert "Revert "init""
Browse files Browse the repository at this point in the history
This reverts commit 436a7b0.
  • Loading branch information
takanorip committed Jan 2, 2021
1 parent 436a7b0 commit d760f7c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
48 changes: 48 additions & 0 deletions eleventy-google-fonts.js
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
}
16 changes: 16 additions & 0 deletions package.json
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": {}
}
4 changes: 4 additions & 0 deletions yarn.lock
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


0 comments on commit d760f7c

Please sign in to comment.