docs: serve GitHub avatars locally

This commit is contained in:
Starbeamrainbowlabs 2022-07-06 01:41:27 +01:00
parent 8eec6a32f7
commit 0e7b292bb1
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
4 changed files with 77 additions and 7 deletions

View file

@ -11,8 +11,12 @@ const CleanCSS = require("clean-css");
const { minify: minify_html } = require("html-minifier-terser"); const { minify: minify_html } = require("html-minifier-terser");
const HTMLPicture = require("./lib/HTMLPicture.js"); const HTMLPicture = require("./lib/HTMLPicture.js");
const FileFetcher = require("./lib/FileFetcher.js");
const file_fetcher = new FileFetcher();
const is_production = typeof process.env.NODE_ENV === "string" && process.env.NODE_ENV === "production"; const is_production = typeof process.env.NODE_ENV === "string" && process.env.NODE_ENV === "production";
var nextid = 0; var nextid = 0;
const image_filename_format = (_id, src, width, format, _options) => { const image_filename_format = (_id, src, width, format, _options) => {
@ -63,10 +67,6 @@ async function shortcode_gallerybox(content, src, idthis, idprev, idnext) {
} }
async function fetch(url) { async function fetch(url) {
const pkg_obj = JSON.parse(await fs.promises.readFile(
path.join(__dirname, "package.json"), "utf8"
));
return (await phin({ return (await phin({
url, url,
headers: { headers: {
@ -77,6 +77,10 @@ async function fetch(url) {
})).body; })).body;
} }
function fetch_file(url) {
return file_fetcher.fetch_file(url);
}
function do_minifycss(source, output_path) { function do_minifycss(source, output_path) {
if(!output_path.endsWith(".css") || !is_production) return source; if(!output_path.endsWith(".css") || !is_production) return source;
@ -122,6 +126,7 @@ module.exports = function(eleventyConfig) {
eleventyConfig.addPassthroughCopy("img2brush/img2brush.js"); eleventyConfig.addPassthroughCopy("img2brush/img2brush.js");
eleventyConfig.addAsyncShortcode("fetch", fetch); eleventyConfig.addAsyncShortcode("fetch", fetch);
eleventyConfig.addFilter("fetch_file", fetch_file);
// eleventyConfig.addPassthroughCopy("images"); // eleventyConfig.addPassthroughCopy("images");
// eleventyConfig.addPassthroughCopy("css"); // eleventyConfig.addPassthroughCopy("css");

View file

@ -45,11 +45,11 @@
{{ content | safe }} {{ content | safe }}
<footer class="shadow-top"> <footer class="shadow-top">
<p>WorldEditAdditions built with ❤️ by these awesome people:</p> <p>WorldEditAdditions is built with ❤️ by these awesome people:</p>
<ul class="contributor-list"> <ul class="contributor-list">
{% for contributor in contributors %} {% for contributor in contributors %}
<li><a href="{{ contributor.profile_url }}"> <li><a href="{{ contributor.profile_url }}">
<img class="icon large" src="{{ contributor.avatar_url }}" alt="{{ contributor.name }}" /> <img class="icon large" src="{{ contributor.avatar_url | fetch_file }}" alt="{{ contributor.name }}" />
<span>{{ contributor.name }}</span> <span>{{ contributor.name }}</span>
</a></li> </a></li>
{% endfor %} {% endfor %}

65
.docs/lib/FileFetcher.js Normal file
View file

@ -0,0 +1,65 @@
"use strict";
const path = require("path");
const fs = require("fs");
const os = require("os");
const phin = require("phin");
const a = require("./Ansi.js");
const pretty_ms = require("pretty-ms");
class FileFetcher {
#cache = [];
#pkg_obj = null;
constructor() {
}
fetch_file(url) {
let target_client = path.join(`/img`, path.basename(url));
if(this.#cache.includes(url)) return target_client;
this.#cache.push(url);
this.download_file(url); // Returns a promise! We fire-and-forget it though 'cause this function *must* be synchronous :-/
return target_client;
}
async download_file(url) {
const time_start = new Date();
if(this.#pkg_obj === null) {
this.#pkg_obj = JSON.parse(await fs.promises.readFile(
path.join(path.dirname(__dirname), "package.json"), "utf8"
));
}
let target_download = path.join(`_site/img`, path.basename(url));
const response = await phin({
url,
headers: {
"user-agent": `WorldEditAdditionsStaticBuilder/${this.#pkg_obj.version} (Node.js/${process.version}; ${os.platform()} ${os.arch()}) eleventy/${this.#pkg_obj.devDependencies["@11ty/eleventy"].replace(/\^/, "")}`
},
followRedirects: true,
parse: 'none' // Returns a Buffer
// If we stream and pipe to a file, the build never ends :-/
});
await fs.promises.writeFile(target_download, response.body);
console.log([
`${a.fred}${a.hicol}FETCH_FILE${a.reset}`,
`${a.fyellow}${pretty_ms(new Date() - time_start)}${a.reset}`,
`${a.fgreen}${url}${a.reset}`
].join("\t"));
}
}
module.exports = FileFetcher;

View file

@ -74,7 +74,7 @@ async function srcset(source_image, target_dir, urlpath, format = "__AUTO__", si
quality, quality,
strip strip
}); });
console.log(`IMAGE\t${a.fcyan}${queue.size}/${queue.pending} tasks${a.reset}\t${a.fyellow}${pretty_ms(new Date() - start)}${a.reset}\t${a.fgreen}${target_current}${a.reset}`); console.log(`${a.fmagenta}${a.hicol}IMAGE${a.reset}\t${a.fcyan}${queue.size}/${queue.pending} tasks${a.reset}\t${a.fyellow}${pretty_ms(new Date() - start)}${a.reset}\t${a.fgreen}${target_current}${a.reset}`);
}); });
// const size_target = await imagickal.dimensions(target_current); // const size_target = await imagickal.dimensions(target_current);