Files
11ty_neocities/.eleventy.js

174 lines
5.4 KiB
JavaScript
Raw Permalink Normal View History

const htmlmin = require("html-minifier");
const rimraf = require("rimraf");
2023-03-02 15:56:23 +10:30
const cleancss = require("clean-css");
2023-02-21 00:02:35 +10:30
module.exports = function (eleventyConfig) {
2023-03-09 13:09:01 +10:30
// delete contents of public to ensure removed files are removed from the final build
rimraf.windows.sync("public/")
2023-03-02 15:56:23 +10:30
eleventyConfig.addPassthroughCopy("./src/_assets/css");
eleventyConfig.addPassthroughCopy("./src/_assets/img");
eleventyConfig.addPassthroughCopy("./src/_assets/fonts");
eleventyConfig.addPassthroughCopy("./src/_assets/js");
2023-03-02 15:56:23 +10:30
2023-10-06 18:09:58 +10:30
eleventyConfig.addShortcode("albumtile", function (title, embedLink, coverImage) {
2023-10-11 20:36:57 +10:30
var slug = slugify(title);
2023-10-06 18:09:58 +10:30
return `<div>
2023-10-11 20:36:57 +10:30
<a class="hide" href="${embedLink}" target="${slug}">
<img class="album-tile-cover-image" src="${coverImage}">
2023-10-06 18:09:58 +10:30
</a>
<iframe class="album-tile-iframe" name="${slug}" src="about:blank" seamless></iframe>
<b>${title}</b>
</div>`
});
2023-10-17 16:45:02 +10:30
eleventyConfig.addShortcode("listentry", function (title, link, image, video, iframelink, description) {
if (!title || title == "") {
return '';
};
2023-10-11 20:20:17 +10:30
var imageString = "";
var linkString = "";
var videoString = "";
var iframeString = "";
2023-10-11 20:36:57 +10:30
var slug = slugify(title);
2023-10-11 18:56:56 +10:30
2023-10-11 20:20:17 +10:30
if (Array.isArray(link)) {
if (typeof link[0] === 'string') {
linkString = link.map((l) => `<a href="${l}">link</a><br>`).join(" // ") + '<br>';
} else {
linkString = link.map((l) => `<a href="${l.link}">${l.title}</a>`).join(" // ") + '<br>';
}
} else if (typeof link === 'string') {
linkString = `<a href="${link}">link</a><br>`;
}
if (Array.isArray(image)) {
imageString = image.map((i, index) => `<a href="#img_${slug}_${index}"><img src="${i}"/></a>
<a href="#_${slug}_${index}" class="lightbox trans" id="img_${slug}_${index}"><img src="${i}"/></a><br>`).join(" ");
} else if (typeof image === 'string') {
imageString = `<a href="#img_${slug}"><img src="${image}"/></a>
<a href="#_${slug}" class="lightbox trans" id="img_${slug}"><img src="${image}"/></a><br>`
}
2023-10-11 20:36:57 +10:30
2023-10-11 20:20:17 +10:30
if (video) {
videoString = `<video autoplay loop muted controls poster="${video.poster}">
<source src="${video.link}" type="video/mp4"></source>
<img src="${video.poster}"></img>
2023-10-11 20:36:57 +10:30
</video><br>`
2023-10-11 20:20:17 +10:30
}
if (iframelink) {
console.log(iframelink)
iframeString = `
2023-10-17 16:45:02 +10:30
<iframe src="${iframelink}" style="width: 560px; aspect-ratio: 16/9; max-width: 100%;" seamless allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe><br>`;
}
2023-10-11 20:36:57 +10:30
return `<p>
<h3 id="${slug}">${title}</h3>
2023-10-11 20:20:17 +10:30
${linkString}
${imageString}
${videoString}
${iframeString}
2023-10-11 20:20:17 +10:30
${description}
</p>`
});
2023-10-11 20:36:57 +10:30
2023-03-09 13:09:01 +10:30
// make a list of all tags besides "post" and add them to the collection
eleventyConfig.addCollection("tagsList", function (collectionApi) {
const tagsList = new Set();
collectionApi.getAll().map(item => {
if (item.data.tags) { // handle pages that don't have tags
2023-03-04 01:39:16 +10:30
item.data.tags.map(tag => {
2023-03-28 15:20:36 +10:30
if (tag != "post") {
2023-03-04 01:39:16 +10:30
tagsList.add(tag)
}
})
}
});
2023-05-01 16:44:40 +09:30
const sortedTagsList = new Set(Array.from(tagsList).sort());
return sortedTagsList;
});
2023-10-11 20:36:57 +10:30
2023-03-28 15:37:11 +10:30
// limit filter
eleventyConfig.addFilter("limit", function (array, limit) {
return array.slice(0, limit);
});
2023-10-11 20:36:57 +10:30
2023-03-09 13:09:01 +10:30
// minify all html files
eleventyConfig.addTransform("htmlmin", function (content) {
if (this.page.outputPath && this.page.outputPath.endsWith(".html")) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true
});
return minified;
}
return content;
});
2023-03-09 13:09:01 +10:30
// clean and inline all css files
2023-03-28 15:20:36 +10:30
eleventyConfig.addFilter("cssmin", function (code) {
2023-03-02 15:56:23 +10:30
return new cleancss({}).minify(code).styles;
});
2023-03-28 15:20:36 +10:30
2023-03-09 13:09:01 +10:30
// // the below three configs allow for excluding files from builds using draft: true
// // https://www.11ty.dev/docs/quicktips/draft-posts/
// When `permalink` is false, the file is not written to disk
2023-03-28 15:20:36 +10:30
eleventyConfig.addGlobalData("eleventyComputed.permalink", function () {
return (data) => {
// Always skip during non-watch/serve builds
if (data.draft && !process.env.BUILD_DRAFTS) {
return false;
}
2023-03-09 13:09:01 +10:30
2023-03-28 15:20:36 +10:30
return data.permalink;
}
});
2023-10-11 20:36:57 +10:30
2023-03-09 13:09:01 +10:30
// When `eleventyExcludeFromCollections` is true, the file is not included in any collections
2023-03-28 15:20:36 +10:30
eleventyConfig.addGlobalData("eleventyComputed.eleventyExcludeFromCollections", function () {
return (data) => {
// Always exclude from non-watch/serve builds
if (data.draft && !process.env.BUILD_DRAFTS) {
return true;
}
2023-03-09 13:09:01 +10:30
2023-03-28 15:20:36 +10:30
return data.eleventyExcludeFromCollections;
}
});
2023-10-11 20:36:57 +10:30
2023-03-28 15:20:36 +10:30
eleventyConfig.on("eleventy.before", ({ runMode }) => {
// Set the environment variable
if (runMode === "serve" || runMode === "watch") {
process.env.BUILD_DRAFTS = true;
}
});
2023-03-02 15:56:23 +10:30
2023-02-21 00:02:35 +10:30
return {
passthroughFileCopy: true,
markdownTemplateEngine: "njk",
2023-02-21 00:02:35 +10:30
dir: {
input: "src",
output: "public",
includes: "_includes",
},
};
2023-10-11 20:36:57 +10:30
};
var slugify = function (preSlug) {
return preSlug
.toLowerCase()
.trim()
// remove accents
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
// replace invalid characters with spaces
.replace(/[^a-z0-9\s-]/g, ' ')
.trim()
// replace multiple spaces or hyphens with a hyphen
.replace(/[\s-]+/g, '-');
}