Skip to content

Commit

Permalink
wip: iswc
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximvdw committed Oct 13, 2022
1 parent f72f976 commit bf79245
Show file tree
Hide file tree
Showing 15 changed files with 410 additions and 119 deletions.
4 changes: 3 additions & 1 deletion .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ const pluginSEO = require("eleventy-plugin-seo");
const pluginSASS = require("eleventy-sass");
const pluginSitemap = require("@quasibit/eleventy-plugin-sitemap");
const pluginValidator = require('eleventy-plugin-html-validate');
const pluginRss = require("@11ty/eleventy-plugin-rss");

const { DateTime } = require("luxon");
const fs = require('fs');
const nunjucks = require("nunjucks");
const markdown = require('nunjucks-markdown');
const markdownItAttrs = require('markdown-it-attrs');
const pluginPDFEmbed = require('eleventy-plugin-pdfembed');
const faviconPlugin = require("eleventy-favicon");
const faviconPlugin = require("./_scripts/favicon");
require('dotenv').config();
const { decktape } = require("./_scripts/decktape");
const { qr } = require("./_scripts/qr");
Expand Down Expand Up @@ -47,6 +48,7 @@ module.exports = function (el) {
},
});
el.addPlugin(faviconPlugin);
el.addPlugin(pluginRss);
//el.addPlugin(pluginValidator);

/* PDF Embedding */
Expand Down
2 changes: 2 additions & 0 deletions _includes/head.njk
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css">
<link rel="stylesheet" href="https://cdn.rawgit.com/jpswalsh/academicons/master/css/academicons.min.css">

<link type="application/atom+xml" title="OpenHPS News Posts" rel="alternate" href="{% canonicalURL '/feed.xml' %}" title="{{ site.title }}" >

{% favicon './images/logo_notext_nogradient.svg' %}
<meta name="theme-color" content="#9c0000">

Expand Down
75 changes: 75 additions & 0 deletions _scripts/favicon.js
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">
`;
});
};
46 changes: 46 additions & 0 deletions feed.njk
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>
Loading

0 comments on commit bf79245

Please sign in to comment.