Repository navigation
Expand file tree
/
Copy patheleventy.config.js
More file actions
151 lines (129 loc) Β· 5.18 KB
/
Copy patheleventy.config.js
File metadata and controls
151 lines (129 loc) Β· 5.18 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 markdownIt from "markdown-it";
import markdownItCjkBreaks from "markdown-it-cjk-breaks";
import markdownItImplicitFigures from "markdown-it-implicit-figures";
import markdownItFootnote from "markdown-it-footnote";
import markdownItAttrs from "markdown-it-attrs";
import { eleventyImageTransformPlugin } from "@11ty/eleventy-img";
import syntaxHighlight from "@11ty/eleventy-plugin-syntaxhighlight";
import pluginRss from "@11ty/eleventy-plugin-rss";
import fs from "fs";
import path from "path";
import postcss from "postcss";
import tailwindcss from "@tailwindcss/postcss";
import cssnano from "cssnano";
export default function (eleventyConfig) {
// --- Plugins ---
eleventyConfig.addPlugin(syntaxHighlight);
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(eleventyImageTransformPlugin, {
formats: ["webp", "auto"],
widths: ["auto"],
failOnError: false,
defaultAttributes: {
loading: "lazy",
decoding: "async",
},
});
// --- Markdown ---
const md = markdownIt({ html: true, linkify: true })
.use(markdownItCjkBreaks)
.use(markdownItImplicitFigures, { figcaption: true })
.use(markdownItFootnote)
.use(markdownItAttrs);
eleventyConfig.setLibrary("md", md);
// --- Shortcodes ---
eleventyConfig.addShortcode("youtube", (id) => {
return `<div class="aspect-video my-4"><iframe src="https://www.youtube-nocookie.com/embed/${id}" title="YouTube video" frameborder="0" loading="lazy" referrerpolicy="strict-origin-when-cross-origin" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen class="w-full h-full"></iframe></div>`;
});
eleventyConfig.addShortcode("tweet", (id, conversation) => {
const convoAttr = conversation === "none" ? ' data-conversation="none"' : "";
return `<blockquote class="twitter-tweet"${convoAttr}><a href="https://twitter.com/i/status/${id}">Tweet</a></blockquote>`;
});
eleventyConfig.addShortcode("telegram", (channel, msgid) => {
return `<script async src="https://telegram.org/js/telegram-widget.js?22" data-telegram-post="${channel}/${msgid}" data-width="100%"></script>`;
});
// --- Passthrough copy ---
eleventyConfig.addPassthroughCopy("src/assets/file");
eleventyConfig.addPassthroughCopy("src/assets/media");
eleventyConfig.addPassthroughCopy("src/static");
eleventyConfig.addPassthroughCopy({ "src/static/robots.txt": "robots.txt" });
// Keep informal notes separate from the regular post collection.
eleventyConfig.addCollection("ramblings", (collectionApi) => {
return collectionApi.getFilteredByGlob("src/ramblings/*.md");
});
// --- Tailwind CSS ---
eleventyConfig.on("eleventy.before", async () => {
const inputFile = path.resolve("src/css/main.css");
const outputFile = path.resolve("_site/css/main.css");
const cssInput = fs.readFileSync(inputFile, "utf8");
const result = await postcss([tailwindcss, cssnano]).process(cssInput, {
from: inputFile,
to: outputFile,
});
fs.mkdirSync(path.dirname(outputFile), { recursive: true });
fs.writeFileSync(outputFile, result.css);
});
// --- Date filters (use UTC to match frontmatter dates) ---
eleventyConfig.addFilter("readableDate", (dateObj) => {
const d = new Date(dateObj);
return d.toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
timeZone: "UTC",
});
});
eleventyConfig.addFilter("isoDate", (dateObj) => {
return new Date(dateObj).toISOString();
});
eleventyConfig.addFilter("currentYear", () => new Date().getFullYear());
eleventyConfig.addFilter("absoluteUrl", (url, base) => {
return new URL(url, base).href;
});
eleventyConfig.addFilter("newestUpdatedDate", (items = []) => {
if (!items.length) return new Date();
return new Date(
Math.max(
...items.map((item) => new Date(item.data.updated || item.date).getTime())
)
);
});
eleventyConfig.addFilter("dateToPath", (dateObj) => {
const d = new Date(dateObj);
const year = d.getUTCFullYear();
const month = String(d.getUTCMonth() + 1).padStart(2, "0");
const day = String(d.getUTCDate()).padStart(2, "0");
return `${year}/${month}/${day}`;
});
// --- Tag filters ---
eleventyConfig.addFilter("getTagList", (collections) => {
const tags = new Set();
for (const name in collections) {
if (name === "posts" || name === "ramblings" || name === "all") continue;
tags.add(name);
}
return [...tags].sort();
});
eleventyConfig.addFilter("tagSlug", (tag) => {
// Slugify for ASCII tags; keep raw Unicode for CJK tags
const ascii = tag.toLowerCase().replace(/[^\w\s-]/g, "").replace(/\s+/g, "-").trim();
return ascii || tag;
});
// --- Category slug filter ---
eleventyConfig.addFilter("categorySlug", (category) => {
const map = JSON.parse(
fs.readFileSync(path.resolve("src/_data/categories.json"), "utf8")
);
return map[category] || category.toLowerCase().replace(/\s+/g, "-");
});
return {
dir: {
input: "src",
output: "_site",
includes: "_includes",
data: "_data",
},
templateFormats: ["njk", "md"],
markdownTemplateEngine: "njk",
};
}