-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheleventy.config.js
More file actions
151 lines (132 loc) · 4.3 KB
/
Copy patheleventy.config.js
File metadata and controls
151 lines (132 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import path from "node:path";
import postcss from "postcss";
import autoprefixer from "autoprefixer";
import postcssImport from "postcss-import";
import cssnano from "cssnano";
import syntaxHighlighting from "@11ty/eleventy-plugin-syntaxhighlight";
import { feedPlugin } from "@11ty/eleventy-plugin-rss";
import markdownIt from "markdown-it";
import markdownItAnchor from "markdown-it-anchor";
import pluginTOC from "eleventy-plugin-toc";
import Image, { eleventyImageTransformPlugin } from "@11ty/eleventy-img";
export default function (eleventyConfig) {
const isDev = process.env.ELEVENTY_RUN_MODE === "serve";
// Set directories
eleventyConfig.setInputDirectory("src");
eleventyConfig.setOutputDirectory("dist");
eleventyConfig.setIncludesDirectory("_includes");
eleventyConfig.setLayoutsDirectory("_layouts");
eleventyConfig.ignores.add("src/_styles/**");
// Add public folder
if (isDev) {
// Allow all files for development and cms
eleventyConfig.addPassthroughCopy("src/assets");
} else {
// Ignore images since they will be transformed
eleventyConfig.addPassthroughCopy("src/assets/**/*.!(png|jpg|jpeg)");
}
// Copy over any js files
eleventyConfig.addPassthroughCopy({
"src/**/*.js": ".",
});
// Add icons
eleventyConfig.addPassthroughCopy({ "src/favicon.ico": "favicon.ico" });
eleventyConfig.addPassthroughCopy({
"src/site.webmanifest": "site.webmanifest",
});
// Export sanitize css
eleventyConfig.addPassthroughCopy({
"node_modules/sanitize.css/sanitize.css": "sanitize.css",
});
// Replace default markdown parser
const markdownLibrary = markdownIt({
html: true,
breaks: true,
linkify: true,
}).use(markdownItAnchor);
eleventyConfig.setLibrary("md", markdownLibrary);
// Add postcss parsing
eleventyConfig.addTemplateFormats("css");
eleventyConfig.addExtension("css", {
outputFileExtension: "css",
compileOptions: {
cache: false,
},
compile: async (inputContent, inputPath) => {
try {
const result = await postcss([
postcssImport,
autoprefixer,
cssnano,
]).process(inputContent, {
from: inputPath,
});
return async () => result.css;
} catch (err) {
console.error("PostCSS error:", err);
}
},
});
// Shortcode for transforming open graph meta tags
eleventyConfig.addLiquidShortcode("ogImage", async function (src) {
if (!src) return "";
// Check if the image is a remote web URL
const isRemote = src.startsWith("http://") || src.startsWith("https://");
// If it is a local absolute path, prepend 'src'.
let targetSrc = src;
if (!isRemote && src.startsWith("/")) {
targetSrc = path.join("src", src);
}
let metadata = await Image(targetSrc, {
widths: [1200],
formats: ["jpeg"],
outputDir: "./dist/assets/images/optimized/",
urlPath: "/assets/images/optimized/",
});
return metadata.jpeg[metadata.jpeg.length - 1].url;
});
// Add read time calculation
eleventyConfig.addFilter("readTime", (content) => {
const wordsPerMinute = 200;
const textOnly = content.replace(/(<([^>]+)>)/gi, "");
const wordCount = textOnly.split(/\s+/).length;
const minutes = Math.ceil(wordCount / wordsPerMinute);
return minutes === 1 ? "1 min read" : `${minutes} min read`;
});
// Add plugins
eleventyConfig.addPlugin(syntaxHighlighting);
eleventyConfig.addPlugin(eleventyImageTransformPlugin, {
extensions: "html",
formats: ["avif", "webp", "jpeg"],
widths: [400, 800, 1600, 2400],
urlPath: "/assets/images/optimized/",
outputDir: "./dist/assets/images/optimized/",
defaultAttributes: {
loading: "lazy",
decoding: "async",
sizes: "(max-width: 800px) 100vw, 800px",
},
});
eleventyConfig.addPlugin(feedPlugin, {
outputPath: "/thoughts/feed.xml",
collection: {
name: "thoughts",
limit: 10,
},
metadata: {
language: "en",
title: "Thinking of thoughts",
subtitle: "Writing about whatever crosses my mind at the moment.",
base: "https://mkf.dev/",
author: {
name: "MorganF",
email: "morgan@mkf.dev",
},
},
});
eleventyConfig.addPlugin(pluginTOC, {
tags: ["h2", "h3"],
wrapper: "nav",
wrapperClass: "post-toc",
});
}