const path = require("path");
const markdownIt = require("markdown-it");
const slugify = require("@sindresorhus/slugify");
const markdownItAnchor = require("markdown-it-anchor");
const markdownItObsidian = require("markdown-it-obsidian");
const markdownItAttrs = require("markdown-it-attrs");
const markdownItEleventyImg = require("markdown-it-eleventy-img");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const embedYouTube = require("eleventy-plugin-youtube-embed");

module.exports = function (eleventyConfig) {
  eleventyConfig.addPlugin(embedYouTube);
  eleventyConfig.addPlugin(pluginRss);

  eleventyConfig.addPassthroughCopy({ "assets/css": "css" });
  eleventyConfig.addPassthroughCopy({ "assets/fonts": "fonts" });
  eleventyConfig.addPassthroughCopy({ "assets/images/*": "images" });
  eleventyConfig.addPassthroughCopy({ "assets/favicon": "/" });
  eleventyConfig.addPassthroughCopy({
    "node_modules/simpledotcss/simple.css": "css/simple.css",
  });

  eleventyConfig.addFilter("stringToDate", function (value) {
    return new Date(value);
  });
  eleventyConfig.addFilter("formatDatetimeForHumans", function (value) {
    const dt = new Date(value);
    const formattedDt = new Intl.DateTimeFormat("en", {
      dateStyle: "medium",
      timeStyle: "medium",
    }).format(dt);
    return formattedDt;
  });
  eleventyConfig.addFilter("formatDateForHumans", function (value) {
    const dt = new Date(value);
    const formattedDt = new Intl.DateTimeFormat("en", {
      dateStyle: "medium",
    }).format(dt);
    return formattedDt;
  });

  const markdownLibrary = markdownIt()
    .use(markdownItAnchor, { slugify })
    .use(
      markdownItObsidian({
        slugifyOnPageLinks: slugify,
      })
    )
    .use(markdownItAttrs)
    .use(markdownItEleventyImg, {
      imgOptions: {
        outputDir: path.join("dist", "img"),
      },
      globalAttributes: {
        loading: "lazy",
        decoding: "async",
      },
    });
  eleventyConfig.setLibrary("md", markdownLibrary);

  return {
    dir: {
      input: "src",
      output: "dist",
    },
  };
};
