-
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.
- Loading branch information
Showing
15 changed files
with
410 additions
and
119 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
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
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,75 @@ | ||
/* Based on: https://github.com/atomrc/eleventy-favicon/blob/master/.eleventy.js */ | ||
const sharp = require("sharp"); | ||
const toIco = require("to-ico"); | ||
const fs = require("fs").promises; | ||
|
||
// Caches all the file generations that were made. | ||
// It keeps track of the mtime of the source file so the cache can be invalidated if the source changes | ||
let cache = {}; | ||
|
||
function generateIcoFavicon({ width, height, density }, sourcePath) { | ||
const faviconDimensions = [32, 64]; | ||
// Create buffer for each size | ||
return Promise.all( | ||
faviconDimensions.map((dimension) => | ||
sharp(sourcePath, { | ||
density: (dimension / Math.max(width, height)) * density, | ||
}) | ||
.resize(dimension, dimension) | ||
.toBuffer() | ||
) | ||
).then((buffers) => toIco(buffers)); | ||
} | ||
|
||
function generatePngFavicon({ density, width, height }, sourcePath) { | ||
return sharp(sourcePath, { | ||
density: (180 / Math.max(width, height)) * density, | ||
}) | ||
.resize(180, 180) | ||
.png() | ||
.toBuffer(); | ||
} | ||
|
||
function saveFile(destination) { | ||
return function (buffer) { | ||
return fs.writeFile(destination, buffer); | ||
}; | ||
} | ||
|
||
const faviconTypes = [ | ||
["favicon.ico", generateIcoFavicon], | ||
["apple-touch-icon.png", generatePngFavicon], | ||
]; | ||
|
||
const defaultOptions = { | ||
destination: "./_site", | ||
}; | ||
module.exports = function (config, options = defaultOptions) { | ||
const destination = options.destination || defaultOptions.destination; | ||
config.addAsyncShortcode("favicon", async function (faviconFile) { | ||
const { mtimeMs } = await fs.stat(faviconFile); | ||
const lastGeneration = cache[faviconFile] || { mtime: 0, svg: false }; | ||
if (mtimeMs > lastGeneration.mtime) { | ||
const metadata = await sharp(faviconFile).metadata(); | ||
cache[faviconFile] = { mtime: mtimeMs, svg: metadata.format === "svg" }; | ||
faviconTypes.forEach(([name, generator]) => | ||
generator(metadata, faviconFile).then( | ||
saveFile(`${destination}/${name}`) | ||
) | ||
); | ||
if (cache[faviconFile].svg) { | ||
fs.copyFile(faviconFile, `${destination}/favicon.svg`); | ||
} | ||
} | ||
|
||
const svgEntry = cache[faviconFile].svg | ||
? '<link rel="icon" type="image/svg+xml" href="/favicon.svg">' | ||
: ""; | ||
|
||
return ` | ||
<link rel="icon" href="/favicon.ico"> | ||
${svgEntry} | ||
<link rel="apple-touch-icon" href="/apple-touch-icon.png"> | ||
`; | ||
}); | ||
}; |
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,46 @@ | ||
---json | ||
{ | ||
"permalink": "feed.xml", | ||
"eleventyExcludeFromCollections": true, | ||
"normalizeAbsoluteUrls": true, | ||
"metadata": { | ||
"title": "OpenHPS", | ||
"subtitle": "An open-source hybrid positioning system framework.", | ||
"language": "en-UK", | ||
"url": "https://openhps.org/", | ||
"author": { | ||
"name": "OpenHPS", | ||
"email": "[email protected]" | ||
} | ||
} | ||
} | ||
--- | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<feed xmlns="http://www.w3.org/2005/Atom" xml:base="{{ metadata.language }}"> | ||
<title>{{ metadata.title }}</title> | ||
<subtitle>{{ metadata.subtitle }}</subtitle> | ||
<link href="{{ permalink | url | absoluteUrl(metadata.url) }}" rel="self"/> | ||
<link href="{{ metadata.url }}"/> | ||
<updated>{{ collections.posts | getNewestCollectionItemDate | dateToRfc3339 }}</updated> | ||
<id>{{ metadata.url }}</id> | ||
<author> | ||
<name>{{ metadata.author.name }}</name> | ||
<email>{{ metadata.author.email }}</email> | ||
</author> | ||
{%- for post in collections.posts | reverse %} | ||
{% set absolutePostUrl %}{{ post.url | url | absoluteUrl(metadata.url) }}{% endset %} | ||
<entry> | ||
<title>{{ post.data.title }}</title> | ||
<link href="{{ absolutePostUrl }}"/> | ||
<updated>{{ post.date | dateToRfc3339 }}</updated> | ||
<id>{{ absolutePostUrl }}</id> | ||
<content type="html"> | ||
{%- if normalizeAbsoluteUrls -%} | ||
{{ post.templateContent | htmlToAbsoluteUrls(absolutePostUrl) }} | ||
{%- else -%} | ||
{{ post.templateContent }} | ||
{%- endif -%} | ||
</content> | ||
</entry> | ||
{%- endfor %} | ||
</feed> |
Oops, something went wrong.