-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
173 lines (144 loc) · 5.08 KB
/
.eleventy.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const markdownIt = require("markdown-it");
const htmlmin = require("html-minifier");
const { format } = require("date-fns");
const { parse } = require("node-html-parser");
const pl = require("date-fns/locale/pl");
const externalLinks = require("eleventy-plugin-external-links");
const siteConfig = require("./content/_data/siteConfig.json");
const orderedQuestionSlugs = require("./content/_data/orderedQuestionSlugs.json");
const Epub = require("epub-gen");
const MARKDOWN_OPTIONS = {
html: true,
xhtmlOut: true,
// linkify: true,
typographer: true,
breaks: true
};
module.exports = function (eleventyConfig) {
eleventyConfig.addLayoutAlias("base", "layouts/base.njk");
eleventyConfig.addLayoutAlias("full", "layouts/full.njk");
eleventyConfig.addLayoutAlias("question", "layouts/question.njk");
eleventyConfig.setLibrary("md", markdownIt(MARKDOWN_OPTIONS));
eleventyConfig.addFilter("toHTML", (str) => {
return new markdownIt(MARKDOWN_OPTIONS).renderInline(str);
});
eleventyConfig.addFilter("localizeLinks", (content) => {
const root = parse(content);
const links = root.querySelectorAll("a");
if (links) {
links.forEach((link) => {
if (!link.classList.contains("skip-localizing")) {
const href = link.getAttribute("href");
if (href && href.startsWith("/")) {
const newHref = `#${href.replace(/\//g, "")}`;
link.setAttribute("href", newHref);
}
}
});
// const newContent = root.toString();
return root.toString();
}
return content;
});
eleventyConfig.addFilter("filterHtmlForEpub", (content) => {
return content.replace(/"/g, '\\"').replace(/(\r\n|\n|\r)/gm, "");
});
eleventyConfig.addPlugin(externalLinks, {
name: "external-links", // Plugin name
regex: /^(([a-z]+:)(?!\/\/mastodon-poradnik.pl)|(\/\/))/i, // Regex that test if href is external
target: "_self", // 'target' attribute for external links
rel: "external noopener noreferrer", // 'rel' attribute for external links
extensions: [".html"], // Extensions to apply transform to
includeDoctype: true // Default to include '<!DOCTYPE html>' at the beginning of the file
});
eleventyConfig.setDataDeepMerge(true);
eleventyConfig.addShortcode("currentYear", () => {
return DateTime.local().toFormat("yyyy");
});
// Define passthrough for assets
eleventyConfig.addPassthroughCopy({
assets: "./"
});
eleventyConfig.addTransform("htmlmin", function (content, outputPath) {
if (outputPath && outputPath.endsWith(".html")) {
return htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true
});
}
return content;
});
// eleventyConfig.addTransform("localLinks", (content, outputPath) => {
// if (outputPath && outputPath.endsWith("czytaj/index.html")) {
// const root = parse(content);
// const links = root.querySelectorAll("a");
// links.forEach((link) => {
// if (!link.classList.contains("skip-localizing")) {
// const href = link.getAttribute("href");
// if (href && href.startsWith("/")) {
// const newHref = `#${href.replace(/\//g, "")}`;
// link.setAttribute("href", newHref);
// }
// }
// });
// const newContent = root.toString();
// return newContent;
// }
// return content;
// });
eleventyConfig.addCollection("questionDataObject", function (collectionApi) {
const data = {};
const targetCollection = collectionApi.getFilteredByTag("question");
targetCollection.forEach((el) => {
const slug = el.data.slug;
data[slug] = el;
});
return data;
});
eleventyConfig.addCollection("epubDataObject", function (collectionApi) {
const data = {};
const targetCollection = collectionApi.getFilteredByTag("epub-meta");
targetCollection.forEach((el) => {
const slug = el.data.slug;
data[slug] = el;
});
return data;
});
eleventyConfig.addCollection("questionDataArray", (collectionApi) => {
const data = [];
const targetCollection = collectionApi.getFilteredByTag("question");
orderedQuestionSlugs.forEach((questionSlug) => {
const element = targetCollection.find(
(item) => item.data.slug === questionSlug
);
data.push(element);
});
return data;
});
eleventyConfig.addFilter("findIndexBySlug", (collection = [], value) =>
collection.findIndex((item) => item.data.slug === value)
);
eleventyConfig.addFilter("readableDate", (date) =>
format(date, "d MMMM yyyy 'r.'", {
locale: pl
})
);
eleventyConfig.addFilter("getByIndex", (collection = [], value) => {
if (value < 0) return null;
return collection[value];
});
eleventyConfig.addFilter(
"alwaysProductionUrl",
(path) => new URL(path, siteConfig.translation.url)
);
eleventyConfig.addShortcode("currentYear", () =>
new Date().getFullYear().toString()
);
eleventyConfig.addPlugin(require("eleventy-plugin-emoji"));
return {
dir: {
input: "content"
}
};
};