Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upstream merge #28

Merged
merged 6 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ const { DateTime } = require("luxon");
const { promisify } = require("util");
const fs = require("fs");
const hasha = require("hasha");
const readFile = promisify(require("fs").readFile);
const readFile = promisify(fs.readFile);
const stat = promisify(fs.stat);
const execFile = promisify(require("child_process").execFile);
const pluginRss = require("@11ty/eleventy-plugin-rss");
const pluginSyntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const pluginNavigation = require("@11ty/eleventy-navigation");
Expand Down Expand Up @@ -89,9 +91,40 @@ module.exports = function (eleventyConfig) {
.catch((error) => callback(error));
});

eleventyConfig.addFilter("lastModifiedDate", function (filename) {
const stats = fs.statSync(filename);
return stats.mtime; // Date
async function lastModifiedDate(filename) {
try {
const { stdout } = await execFile("git", [
"log",
"-1",
"--format=%cd",
filename,
]);
return new Date(stdout);
} catch (e) {
console.error(e.message);
// Fallback to stat if git isn't working.
const stats = await stat(filename);
return stats.mtime; // Date
}
}
// Cache the lastModifiedDate call because shelling out to git is expensive.
// This means the lastModifiedDate will never change per single eleventy invocation.
const lastModifiedDateCache = new Map();
eleventyConfig.addNunjucksAsyncFilter("lastModifiedDate", function (
filename,
callback
) {
const call = (result) => {
result.then((date) => callback(null, date));
result.catch((error) => callback(error));
};
const cached = lastModifiedDateCache.get(filename);
if (cached) {
return call(cached);
}
const promise = lastModifiedDate(filename);
lastModifiedDateCache.set(filename, promise);
call(promise);
});

eleventyConfig.addFilter("encodeURIComponent", function (str) {
Expand Down
2 changes: 1 addition & 1 deletion _11ty/blurry-placeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ module.exports = async function (src) {
<feFuncA type="discrete" tableValues="1 1"></feFuncA>
</feComponentTransfer>
</filter>
<image filter="url(#b)" x="0" y="0"
<image filter="url(#b)" preserveAspectRatio="none"
height="100%" width="100%"
xlink:href="${dataURI.src}">
</image>
Expand Down
1 change: 1 addition & 0 deletions _includes/layouts/base.njk
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<link rel="icon" href="{{ '/img/favicon/favicon-192x192.png' | addHash }}" type="image/png">
{% endif %}
<meta name="theme-color" content="#f9c412">
<meta name="robots" content="max-snippet:-1, max-image-preview: large, max-video-preview: -1">
<title>{{ renderData.title or title or metadata.title | escape }}</title>
<meta property="og:title" content="{{ renderData.title or title or metadata.title | escape }}">
{% set desc = renderData.description or description %}
Expand Down
7 changes: 4 additions & 3 deletions test/test-generic-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ describe("check build output for a generic post", () => {

it("should have script elements", () => {
const scripts = doc.querySelectorAll("script[src]");
expect(scripts).to.have.length(GA_ID ? 2 : 1);
let has_ga_id = GA_ID ? 1 : 0;
expect(scripts).to.have.length(has_ga_id + 1); // NOTE: update this when adding more <script>
expect(scripts[0].getAttribute("src")).to.match(
/^\/js\/min\.js\?hash=\w+/
);
Expand Down Expand Up @@ -168,8 +169,8 @@ describe("check build output for a generic post", () => {
//expect(avif.type).to.equal("image/avif");
expect(jpg.sizes).to.equal("(max-width: 608px) 100vw, 608px");
expect(webp.sizes).to.equal("(max-width: 608px) 100vw, 608px");
expect(img.height).to.equal(850);
expect(img.width).to.equal(1280);
expect(img.height).to.match(/^\d+$/);
expect(img.width).to.match(/^\d+$/);
expect(img.getAttribute("loading")).to.equal("lazy");
expect(img.getAttribute("decoding")).to.equal("async");
// JSDom fails to parse the style attribute properly
Expand Down