2021-10-17 04:00:24 +02:00
|
|
|
"use strict";
|
|
|
|
|
2021-07-06 03:47:45 +02:00
|
|
|
const os = require("os");
|
2021-06-14 02:36:13 +02:00
|
|
|
const fs = require("fs");
|
2021-05-31 22:06:17 +02:00
|
|
|
const path = require("path");
|
|
|
|
|
2021-10-17 04:00:24 +02:00
|
|
|
const debug = require("debug");
|
2021-05-31 22:06:17 +02:00
|
|
|
const htmlentities = require("html-entities");
|
2021-07-06 03:47:45 +02:00
|
|
|
const phin = require("phin");
|
2022-07-06 01:28:55 +02:00
|
|
|
const CleanCSS = require("clean-css");
|
|
|
|
const { minify: minify_html } = require("html-minifier-terser");
|
2021-07-06 03:47:45 +02:00
|
|
|
|
2021-10-17 04:00:24 +02:00
|
|
|
const HTMLPicture = require("./lib/HTMLPicture.js");
|
2022-07-06 02:41:27 +02:00
|
|
|
const FileFetcher = require("./lib/FileFetcher.js");
|
|
|
|
|
|
|
|
const file_fetcher = new FileFetcher();
|
2021-07-06 03:47:45 +02:00
|
|
|
|
2022-07-06 01:28:55 +02:00
|
|
|
const is_production = typeof process.env.NODE_ENV === "string" && process.env.NODE_ENV === "production";
|
2022-07-06 02:41:27 +02:00
|
|
|
|
2021-05-31 22:06:17 +02:00
|
|
|
var nextid = 0;
|
|
|
|
|
2021-06-14 02:36:13 +02:00
|
|
|
const image_filename_format = (_id, src, width, format, _options) => {
|
|
|
|
const extension = path.extname(src);
|
|
|
|
const name = path.basename(src, extension);
|
|
|
|
return `${name}-${width}w.${format}`;
|
|
|
|
};
|
|
|
|
|
2021-10-17 04:00:24 +02:00
|
|
|
async function shortcode_image(src, alt) {
|
2021-05-31 22:06:17 +02:00
|
|
|
|
2021-10-17 04:00:24 +02:00
|
|
|
return HTMLPicture(
|
|
|
|
src, alt,
|
|
|
|
`./_site/img`, `/img`
|
|
|
|
);
|
2021-05-31 22:06:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function shortcode_image_url(src) {
|
2021-10-17 04:00:24 +02:00
|
|
|
const src_parsed = path.parse(src);
|
|
|
|
const target = path.join(`./_site/img`, src_parsed.base);
|
|
|
|
if(!fs.existsSync(path.dirname(target)))
|
|
|
|
await fs.promises.mkdir(target_dir, { recursive: true });
|
|
|
|
await fs.promises.copyFile(src, target);
|
2021-05-31 22:06:17 +02:00
|
|
|
|
2021-10-17 04:00:24 +02:00
|
|
|
return path.join(`/img`, src_parsed.base);
|
2021-05-31 22:06:17 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 03:25:45 +02:00
|
|
|
async function shortcode_image_urlpass(src) {
|
2021-06-14 02:36:13 +02:00
|
|
|
let target_dir = `./_site/img`;
|
|
|
|
if(!fs.existsSync(target_dir))
|
|
|
|
await fs.promises.mkdir(target_dir, { recursive: true });
|
|
|
|
let filename = path.basename(src);
|
|
|
|
// Generally speaking we optimise PNGs *very* well with oxipng/Zopfli,
|
|
|
|
// and the Image plugin doesn't respect this
|
|
|
|
await fs.promises.copyFile(src, path.join(target_dir, filename));
|
|
|
|
return `/img/${filename}`;
|
|
|
|
}
|
|
|
|
|
2021-06-09 01:34:25 +02:00
|
|
|
async function shortcode_gallerybox(content, src, idthis, idprev, idnext) {
|
|
|
|
return `<figure class="gallerybox-item" id="${idthis}">
|
|
|
|
<!-- ${await shortcode_image(src, "", "gallerybox-thumb", "300w")} -->
|
|
|
|
${await shortcode_image(src, "", "", "1920w")}
|
|
|
|
|
|
|
|
<figcaption>${content}</figcaption>
|
|
|
|
|
|
|
|
<a class="gallerybox-prev" href="#${idprev}">❰</a>
|
|
|
|
<a class="gallerybox-next" href="#${idnext}">❱</a>
|
|
|
|
</figure>`;
|
2021-05-31 22:06:17 +02:00
|
|
|
}
|
|
|
|
|
2021-07-06 03:47:45 +02:00
|
|
|
async function fetch(url) {
|
|
|
|
return (await phin({
|
|
|
|
url,
|
|
|
|
headers: {
|
2021-10-17 04:00:24 +02:00
|
|
|
"user-agent": `WorldEditAdditionsStaticBuilder/${pkg_obj.version} (Node.js/${process.version}; ${os.platform()} ${os.arch()}) eleventy/${pkg_obj.devDependencies["@11ty/eleventy"].replace(/\^/, "")}`
|
2021-07-06 03:47:45 +02:00
|
|
|
},
|
|
|
|
followRedirects: true,
|
|
|
|
parse: "string"
|
|
|
|
})).body;
|
|
|
|
}
|
|
|
|
|
2022-07-06 02:41:27 +02:00
|
|
|
function fetch_file(url) {
|
|
|
|
return file_fetcher.fetch_file(url);
|
|
|
|
}
|
|
|
|
|
2022-07-06 01:28:55 +02:00
|
|
|
function do_minifycss(source, output_path) {
|
|
|
|
if(!output_path.endsWith(".css") || !is_production) return source;
|
|
|
|
|
|
|
|
const result = new CleanCSS({
|
|
|
|
level: 2
|
|
|
|
}).minify(source).styles.trim();
|
|
|
|
console.log(`MINIFY ${output_path}`, source.length, `→`, result.length, `(${((1 - (result.length / source.length)) * 100).toFixed(2)}% reduction)`);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function do_minifyhtml(source, output_path) {
|
|
|
|
if(!output_path.endsWith(".html") || !is_production) return source;
|
|
|
|
|
|
|
|
const result = await minify_html(source, {
|
|
|
|
collapseBooleanAttributes: true,
|
|
|
|
collapseWhitespace: true,
|
|
|
|
collapseInlineTagWhitespace: true,
|
|
|
|
continueOnParseError: true,
|
|
|
|
decodeEntities: true,
|
|
|
|
keepClosingSlash: true,
|
|
|
|
minifyCSS: true,
|
|
|
|
quoteCharacter: `"`,
|
|
|
|
removeComments: true,
|
|
|
|
removeAttributeQuotes: true,
|
|
|
|
removeRedundantAttributes: true,
|
|
|
|
removeScriptTypeAttributes: true,
|
|
|
|
removeStyleLinkTypeAttributes: true,
|
|
|
|
sortAttributes: true,
|
|
|
|
sortClassName: true,
|
|
|
|
useShortDoctype: true
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log(`MINIFY ${output_path}`, source.length, `→`, result.length, `(${((1 - (result.length / source.length)) * 100).toFixed(2)}% reduction)`);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_production) console.log("Production environment detected, minifying content");
|
|
|
|
|
2021-05-31 03:20:23 +02:00
|
|
|
module.exports = function(eleventyConfig) {
|
2022-07-06 01:28:55 +02:00
|
|
|
eleventyConfig.addTransform("cssmin", do_minifycss);
|
|
|
|
eleventyConfig.addTransform("htmlmin", do_minifyhtml);
|
|
|
|
|
2021-12-28 23:13:38 +01:00
|
|
|
eleventyConfig.addPassthroughCopy("img2brush/img2brush.js");
|
2021-07-06 03:47:45 +02:00
|
|
|
eleventyConfig.addAsyncShortcode("fetch", fetch);
|
2022-07-06 02:41:27 +02:00
|
|
|
eleventyConfig.addFilter("fetch_file", fetch_file);
|
2021-07-06 03:47:45 +02:00
|
|
|
|
2021-05-31 22:06:17 +02:00
|
|
|
// eleventyConfig.addPassthroughCopy("images");
|
|
|
|
// eleventyConfig.addPassthroughCopy("css");
|
|
|
|
eleventyConfig.addShortcode("image", shortcode_image);
|
|
|
|
eleventyConfig.addJavaScriptFunction("image", shortcode_image);
|
2021-06-14 03:25:45 +02:00
|
|
|
// eleventyConfig.addNunjucksAsyncShortcode("image_url", shortcode_image_url);
|
|
|
|
eleventyConfig.addAsyncShortcode("image_url", shortcode_image_url);
|
2021-06-19 19:14:23 +02:00
|
|
|
eleventyConfig.addAsyncShortcode("image_urlpass", shortcode_image_urlpass);
|
2021-06-14 03:25:45 +02:00
|
|
|
eleventyConfig.addNunjucksAsyncShortcode("image_urlpass", shortcode_image_urlpass);
|
2021-06-09 01:34:25 +02:00
|
|
|
eleventyConfig.addPairedShortcode("gallerybox", shortcode_gallerybox);
|
2021-05-31 03:20:23 +02:00
|
|
|
}
|