import markdownIt from "markdown-it";
import markdownItAnchor from "markdown-it-anchor";
import { format } from "date-fns";
import pluginInlineLinkFavicon from "eleventy-plugin-inline-link-favicon"

export default async function (eleventyConfig) {

  const markdownItOptions = {
    html: true
  };

  const markdownItAnchorOptions = {
    permalink: true, permalinkBefore: true, permalinkSymbol: '#'
  };

  const markdownLib = markdownIt(markdownItOptions).use(
    markdownItAnchor,
    markdownItAnchorOptions
  );

  eleventyConfig.setLibrary("md", markdownLib);

  const assets = "./_src/assets/";
  eleventyConfig.addPassthroughCopy(assets);
  eleventyConfig.addWatchTarget(assets);

  eleventyConfig.addPlugin(pluginInlineLinkFavicon);

  eleventyConfig.addFilter("firstSegment", url => (typeof url === 'string' ? url.split('/')[1] || '' : ''));

  eleventyConfig.addFilter("sortByFirstDate", items => items.sort((a, b) => new Date(b.data.images[0].date) - new Date(a.data.images[0].date)));

  eleventyConfig.addFilter("ISO", (dateObj) => {
    return format(dateObj, ("yyyy-LL-dd"));
  });
  eleventyConfig.addFilter("readable", (dateObj) => {
    return format(dateObj, ("LLLL d, yyyy"));
  });

  eleventyConfig.setBrowserSyncConfig({
    open: true,
  });
};

export const config = {
  dir: {
    input: "_src",
    output: "_site"
  }
};

