Repository navigation
Expand file tree
/
Copy path.eleventy.js
More file actions
197 lines (163 loc) · 6.46 KB
/
Copy path.eleventy.js
File metadata and controls
197 lines (163 loc) · 6.46 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const markdownIt = require("markdown-it");
const fs = require("fs");
const nodePath = require("path");
function normalizePathPrefix(rawPathPrefix = "/") {
if (!rawPathPrefix || rawPathPrefix === "/") {
return "/";
}
return `/${rawPathPrefix.replace(/^\/+|\/+$/g, "")}/`;
}
module.exports = function (eleventyConfig) {
const pathPrefix = normalizePathPrefix(process.env.ELEVENTY_PATH_PREFIX);
const pathPrefixWithoutTrailingSlash = pathPrefix === "/"
? "/"
: pathPrefix.slice(0, -1);
const pathPrefixKey = pathPrefix.replace(/^\/|\/$/g, "");
const ga4MeasurementId = (process.env.GA4_MEASUREMENT_ID || "").trim();
const md = markdownIt({
html: true,
breaks: false,
linkify: true
});
eleventyConfig.setLibrary("md", md);
eleventyConfig.addGlobalData("ga4MeasurementId", ga4MeasurementId);
eleventyConfig.addPassthroughCopy({ "assets/img": "img" });
eleventyConfig.addPassthroughCopy({ "assets/css": "css" });
eleventyConfig.addPassthroughCopy({ "assets/js": "js" });
eleventyConfig.addPassthroughCopy(".nojekyll");
eleventyConfig.addPassthroughCopy({ "assets/img/favicon.png": "favicon.png" });
eleventyConfig.addFilter("htmlDateString", (dateObj) => {
return new Date(dateObj).toISOString().split('T')[0];
});
eleventyConfig.addFilter("fileLastModified", function (inputPath, langCode) {
if (!inputPath) {
return null;
}
try {
const normalizedPath = String(inputPath).replace(/^\.\/+/, "");
const absolutePath = nodePath.resolve(process.cwd(), normalizedPath);
const stats = fs.statSync(absolutePath);
const locale = langCode === "uz" ? "uz-UZ" : "en-US";
return new Intl.DateTimeFormat(locale, {
year: "numeric",
month: "long",
day: "numeric"
}).format(stats.mtime);
} catch (_error) {
return null;
}
});
eleventyConfig.addFilter("urlencode", function (str) {
return encodeURIComponent(str);
});
eleventyConfig.addFilter("padStart", function (value, length, char) {
return String(value).padStart(length || 2, char || "0");
});
eleventyConfig.addFilter("collectionHasUrl", function (items, candidateUrl) {
if (!Array.isArray(items) || !candidateUrl) {
return false;
}
return items.some((item) => item && item.url === candidateUrl);
});
eleventyConfig.addFilter("displayTutorialTitle", function (value) {
if (typeof value !== "string") {
return value;
}
const original = value.trim();
if (!original) {
return value;
}
let title = original.replace(/\s+/g, " ");
const titleBeforeLeadingStrip = title;
// Remove common tutorial title wrappers that redundantly include "Python".
title = title.replace(/\s*\(\s*Python\s*\)\s*$/i, "");
title = title.replace(/\s+(?:to|in|with|for|on)\s+Python$/i, "");
title = title.replace(/\s+Python$/i, "");
title = title.replace(/^Python'(?:ga|ni|da|ning)\s+/i, "");
title = title.replace(/^Python(?:ga|ni|da|ning)\s+/i, "");
title = title.replace(/^Python\s+/i, "");
title = title.trim();
if (titleBeforeLeadingStrip !== title && /^[a-z]/.test(title)) {
title = title.charAt(0).toUpperCase() + title.slice(1);
}
return title || original;
});
eleventyConfig.addFilter("getTutorialNav", function (tutorials, currentUrl) {
if (!Array.isArray(tutorials) || !currentUrl) {
return { first: null, prev: null, next: null };
}
const currentIndex = tutorials.findIndex((item) => item.url === currentUrl);
const first = tutorials.length > 0 ? tutorials[0] : null;
const prev = currentIndex > 0 ? tutorials[currentIndex - 1] : null;
const next = currentIndex >= 0 && currentIndex < tutorials.length - 1
? tutorials[currentIndex + 1]
: null;
return { first, prev, next };
});
eleventyConfig.addCollection("tutorials", function (collectionApi) {
return collectionApi.getFilteredByGlob("content/en/tutorial/**/*.md").sort((a, b) => {
return (a.data.order || 0) - (b.data.order || 0);
});
});
eleventyConfig.addCollection("tutorialsUz", function (collectionApi) {
return collectionApi.getFilteredByGlob("content/uz/tutorial/**/*.md").sort((a, b) => {
return (a.data.order || 0) - (b.data.order || 0);
});
});
eleventyConfig.addCollection("dataStructures", function (collectionApi) {
return collectionApi.getFilteredByGlob("content/en/data-structures/**/*.md").sort((a, b) => {
return (a.data.order || 0) - (b.data.order || 0);
});
});
eleventyConfig.addCollection("dataStructuresUz", function (collectionApi) {
return collectionApi.getFilteredByGlob("content/uz/data-structures/**/*.md").sort((a, b) => {
return (a.data.order || 0) - (b.data.order || 0);
});
});
eleventyConfig.addCollection("algorithms", function (collectionApi) {
return collectionApi.getFilteredByGlob("content/en/algorithms/**/*.md").sort((a, b) => {
return (a.data.order || 0) - (b.data.order || 0);
});
});
eleventyConfig.addCollection("algorithmsUz", function (collectionApi) {
return collectionApi.getFilteredByGlob("content/uz/algorithms/**/*.md").sort((a, b) => {
return (a.data.order || 0) - (b.data.order || 0);
});
});
eleventyConfig.addCollection("tools", function (collectionApi) {
return collectionApi.getFilteredByGlob("tools/*.njk");
});
eleventyConfig.addWatchTarget("./assets/css/");
eleventyConfig.addWatchTarget("./assets/js/");
eleventyConfig.addWatchTarget("./tailwind.css");
eleventyConfig.setServerOptions({
watch: ["_site/css/**/*.css"],
liveReload: true,
});
eleventyConfig.addTransform("prefixRootRelativeUrls", function (content, outputPath) {
const currentOutputPath = outputPath || this?.page?.outputPath || this?.outputPath;
if (!currentOutputPath || !currentOutputPath.endsWith(".html") || pathPrefix === "/") {
return content;
}
return content.replace(/\b(href|src|poster)=("|')\/(?!\/)([^"']*)\2/g, (match, attr, quote, targetPath) => {
if (targetPath === pathPrefixKey || targetPath.startsWith(`${pathPrefixKey}/`)) {
return match;
}
return `${attr}=${quote}${pathPrefixWithoutTrailingSlash}/${targetPath}${quote}`;
});
});
return {
pathPrefix,
dir: {
input: ".",
output: "_site",
includes: "_includes",
layouts: "_layouts",
data: "_data"
},
templateFormats: ["md", "njk", "html"],
markdownTemplateEngine: "njk",
htmlTemplateEngine: "njk",
passthroughFileCopy: true
};
};