import { HtmlBasePlugin, IdAttributePlugin } from "@11ty/eleventy";
import { feedPlugin } from "@11ty/eleventy-plugin-rss";

import pluginFilters from "./_config/filters.js";

export default async function(eleventyConfig) {
	/* Bundles */
	/* CSS */
	eleventyConfig.addBundle("css", {
		toFileDirectory: "dist",
		bundleHtmlContentFromSelector: "style",
	});

	/* Javascript */
	eleventyConfig.addBundle("js", {
		toFileDirectory: "dist",
		bundleHtmlContentFromSelector: "script",
	});

	/* Passthrough */
	eleventyConfig
		.addPassthroughCopy("favicon.ico")
		.addPassthroughCopy("robots.txt");

	/* Plugins */
	/* All filters from _config/filters.js */
	eleventyConfig.addPlugin(pluginFilters);

	/* Pathprefix stuff */
	eleventyConfig.addPlugin(HtmlBasePlugin);

	/* `id` attributes */
	eleventyConfig.addPlugin(IdAttributePlugin);

	/* RSS */
	eleventyConfig.addPlugin(feedPlugin, {
		type: "atom", // or "rss", "json"
		outputPath: "/feed.xml",
		collection: {
			name: "daily", // iterate over `collections.posts`
			limit: 10,     // 0 means no limit
		},
		metadata: {
			language: "en",
			title: "Fediverse Skillshare Classifieds",
			subtitle: "Skillshare listings for the Fediverse",
			base: "https://inherentlee.codeberg.page/",
			author: {
				name: "Lee Cattarin"
			}
		}
	});

	/* Draft handling */
	eleventyConfig.addPreprocessor("drafts", "*", (data, content) => {
		if(data.draft) {
			return false;
		}
	});

	/* Shortcodes */
	eleventyConfig.addShortcode("currentBuildDate", () => {
		return (new Date()).toISOString();
	});

	/* Watch when serving */
	eleventyConfig.addWatchTarget("css");

};

export const config = {
  dir: {
    input: "src",
		includes: "../_includes",
		layouts: "../_includes/layouts",
		data: "../_data"
  },
	markdownTemplateEngine: "njk",
	htmlTemplateEngine: "njk",
	templateFormats: ["html", "md", "njk"],
};
