portfolio/.eleventy.js
2024-03-27 00:43:45 -05:00

81 lines
2.7 KiB
JavaScript

const Nunjucks = require('nunjucks');
const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone')
const localizedFormat = require('dayjs/plugin/localizedFormat')
const hljs = require('highlight.js/lib/common');
const { setup } = require('highlightjs-glimmer');
const markdownit = require('markdown-it');
const markdownitClass = require('@toycode/markdown-it-class');
const cheerio = require('cheerio');
const expectedInputFormat = 'YYYY-MM-DD';
const defaultOuputFormat = 'LL';
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(localizedFormat);
function dayjsFilter(date, format = defaultOuputFormat) {
return dayjs(date, expectedInputFormat).utc().format(format);
}
const markdownClassMapping = {
h1: ['text-6xl', 'mt-6', 'text-pink'],
h2: ['text-3xl', 'mt-6', 'text-text'],
h3: ['text-2xl', 'mt-4', 'text-text'],
h4: ['text-xl', 'mt-2', 'text-text'],
h5: ['text-lg', 'mt-2', 'text-text'],
h6: ['mt-2', 'text-text'],
blockquote: ['border-l-2', 'border-lavender', 'px-4', 'pt-4', 'pb-px', 'my-8', 'bg-mantle'],
a: ['link'],
s: ['text-red'],
p: ['mb-4', 'text-text', 'leading-7'],
strong: ['font-normal', 'text-peach', 'dark:text-yellow'],
em: ['italic', 'text-green'],
img: ['max-w-full', 'rounded-lg', 'max-h-600px', 'contrast-more:contrast-50'], // (contrast-more-50 here undoes the content contrast boost elsewhere)
video: ['max-w-full', 'rounded-lg', 'max-h-600px', 'contrast-more:contrast-50'], // (contrast-more-50 here undoes the content contrast boost elsewhere)
ul: ['list-disc', 'ml-4', 'text-subtext0'],
li: ['text-subtext0'],
};
setup(hljs);
const md = markdownit({
linkify: true,
html: true,
highlight: function(str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(str, { language: lang }).value;
} catch (__) {}
}
return ''; // use external default escaping
}
});
md.use(markdownitClass, markdownClassMapping);
module.exports = function(eleventyConfig) {
let nunjucksEnvironment = new Nunjucks.Environment(
new Nunjucks.FileSystemLoader("_includes")
);
eleventyConfig.setLibrary('md', md);
eleventyConfig.addPassthroughCopy('img');
eleventyConfig.addPassthroughCopy('files');
eleventyConfig.addCollection(
"pinned",
collection => collection.getAllSorted().filter(item => item.data.pinned),
);
eleventyConfig.addFilter('markdown', value => md.render(value));
eleventyConfig.addFilter('markdownToPlaintext', value => {
const htmlString = md.render(value);
const parser = cheerio.load(htmlString);
return parser.text();
});
eleventyConfig.addNunjucksFilter('date', dayjsFilter);
eleventyConfig.setLibrary('njk', nunjucksEnvironment);
};