Skip to content

Commit 9057660

Browse files
committed
chore(#160): Simplify and document code a bit:
* Use smaller functions with a narrower scope. * Add comments to better explain functions.
1 parent 4f0b3fd commit 9057660

File tree

3 files changed

+61
-56
lines changed

3 files changed

+61
-56
lines changed

_src/_data/eleventyComputed.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default {
2424
for(const participation of data.participations) {
2525
const yearCount = participationsCount[participation.year];
2626

27-
participationsCount[participation.year] = yearCount
27+
participationsCount[participation.year] = yearCount > 0
2828
? participationsCount[participation.year] + 1
2929
: 1;
3030
};

_src/year.njk

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,15 @@
5050
<ol>
5151
{% for filename, participations in participations | getParticipantsForYear(year) | groupby('participant') %}
5252
{% set participant = participants[filename] %}
53-
{% set websites = participant.websites | getWebsitesForYear(year) %}
53+
{% set websites = participant | getParticipantWebsitesForYear(year) %}
5454

5555
<li>
5656
{%- if websites | length === 1 -%}
57-
{% linkNoSpam 'getParticipantDisplayName', websites[0].url, participant, year %}
57+
{% linkNoSpam filename | getParticipantDisplayName, websites[0] %}
5858
{%- else -%}
59-
{{ participant | getParticipantDisplayName }}:
60-
61-
{% for website in websites -%}
62-
{% linkNoSpam 'getSiteTitle', website.url, participant, year, loop.index0 %}
63-
{%- endfor %}
64-
{% endif %}
59+
{{- filename | getParticipantDisplayName }}:
60+
{% formatWebsitesForYear websites -%}
61+
{% endif -%}
6562
</li>
6663
{% endfor %}
6764
</ol>

eleventy.config.js

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,35 @@ export default function (eleventyConfig) {
2424
// Filters
2525
// ===============================================================================================
2626

27-
// Global data filters.
28-
// ---------------------------------------------------------------------------
29-
30-
eleventyConfig.addFilter('getParticipantDisplayName', (participant) => {
31-
// TODO: Either get current year’s website, or use the filename.
32-
const websiteURL = participant.websites[0].url;
33-
34-
return participant.display ?? participant.username ?? getWebsiteDomain(websiteURL);
27+
eleventyConfig.addFilter('getSiteTitle', function(website) {
28+
return website.title ?? getWebsiteDomain(website.url);
3529
});
3630

37-
eleventyConfig.addFilter('getSiteTitle', (url, participant) => {
38-
const website = participant.websites.find(website => website.url === url);
31+
// Global data filters.
32+
// ---------------------------------------------------------------------------
3933

40-
return website.title ?? getWebsiteDomain(website.url);
41-
});
34+
eleventyConfig.addFilter('getParticipantDisplayName', function(filename) {
35+
const participant = this.ctx.participants[filename];
4236

43-
// Return website matching url and year.
44-
// This allows to have different configurations for different years.
45-
// This is uSeful for prefix and suffix which might need different values depending on the year.
46-
eleventyConfig.addFilter('getSiteData', (url, participant) => {
47-
return participant.websites.find(website => website.url === url);
37+
return participant.display ?? participant.username ?? filename;
4838
});
4939

50-
eleventyConfig.addFilter('getWebsitesForYear', (websites, year) => {
51-
return websites.filter(website => website.years.includes(year));
40+
eleventyConfig.addFilter('getParticipantWebsitesForYear', function(participant, year) {
41+
return participant.websites.filter(website => website.years.includes(year));
5242
});
5343

5444
// eleventyComputed data filters.
5545
// ---------------------------------------------------------------------------
5646

57-
eleventyConfig.addFilter('getParticipantsForYear', (participations, year) => {
47+
/**
48+
* Get all participants from the eleventyComputed data.
49+
* See {@link https://www.11ty.dev/docs/data-computed/|eleventyComputed}.
50+
*
51+
* @param {array} participations - {year, filename}[]
52+
* @param {number} year - target year
53+
* @return {array} year’s participations - {year, filename}[]
54+
*/
55+
eleventyConfig.addFilter('getParticipantsForYear', function(participations, year) {
5856
return participations
5957
.filter(participation => participation.year === year)
6058
.sort((a, b) => a.participant > b.participant ? 1 : -1);
@@ -63,38 +61,48 @@ export default function (eleventyConfig) {
6361
// Shortcodes
6462
// ===============================================================================================
6563

66-
eleventyConfig.addShortcode('linkNoSpam', function(callback, url, participant, year, loopIndex0) {
67-
const website = eleventyConfig.getFilter('getSiteData')(url, participant, year);
68-
69-
if(!website) {
70-
return;
71-
};
72-
73-
let title, prefix, suffix, separator;
74-
75-
if (callback === 'getSiteTitle') {
76-
title = eleventyConfig.getFilter('getSiteTitle')(url, participant);
77-
prefix = website.prefix;
78-
suffix = website.suffix;
79-
} else {
80-
title = eleventyConfig.getFilter('getParticipantDisplayName')(participant);
81-
}
82-
83-
if (website.separator === undefined && loopIndex0) {
84-
separator = loopIndex0 > 1 ? ' & ' : ', ';
85-
}
86-
87-
if (!website.url) {
88-
return title;
89-
}
64+
/**
65+
* Display the URL of the website without a link if it is marked as spam.
66+
*
67+
* @param {string} title - Anchor of the link
68+
* @param {Object} website - {url, [spam]}
69+
* @return {string} - Formatted link or URL as string
70+
*/
71+
eleventyConfig.addShortcode('linkNoSpam', function(title, website) {
72+
return website.spam ? website.url : `<a href="${website.url}">${title}</a>`;
73+
});
9074

91-
if (website.spam) {
92-
return website.url;
75+
/**
76+
* Format websites as _website 1, website 2 & website 3_ by default.
77+
* Use separator, prefix or suffix properties otherwise.
78+
*
79+
* @param {array} websites - {url, [title], [prefix], [suffix], [separator], years[]}[]
80+
* @return {string} - Formatted string of links
81+
*/
82+
eleventyConfig.addShortcode('formatWebsitesForYear', function(websites) {
83+
let output = '';
84+
85+
for(const website of websites) {
86+
const title = eleventyConfig.getFilter('getSiteTitle')(website);
87+
const link = eleventyConfig.getShortcode('linkNoSpam')(title, website);;
88+
const prefix = website.prefix ?? '';
89+
const suffix = website.suffix ?? '';
90+
91+
let loopIndex = websites.indexOf(website);
92+
let separator = '';
93+
94+
if (loopIndex > 0 && website.separator === undefined) {
95+
separator = loopIndex > 1 ? ' & ' : ', ';
96+
}
97+
98+
output += `${separator}${prefix}${link}${suffix}`;
9399
}
94100

95-
return `${separator ?? ''}${prefix ?? ''}<a href="${website.url}">${title}</a>${suffix ?? ''}`
101+
return output;
96102
});
97103

104+
// ===============================================================================================
105+
98106
return {
99107
htmlTemplateEngine: 'njk',
100108
};

0 commit comments

Comments
 (0)