|
| 1 | +import { EleventyRenderPlugin } from "@11ty/eleventy"; |
| 2 | +import markdownIt from "markdown-it"; |
| 3 | +import markdownAnchor from "markdown-it-anchor"; |
| 4 | + |
| 5 | +import syntaxHighlight from "@11ty/eleventy-plugin-syntaxhighlight"; |
| 6 | +import { eleventyAlembic } from "@openlab/alembic/11ty.cjs"; |
| 7 | +import slugify from "slugify"; |
| 8 | + |
| 9 | +import pkg from "./package.json" with { type: "json" }; |
| 10 | +import site from "./src/_data/site.json" with { type: "json" }; |
| 11 | + |
| 12 | +// TODO: refactor this out when upgrading to eleventy@2 |
| 13 | +const md = markdownIt({ |
| 14 | + html: true, |
| 15 | +}); |
| 16 | +md.use(markdownAnchor, { |
| 17 | + slugify: (str) => slugify(str, { lower: true, strict: true }), |
| 18 | +}); |
| 19 | +md.disable("code"); |
| 20 | + |
| 21 | +// TODO: add watch/rebuild for src in development mode? |
| 22 | + |
| 23 | +/** @param {import('@11ty/eleventy/src/UserConfig')} eleventyConfig */ |
| 24 | +export default function (eleventyConfig) { |
| 25 | + eleventyConfig.setLibrary("md", md); |
| 26 | + |
| 27 | + eleventyConfig.addPlugin(EleventyRenderPlugin); |
| 28 | + eleventyConfig.addPlugin(eleventyAlembic); |
| 29 | + eleventyConfig.addPlugin(syntaxHighlight); |
| 30 | + |
| 31 | + eleventyConfig.addFilter("apiSort", (items) => { |
| 32 | + return Array.from(items).sort((a, b) => |
| 33 | + b.data.title.localeCompare(a.data.title), |
| 34 | + ); |
| 35 | + }); |
| 36 | + eleventyConfig.addFilter("fullUrl", (path) => { |
| 37 | + return new URL(path, site.url).href; |
| 38 | + }); |
| 39 | + eleventyConfig.addFilter("isCurrentPage", (pageUrl, currentUrl) => { |
| 40 | + return currentUrl.startsWith(pageUrl); |
| 41 | + }); |
| 42 | + eleventyConfig.addFilter("md", (content) => { |
| 43 | + // TODO: this seems to have 11ty's syntax added already |
| 44 | + return md.render(content); |
| 45 | + }); |
| 46 | + eleventyConfig.addFilter("json", (content) => |
| 47 | + JSON.stringify(content, null, 2), |
| 48 | + ); |
| 49 | + eleventyConfig.addFilter("getPages", (collection, tags = []) => { |
| 50 | + const set = new Set(tags); |
| 51 | + return collection |
| 52 | + .filter((item) => item.data.tags?.some((t) => set.has(t))) |
| 53 | + .sort((a, b) => a.data.title?.localeCompare(b.data.title)); |
| 54 | + }); |
| 55 | + eleventyConfig.addFilter("slug", (text) => slugify(text)); |
| 56 | + |
| 57 | + eleventyConfig.addShortcode("pkgVersion", () => pkg.version); |
| 58 | + eleventyConfig.addPairedShortcode( |
| 59 | + "error", |
| 60 | + (content) => `<p class="eleventyError">${content}</p>`, |
| 61 | + ); |
| 62 | + |
| 63 | + // eleventyConfig.addWatchTarget('./src/**/*.css') |
| 64 | + // eleventyConfig.addWatchTarget('./src/**/*.ts') |
| 65 | +} |
| 66 | + |
| 67 | +export const config = { |
| 68 | + markdownTemplateEngine: "njk", |
| 69 | + htmlTemplateEngine: "njk", |
| 70 | + templateFormats: ["html", "md"], |
| 71 | + dir: { |
| 72 | + input: "src", |
| 73 | + output: "dist", |
| 74 | + layouts: "_layouts", |
| 75 | + }, |
| 76 | +}; |
0 commit comments