2021-10-17 02:00:24 +00:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-01 23:21:05 +00:00
|
|
|
import os from "os";
|
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
2021-05-31 20:06:17 +00:00
|
|
|
|
2024-10-01 23:21:05 +00:00
|
|
|
import debug from "debug";
|
|
|
|
import htmlentities from "html-entities";
|
|
|
|
import phin from "phin";
|
|
|
|
import CleanCSS from "clean-css";
|
|
|
|
import { minify as minify_html } from "html-minifier-terser";
|
2021-07-06 01:47:45 +00:00
|
|
|
|
2024-10-01 23:55:24 +00:00
|
|
|
import moondoc_runner from "./lib/moondoc_runner.mjs";
|
|
|
|
import HTMLPicture from "./lib/HTMLPicture.mjs";
|
|
|
|
import FileFetcher from "./lib/FileFetcher.mjs";
|
2022-07-06 00:41:27 +00:00
|
|
|
const file_fetcher = new FileFetcher();
|
2021-07-06 01:47:45 +00:00
|
|
|
|
2024-10-01 23:21:05 +00:00
|
|
|
// HACK: Make sure __dirname is defined when using es6 modules. I forget where I found this - a PR with a source URL would be great!
|
|
|
|
const __dirname = import.meta.url.slice(7, import.meta.url.lastIndexOf("/"));
|
|
|
|
|
2022-07-05 23:28:55 +00:00
|
|
|
const is_production = typeof process.env.NODE_ENV === "string" && process.env.NODE_ENV === "production";
|
2022-07-06 00:41:27 +00:00
|
|
|
|
2021-05-31 20:06:17 +00:00
|
|
|
var nextid = 0;
|
|
|
|
|
2021-06-14 00:36:13 +00: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 02:00:24 +00:00
|
|
|
async function shortcode_image(src, alt) {
|
2021-05-31 20:06:17 +00:00
|
|
|
|
2021-10-17 02:00:24 +00:00
|
|
|
return HTMLPicture(
|
|
|
|
src, alt,
|
|
|
|
`./_site/img`, `/img`
|
|
|
|
);
|
2021-05-31 20:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function shortcode_image_url(src) {
|
2024-10-16 19:18:51 +00:00
|
|
|
const target_dir = `./_site/img`;
|
2021-10-17 02:00:24 +00:00
|
|
|
const src_parsed = path.parse(src);
|
2024-10-16 19:18:51 +00:00
|
|
|
const target = path.join(target_dir, src_parsed.base);
|
2021-10-17 02:00:24 +00:00
|
|
|
if(!fs.existsSync(path.dirname(target)))
|
|
|
|
await fs.promises.mkdir(target_dir, { recursive: true });
|
|
|
|
await fs.promises.copyFile(src, target);
|
2021-05-31 20:06:17 +00:00
|
|
|
|
2021-10-17 02:00:24 +00:00
|
|
|
return path.join(`/img`, src_parsed.base);
|
2021-05-31 20:06:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-14 01:25:45 +00:00
|
|
|
async function shortcode_image_urlpass(src) {
|
2024-10-02 00:03:09 +00:00
|
|
|
const target_dir = `./_site/img`;
|
2021-06-14 00:36:13 +00:00
|
|
|
if(!fs.existsSync(target_dir))
|
|
|
|
await fs.promises.mkdir(target_dir, { recursive: true });
|
2024-10-02 00:03:09 +00:00
|
|
|
const filename = path.basename(src);
|
2021-06-14 00:36:13 +00:00
|
|
|
// 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}`;
|
|
|
|
}
|
|
|
|
|
2023-12-26 01:30:19 +00:00
|
|
|
async function shortcode_gallerybox(content, src) {
|
|
|
|
return `<div class="keen-slider__slide"><figure class="gallery-item">
|
2021-06-08 23:34:25 +00:00
|
|
|
<!-- ${await shortcode_image(src, "", "gallerybox-thumb", "300w")} -->
|
|
|
|
${await shortcode_image(src, "", "", "1920w")}
|
|
|
|
|
|
|
|
<figcaption>${content}</figcaption>
|
2023-12-26 01:30:19 +00:00
|
|
|
</figure></div>`;
|
|
|
|
// <a class="gallerybox-prev" href="#${id_prev}">❰</a>
|
|
|
|
// <a class="gallerybox-next" href="#${id_next}">❱</a>
|
2021-05-31 20:06:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 01:47:45 +00:00
|
|
|
async function fetch(url) {
|
|
|
|
return (await phin({
|
|
|
|
url,
|
|
|
|
headers: {
|
2021-10-17 02:00:24 +00: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 01:47:45 +00:00
|
|
|
},
|
|
|
|
followRedirects: true,
|
|
|
|
parse: "string"
|
|
|
|
})).body;
|
|
|
|
}
|
|
|
|
|
2022-07-06 00:41:27 +00:00
|
|
|
function fetch_file(url) {
|
|
|
|
return file_fetcher.fetch_file(url);
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:02:23 +00:00
|
|
|
function do_minify_css(source, output_path) {
|
2022-07-05 23:28:55 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:02:23 +00:00
|
|
|
async function do_minify_html(source, output_path) {
|
2022-07-05 23:28:55 +00:00
|
|
|
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");
|
|
|
|
|
2024-10-01 23:21:05 +00:00
|
|
|
export default function config(eleventyConfig) {
|
2024-01-02 16:31:30 +00:00
|
|
|
moondoc_runner(
|
|
|
|
path.resolve(__dirname, "_site/api/index.html")
|
|
|
|
);
|
|
|
|
|
2022-09-25 22:02:23 +00:00
|
|
|
eleventyConfig.addTransform("cssmin", do_minify_css);
|
|
|
|
eleventyConfig.addTransform("htmlmin", do_minify_html);
|
2022-07-05 23:28:55 +00:00
|
|
|
|
2021-12-28 22:13:38 +00:00
|
|
|
eleventyConfig.addPassthroughCopy("img2brush/img2brush.js");
|
2021-07-06 01:47:45 +00:00
|
|
|
eleventyConfig.addAsyncShortcode("fetch", fetch);
|
2022-07-06 00:41:27 +00:00
|
|
|
eleventyConfig.addFilter("fetch_file", fetch_file);
|
2021-07-06 01:47:45 +00:00
|
|
|
|
2021-05-31 20:06:17 +00:00
|
|
|
// eleventyConfig.addPassthroughCopy("images");
|
|
|
|
// eleventyConfig.addPassthroughCopy("css");
|
|
|
|
eleventyConfig.addShortcode("image", shortcode_image);
|
|
|
|
eleventyConfig.addJavaScriptFunction("image", shortcode_image);
|
2021-06-14 01:25:45 +00:00
|
|
|
// eleventyConfig.addNunjucksAsyncShortcode("image_url", shortcode_image_url);
|
|
|
|
eleventyConfig.addAsyncShortcode("image_url", shortcode_image_url);
|
2021-06-19 17:14:23 +00:00
|
|
|
eleventyConfig.addAsyncShortcode("image_urlpass", shortcode_image_urlpass);
|
2021-06-14 01:25:45 +00:00
|
|
|
eleventyConfig.addNunjucksAsyncShortcode("image_urlpass", shortcode_image_urlpass);
|
2021-06-08 23:34:25 +00:00
|
|
|
eleventyConfig.addPairedShortcode("gallerybox", shortcode_gallerybox);
|
2023-12-26 01:30:19 +00:00
|
|
|
|
|
|
|
eleventyConfig.addPassthroughCopy({
|
2023-12-26 12:27:02 +00:00
|
|
|
"node_modules/keen-slider/keen-slider.es.js": "./keen-slider.es.js"
|
2023-12-26 01:30:19 +00:00
|
|
|
});
|
2024-01-02 16:31:30 +00:00
|
|
|
|
2024-10-01 23:21:05 +00:00
|
|
|
}
|