Skip to content

Commit b4ef25a

Browse files
committed
A few random tweaks #2
1 parent d69332f commit b4ef25a

File tree

15 files changed

+200
-194
lines changed

15 files changed

+200
-194
lines changed

.eleventy.js

Lines changed: 81 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,111 @@
11

22
module.exports = function (eleventyConfig) {
33

4+
5+
const now = Date.now();
6+
const livePosts = (post) => post.date <= now;
7+
8+
eleventyConfig.addFilter("tagsList", (arr = []) => {
9+
const tagsSet = new Set();
10+
arr.forEach((item) => item.data.tags?.forEach((tag) => tagsSet.add(tag)));
11+
return [...tagsSet].sort((b, a) => b.localeCompare(a));
12+
});
13+
14+
eleventyConfig.addFilter("inspect", require("util").inspect);
15+
16+
17+
418
// SIGHTS - custom collections
5-
const nowsights = new Date();
6-
const sightslivePosts = (post) => post.date <= nowsights;
19+
720
eleventyConfig.addCollection("sights", (collection) => {
8-
return [
9-
...collection.getFilteredByGlob("./sights/*.njk").filter(sightslivePosts),
10-
].reverse();
21+
const posts = collection.getFilteredByGlob("./sights/*.njk");
22+
return [...posts].filter(livePosts).reverse();
1123
});
1224

1325
// SOUNDS - custom collections
14-
const nowsounds = new Date();
15-
const soundslivePosts = (post) => post.date <= nowsounds;
26+
1627
eleventyConfig.addCollection("sounds", (collection) => {
17-
return [
18-
...collection.getFilteredByGlob("./sounds/*.njk").filter(soundslivePosts),
19-
].reverse();
28+
const posts = collection.getFilteredByGlob("./sounds/*.njk");
29+
return [...posts].filter(livePosts).reverse();
2030
});
2131

2232
// generate a list of all tags collections
2333
// with alphabetical sorting - had to invert to "b, a" because it was sorting upside down
24-
eleventyConfig.addCollection('tagsList', (collectionApi) => {
25-
const tagsSet = new Set()
26-
collectionApi.getAll().forEach((item) => {
27-
if (!item.data.tags) return
28-
item.data.tags.forEach((tag) => tagsSet.add(tag))
29-
})
30-
return [...tagsSet].sort((b, a) => b.localeCompare(a))
34+
eleventyConfig.addCollection("sightsTagsList", (collection) => {
35+
const tagsListFilter = eleventyConfig.getFilter("tagsList");
36+
const items = collection
37+
.getFilteredByGlob("./sights/*.njk")
38+
.filter(livePosts);
39+
return tagsListFilter(items);
3140
});
3241

3342

34-
// https://www.11ty.dev/docs/copy/#manual-passthrough-file-copy-(faster)
35-
eleventyConfig.addPassthroughCopy('assets');
36-
eleventyConfig.addPassthroughCopy('sitemap.xml');
37-
eleventyConfig.addPassthroughCopy('robots.txt');
43+
eleventyConfig.addCollection("soundsTagsList", (collection) => {
44+
const tagsListFilter = eleventyConfig.getFilter("tagsList");
45+
const items = collection
46+
.getFilteredByGlob("./sounds/*.njk")
47+
.filter(livePosts);
48+
return tagsListFilter(items);
49+
});
3850

3951

40-
// format dates on SIGHTS and SOUNDS Articles
41-
const dateformat = require('./lib/filters/dateformat');
42-
eleventyConfig.addFilter('datefriendly', dateformat.friendly);
43-
eleventyConfig.addFilter('dateymd', dateformat.ymd);
52+
eleventyConfig.addFilter("head", (arr = [], idx = 0) => {
53+
if (idx < 0) {
54+
return arr.slice(idx);
55+
}
56+
return arr.slice(0, idx);
57+
});
4458

4559

46-
// "return" in order to keep the previously entered configuration values
47-
// taken from: https://github.com/11ty/eleventy-base-blog/blob/master/.eleventy.js
48-
return {
49-
// Control which files Eleventy will process
50-
// e.g.: *.md, *.njk, *.html, *.liquid
51-
templateFormats: [
52-
"md",
53-
"njk",
54-
"html",
55-
"liquid"
56-
],
60+
// https://www.11ty.dev/docs/copy/#manual-passthrough-file-copy-(faster)
61+
eleventyConfig.addPassthroughCopy("assets");
62+
eleventyConfig.addPassthroughCopy("sitemap.xml");
63+
eleventyConfig.addPassthroughCopy("robots.txt");
5764

58-
// added
59-
passthroughFileCopy: true,
65+
// format dates on SIGHTS and SOUNDS Articles
66+
const dateformat = require("./lib/filters/dateformat");
67+
eleventyConfig.addFilter("datefriendly", dateformat.friendly);
68+
eleventyConfig.addFilter("dateymd", dateformat.ymd);
6069

61-
// -----------------------------------------------------------------
62-
// If your site deploys to a subdirectory, change `pathPrefix`.
63-
// Don’t worry about leading and trailing slashes, we normalize these.
70+
// "return" in order to keep the previously entered configuration values
71+
// taken from: https://github.com/11ty/eleventy-base-blog/blob/master/.eleventy.js
72+
return {
73+
// Control which files Eleventy will process
74+
// e.g.: *.md, *.njk, *.html, *.liquid
75+
templateFormats: ["md", "njk", "html", "liquid"],
6476

65-
// If you don’t have a subdirectory, use "" or "/" (they do the same thing)
66-
// This is only used for link URLs (it does not affect your file structure)
67-
// Best paired with the `url` filter: https://www.11ty.dev/docs/filters/url/
77+
// added
78+
passthroughFileCopy: true,
6879

69-
// You can also pass this in on the command line using `--pathprefix`
80+
// -----------------------------------------------------------------
81+
// If your site deploys to a subdirectory, change `pathPrefix`.
82+
// Don’t worry about leading and trailing slashes, we normalize these.
7083

71-
// Optional (default is shown)
72-
pathPrefix: "/",
73-
// -----------------------------------------------------------------
84+
// If you don’t have a subdirectory, use "" or "/" (they do the same thing)
85+
// This is only used for link URLs (it does not affect your file structure)
86+
// Best paired with the `url` filter: https://www.11ty.dev/docs/filters/url/
7487

75-
// Pre-process *.md files with: (default: `liquid`)
76-
markdownTemplateEngine: "njk",
88+
// You can also pass this in on the command line using `--pathprefix`
7789

78-
// Pre-process *.html files with: (default: `liquid`)
79-
htmlTemplateEngine: "njk",
90+
// Optional (default is shown)
91+
pathPrefix: "/",
92+
// -----------------------------------------------------------------
8093

81-
// Opt-out of pre-processing global data JSON files: (default: `liquid`)
82-
dataTemplateEngine: false,
94+
// Pre-process *.md files with: (default: `liquid`)
95+
markdownTemplateEngine: "njk",
8396

84-
// These are all optional (defaults are shown):
85-
dir: {
86-
input: ".",
87-
includes: "_includes",
88-
data: "_data",
89-
output: "_site"
90-
}
91-
};
97+
// Pre-process *.html files with: (default: `liquid`)
98+
htmlTemplateEngine: "njk",
99+
100+
// Opt-out of pre-processing global data JSON files: (default: `liquid`)
101+
dataTemplateEngine: false,
102+
103+
// These are all optional (defaults are shown):
104+
dir: {
105+
input: ".",
106+
includes: "_includes",
107+
data: "_data",
108+
output: "_site",
109+
},
110+
};
92111
};
Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11

22
{% if postListItems.length %}
33

4-
{% for item in postListItems %}
5-
6-
4+
{% for item in postListItems %}
75
<!-- this is a list of posts which serves the pagination and tag overview pages -->
8-
9-
106
<!-- START articles -->
11-
<article>
12-
<div>
13-
<a href="{{ item.url }}">
14-
<p>{% if item.data.date %}<time datetime="{{ item.data.date | dateymd }}">{{ item.data.date | datefriendly }}</time>{% endif %}</p>
15-
<h2>{{ item.data.title }}</h2>
16-
<p>{{ item.data.details }}</p></a>
17-
</div>
18-
</article>
7+
<article>
8+
<div>
9+
<a href="{{ item.url }}">
10+
<p>{% if item.date %}<time datetime="{{ item.data.date | dateymd }}">{{ item.data.date | datefriendly }}</time>{% endif %}</p>
11+
<h2>{{ item.data.title }}</h2>
12+
<p>{{ item.data.details }}</p>
13+
</a>
14+
</div>
15+
</article>
1916
<!-- END articles -->
20-
21-
{% endfor %}
22-
23-
{% endif %}
17+
{% endfor %}
18+
{% endif %}

lib/filters/dateformat.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11

22
// date formatting functions
3-
const toMonth = new Intl.DateTimeFormat('en', { month: 'long' });
3+
const toMonth = new Intl.DateTimeFormat("en", { month: "long" });
44

55

66
// format a date to YYYY-MM-DD
7-
module.exports.ymd = date => (
8-
9-
date instanceof Date ?
10-
`${ date.getUTCFullYear() }-${ String(date.getUTCMonth() + 1).padStart(2, '0') }-${ String(date.getUTCDate()).padStart(2, '0') }` : ''
11-
12-
);
7+
module.exports.ymd = (date) =>
8+
date instanceof Date
9+
? `${date.getUTCFullYear()}-${String(date.getUTCMonth() + 1).padStart(
10+
2,
11+
"0"
12+
)}-${String(date.getUTCDate()).padStart(2, "0")}`
13+
: "";
1314

1415
// format a date to DD MMMM, YYYY
15-
module.exports.friendly = date => (
16-
17-
date instanceof Date ?
18-
19-
date.getUTCDate() + ' ' + toMonth.format(date) + ', ' + date.getUTCFullYear() : ''
20-
21-
);
16+
module.exports.friendly = (date) =>
17+
date instanceof Date
18+
? date.getUTCDate() +
19+
" " +
20+
toMonth.format(date) +
21+
", " +
22+
date.getUTCFullYear()
23+
: "";

sights/index.njk

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ pagination:
1818

1919

2020
<!-- START articles -->
21-
<div>
22-
{% include "partials/components/post-list.njk" %} <!-- list of articles -->
23-
</div>
21+
<div>
22+
{% include "partials/components/post-list.njk" %} <!-- list of articles -->
23+
</div>
2424
<!-- END articles -->
2525

2626
{% include "partials/components/pagination.njk" %}
2727

2828
<div>
29-
<p>
30-
<!-- PROBLEM -->
31-
<!-- outputs tags. Should be all tags for collection.sights only, but outputs also all collections.sounds tags - I don't want this to happen and I have to find a solution -->
32-
{% for tag in collections.tagsList %}
29+
<p>
30+
<!-- PROBLEM -->
31+
<!-- outputs tags. Should be all tags for collection.sights only, but outputs also all collections.sounds tags - I don't want this to happen and I have to find a solution -->
32+
{% for tag in collections.sightsTagsList %}
3333
<a href="/sights/tags/{{ tag | slug }}" class="tags">{{tag}}</a>
34-
{% endfor %}
35-
</p>
34+
{% endfor %}
35+
</p>
3636
</div>
3737

38-
{% endblock %}
38+
{% endblock %}

sights/tags.njk

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22
local: en
33

44
pagination:
5-
data: collections
5+
data: collections.sightsTagsList
66
size: 1
77
alias: tag
8-
filter:
9-
- all
10-
- sights
11-
- sounds
12-
- tagsList
138
addAllPagesToCollections: true
14-
permalink: /sights/tags/{{ tag | slug }}/
9+
permalink: "/sights/tags/{{ tag | slug }}/"
1510
---
1611
<!-- this should be tag archive for SIGHTS category only -->
1712
<!-- This will create a page for every tag -->
@@ -26,17 +21,17 @@ permalink: /sights/tags/{{ tag | slug }}/
2621
<!-- START articles -->
2722
<div>
2823
{% include "partials/components/post-list.njk" %}<!-- list of articles -->
29-
</div>
30-
<!-- END articles -->
31-
32-
<div>
33-
<p>
34-
<!-- PROBLEM -->
35-
<!-- outputs tags. Should be all tags for collection.sights only, but outputs also all collections.sounds tags - I don't want this to happen and I have to find a solution -->
36-
{% for tag in collections.tagsList %}
37-
<a href="/sights/tags/{{ tag | slug }}" class="tags">{{tag}}</a>
38-
{% endfor %}
39-
</p>
40-
</div>
24+
</div>
25+
<!-- END articles -->
26+
27+
<div>
28+
<p>
29+
<!-- PROBLEM -->
30+
<!-- outputs tags. Should be all tags for collection.sights only, but outputs also all collections.sounds tags - I don't want this to happen and I have to find a solution -->
31+
{% for tag in collections.sightsTagsList %}
32+
<a href="/sights/tags/{{ tag | slug }}" class="tags">{{ tag }}</a>
33+
{% endfor %}
34+
</p>
35+
</div>
4136

4237
{% endblock %}

sights/video-four.njk

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ title: Video Test Four
88
details: Promo, 2020
99

1010
tags:
11-
- Video Four tag
12-
- promo video
13-
- a video tag
11+
- Video Four
12+
- documentary
13+
- jazz
1414
---
1515

1616
<div>
@@ -19,7 +19,7 @@ tags:
1919
<p>This is a video article</p>
2020
<p>Tagged:
2121
{% for item in tags %}
22-
<a href="/sights/tags/{{ item | slug }}" class="tags article-tag"> {{ item }} </a>
22+
<a href="/sights/tags/{{ item | slug }}" class="tags article-tag">{{ item }}</a>
2323
{% endfor %}
2424
</p>
25-
</div>
25+
</div>

sights/video-one.njk

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ title: Video Test One
88
details: Ident, 2019
99

1010
tags:
11-
- Video One tag
12-
- ident 1 video
13-
- one video tag
11+
- Video One
12+
- documentary
13+
- soul
1414
---
1515

1616
<div>
@@ -19,7 +19,7 @@ tags:
1919
<p>This is a video article</p>
2020
<p>Tagged:
2121
{% for item in tags %}
22-
<a href="/sights/tags/{{ item | slug }}" class="tags article-tag"> {{ item }} </a>
22+
<a href="/sights/tags/{{ item | slug }}" class="tags article-tag">{{ item }}</a>
2323
{% endfor %}
2424
</p>
25-
</div>
25+
</div>

sights/video-three.njk

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ title: Video Test Three
88
details: Docu, 2020
99

1010
tags:
11-
- Video Three tag
12-
- docu video
13-
- another video tag
11+
- Video Three
12+
- docu
13+
- rock
1414
---
1515

1616
<div>
@@ -19,7 +19,7 @@ tags:
1919
<p>This is a video article</p>
2020
<p>Tagged:
2121
{% for item in tags %}
22-
<a href="/sights/tags/{{ item | slug }}" class="tags article-tag"> {{ item }} </a>
22+
<a href="/sights/tags/{{ item | slug }}" class="tags article-tag">{{ item }}</a>
2323
{% endfor %}
2424
</p>
25-
</div>
25+
</div>

0 commit comments

Comments
 (0)