-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy patheleventy.config.js
More file actions
155 lines (121 loc) · 4.3 KB
/
Copy patheleventy.config.js
File metadata and controls
155 lines (121 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
152
153
154
155
import toml from '@iarna/toml';
import { minify } from 'html-minifier-next';
function getWebsiteDomain(url) {
return url.replace(
/^https?:\/\/(www\.)?([^/]+)\/?.*/,
(string, www, domain) => domain
);
}
// TODO: Add a tool to target duplicated domains
// TODO: Add finer sorting
export default function (eleventyConfig) {
eleventyConfig.setQuietMode(true);
eleventyConfig.setInputDirectory('_src');
// Host static assets—anything from `./public/` goes to site’s root `/`
eleventyConfig.addPassthroughCopy({ '_assets/public': '/' });
// Allow to parse Toml files for Global data
eleventyConfig.addDataExtension('toml', (contents) => toml.parse(contents));
// Filters
eleventyConfig.addFilter('getSiteTitle', function(website) {
return website.title ?? getWebsiteDomain(website.url);
});
// Global data filters
eleventyConfig.addFilter('getParticipantDisplayName', function(filename) {
const participant = this.ctx.participants[filename];
return participant.display ?? filename;
});
eleventyConfig.addFilter('getParticipantWebsitesForYear', function(participant, year) {
return participant.websites.filter(website => website.years.includes(year));
});
eleventyConfig.addFilter('mergeNameWithURL', function(participant, websitesForYear) {
// Is there only one website for the current year?
if (websitesForYear.length > 1) {
return false;
}
// Is there at least one `homeURL` in the user file
const hasHomeURL = participant.websites.some(website => website.homeURL !== undefined);
if (!hasHomeURL) {
return true;
}
return websitesForYear[0].homeURL;
});
// eleventyComputed data filters
/**
* Get all participants from the eleventyComputed data.
* See {@link https://www.11ty.dev/docs/data-computed/|eleventyComputed}.
*
* @param {array} participations - {year, filename}[]
* @param {number} year - target year
* @return {array} year’s participations - {year, filename}[]
*/
eleventyConfig.addFilter('getParticipantsForYear', function(participations, year) {
return participations
.filter(participation => participation.year === year)
.sort((a, b) => a.participant > b.participant ? 1 : -1);
});
/**
* Get all websites for the requested year.
*
* @param {string) year - target year
* @return {array} URLs for websites participating for this year
*/
eleventyConfig.addFilter('getWebsitesForYear', function(year) {
const participants = this.ctx.participants;
const websitesForYear = [];
for (const participant in participants) {
participants[participant].websites.forEach(website => {
if(website.years.includes(year)) {
websitesForYear.push(website.url);
}
});
};
return websitesForYear;
});
// Shortcodes
/**
* Display the URL of the website without a link if it is marked as spam.
*
* @param {string} title - Anchor of the link
* @param {Object} website - {url, [spam]}
* @return {string} - Formatted link or URL as string
*/
eleventyConfig.addShortcode('linkNoSpam', function(title, website) {
return website.spam ? website.url : `<a href="${website.url}">${title}</a>`;
});
/**
* Format websites as _website 1, website 2 & website 3_ by default.
* Use separator, prefix or suffix properties otherwise.
*
* @param {array} websites - {url, [title], [prefix], [suffix], [separator], years[]}[]
* @return {string} - Formatted string of links
*/
eleventyConfig.addShortcode('formatWebsitesForYear', function(websites) {
let output = '';
for(const website of websites) {
const title = eleventyConfig.getFilter('getSiteTitle')(website);
const link = eleventyConfig.getShortcode('linkNoSpam')(title, website);;
const prefix = website.prefix ?? '';
const suffix = website.suffix ?? '';
let loopIndex = websites.indexOf(website);
let separator = '';
if (loopIndex > 0 && website.separator === undefined) {
separator = loopIndex > 1 ? ' & ' : ', ';
}
output += `${separator}${prefix}${link}${suffix}`;
}
return output;
});
// HTML minification
eleventyConfig.addTransform('htmlmin', function(content) {
if (this.page.outputPath && this.page.outputPath.endsWith('.html')) {
let minified = minify(content, {
preset: 'comprehensive',
});
return minified;
}
return content;
});
return {
htmlTemplateEngine: 'njk',
};
};