-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
373 lines (319 loc) · 11.3 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
const { DateTime } = require("luxon");
const pluginRss = require("@11ty/eleventy-plugin-rss");
require('dotenv').config();
module.exports = function(eleventyConfig) {
eleventyConfig.addPlugin(pluginRss);
// Add a custom date filter
eleventyConfig.addFilter("date", (dateObj, format = "yyyy-MM-dd") => {
return DateTime.fromISO(dateObj).toFormat(format);
});
// Add a custom filter to filter blog posts by tag
eleventyConfig.addFilter("filterByTag", (posts, tag) => {
return posts.filter(post => {
return post && post.data && Array.isArray(post.data.tags) && post.data.tags.includes(tag);
});
});
eleventyConfig.addFilter("intersect", function(array1, array2) {
return array1.filter(value => array2.includes(value));
});
// Add a collection for blog posts
eleventyConfig.addCollection("blog", function(collectionApi) {
return collectionApi.getFilteredByGlob("src/posts/*.md").sort((a, b) => {
return b.date - a.date;
});
});
eleventyConfig.addFilter("rss_date", (dateObj) => {
return DateTime.fromISO(dateObj).toRFC2822();
});
// eleventyConfig.addCollection("rss", function(collectionApi) {
// return collectionApi.getFilteredByGlob("src/posts/*.md").sort((a, b) => {
// return b.date - a.date;
// });
// });
// eleventyConfig.addPassthroughCopy({ "src/rss-feed.njk": "rss.xml" })
// Add a collection for unique tags
eleventyConfig.addCollection("tagsList", function(collectionApi) {
let tags = new Set();
collectionApi.getAll().forEach(item => {
if("tags" in item.data) {
item.data.tags.forEach(tag => tags.add(tag));
}
});
return Array.from(tags);
});
// Add GitHub profile data
eleventyConfig.addGlobalData("githubProfile", async () => {
const fetch = (await import("node-fetch")).default;
const username = 'udaysinh-git';
const token = process.env.GITHUB_TOKEN;
if (!token) {
console.error("GITHUB_TOKEN is not defined in the environment variables.");
return {};
}
try {
const profileResponse = await fetch(`https://api.github.com/users/${username}`, {
headers: {
'Authorization': `token ${token}`
}
});
if (!profileResponse.ok) {
console.error("Error fetching GitHub profile:", profileResponse.statusText);
return {};
}
const profileData = await profileResponse.json();
const commitsResponse = await fetch(`https://api.github.com/search/commits?q=author:${username}`, {
headers: {
'Authorization': `token ${token}`,
'Accept': 'application/vnd.github.cloak-preview'
}
});
if (!commitsResponse.ok) {
console.error("Error fetching GitHub commits:", commitsResponse.statusText);
return {};
}
const commitsData = await commitsResponse.json();
return {
profile: profileData,
totalCommits: commitsData.total_count
};
} catch (error) {
console.error("Error fetching GitHub data:", error);
return {};
}
});
// Add pinnedRepos global data
eleventyConfig.addGlobalData("pinnedRepos", async () => {
const fetch = (await import("node-fetch")).default;
const username = 'udaysinh-git';
const token = process.env.GITHUB_TOKEN;
if (!token) {
console.error("GITHUB_TOKEN is not defined in the environment variables.");
return [];
}
// Fetch pinned repositories using GitHub GraphQL API
const pinnedQuery = {
query: `
{
user(login: "${username}") {
pinnedItems(first: 6, types: REPOSITORY) {
edges {
node {
... on Repository {
name
description
url
createdAt
pushedAt
stargazerCount
forkCount
}
}
}
}
}
}
`
};
try {
const pinnedResponse = await fetch('https://api.github.com/graphql', {
method: 'POST',
headers: {
'Authorization': `bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(pinnedQuery)
});
if (!pinnedResponse.ok) {
console.error("Error fetching pinned repositories:", pinnedResponse.statusText);
return [];
}
const pinnedData = await pinnedResponse.json();
if (pinnedData.errors) {
console.error("GraphQL errors:", pinnedData.errors);
return [];
}
const pinnedRepos = pinnedData.data.user.pinnedItems.edges.map(edge => {
const repo = edge.node;
return {
name: repo.name,
description: repo.description,
html_url: repo.url, // Map 'url' to 'html_url'
created_at: repo.createdAt, // Map 'createdAt' to 'created_at'
pushed_at: repo.pushedAt, // New field
stargazers_count: repo.stargazerCount, // Map 'stargazerCount' to 'stargazers_count'
forks_count: repo.forkCount // Map 'forkCount' to 'forks_count'
};
});
return pinnedRepos;
} catch (error) {
console.error("Error fetching pinned repositories:", error);
return [];
}
});
// Add latestRepos global data
eleventyConfig.addGlobalData("latestRepos", async () => {
const fetch = (await import("node-fetch")).default;
const username = 'udaysinh-git';
const token = process.env.GITHUB_TOKEN;
if (!token) {
console.error("GITHUB_TOKEN is not defined in the environment variables.");
return [];
}
// Fetch all repositories
try {
const reposResponse = await fetch(`https://api.github.com/users/${username}/repos?sort=created&direction=desc`, {
headers: {
'Authorization': `token ${token}`
}
});
if (!reposResponse.ok) {
console.error("Error fetching repositories:", reposResponse.statusText);
return [];
}
const repos = await reposResponse.json();
// Remove slicing to fetch all repositories
const latestRepos = repos.map(repo => ({
name: repo.name,
description: repo.description,
html_url: repo.html_url,
created_at: repo.created_at,
pushed_at: repo.pushed_at,
stargazers_count: repo.stargazers_count,
forks_count: repo.forks_count
}));
return latestRepos;
} catch (error) {
console.error("Error fetching latest repositories:", error);
return [];
}
});
// Add languageStats global data
eleventyConfig.addGlobalData("languageStats", async () => {
const fetch = (await import("node-fetch")).default;
const username = 'udaysinh-git';
const token = process.env.GITHUB_TOKEN;
if (!token) {
console.error("GITHUB_TOKEN is not defined in the environment variables.");
return {};
}
try {
const reposResponse = await fetch(`https://api.github.com/users/${username}/repos`, {
headers: {
'Authorization': `token ${token}`
}
});
if (!reposResponse.ok) {
console.error("Error fetching repositories:", reposResponse.statusText);
return {};
}
const repos = await reposResponse.json();
const languageCounts = {};
for (const repo of repos) {
const languagesResponse = await fetch(repo.languages_url, {
headers: {
'Authorization': `token ${token}`
}
});
if (!languagesResponse.ok) {
console.error(`Error fetching languages for repo ${repo.name}:`, languagesResponse.statusText);
continue;
}
const languages = await languagesResponse.json();
for (const [language, count] of Object.entries(languages)) {
languageCounts[language] = (languageCounts[language] || 0) + count;
}
}
return languageCounts;
} catch (error) {
console.error("Error fetching language data:", error);
return {};
}
});
// Add contributionsData global data
eleventyConfig.addGlobalData("contributionsData", async () => {
const fetch = (await import("node-fetch")).default;
const { DateTime } = require("luxon");
const username = 'udaysinh-git';
const token = process.env.GITHUB_TOKEN;
if (!token) {
console.error("GITHUB_TOKEN is not defined in the environment variables.");
return { labels: [], data: [] };
}
const currentYear = DateTime.now().year;
// Last 4 complete years + current year
const years = Array.from({ length: 5 }, (_, i) => currentYear - i);
const contributionsPerYear = {};
try {
const queries = years.map(year => {
const from = DateTime.fromObject({ year, month: 1, day: 1 }).toISO();
const to = year === currentYear
? DateTime.now().toISO()
: DateTime.fromObject({ year, month: 12, day: 31 }).toISO();
const query = `
{
user(login: "${username}") {
contributionsCollection(from: "${from}", to: "${to}") {
contributionCalendar {
totalContributions
}
}
}
}
`;
return fetch('https://api.github.com/graphql', {
method: 'POST',
headers: {
'Authorization': `bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ query })
})
.then(response => {
if (!response.ok) {
throw new Error(`Failed to fetch contributions for ${year}: ${response.statusText}`);
}
return response.json();
})
.then(data => {
if (data.errors) {
throw new Error(`GraphQL errors for ${year}: ${JSON.stringify(data.errors)}`);
}
const total = data.data.user.contributionsCollection.contributionCalendar.totalContributions;
contributionsPerYear[year] = total;
});
});
await Promise.all(queries);
// Reverse the years array once for chronological order
const reversedYears = [...years].reverse();
const labels = reversedYears.map(year => year.toString());
const commits = reversedYears.map(year => contributionsPerYear[year] || 0);
return { labels, data: commits };
} catch (error) {
console.error("Error fetching contributions data:", error);
return { labels: [], data: [] };
}
});
// Add current year to global data
eleventyConfig.addGlobalData("currentYear", () => {
return new Date().getFullYear();
});
// Passthrough copy for CSS
eleventyConfig.addPassthroughCopy("src/styles/base.css");
eleventyConfig.addPassthroughCopy("src/styles/footer.css");
eleventyConfig.addPassthroughCopy("src/styles/header.css");
eleventyConfig.addPassthroughCopy("src/styles/main.css");
eleventyConfig.addPassthroughCopy("src/styles/themes.css");
eleventyConfig.addPassthroughCopy("src/styles/blogs.css");
eleventyConfig.addPassthroughCopy("src/styles/contact.css");
eleventyConfig.addPassthroughCopy("src/styles/styles.css");
eleventyConfig.addPassthroughCopy("src/styles");
eleventyConfig.addPassthroughCopy("src/scripts");
return {
dir: {
input: "src",
output: "_site",
includes: "_includes",
data: "_data"
}
};
};