Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 84 additions & 77 deletions .eleventy.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,99 @@

module.exports = function (eleventyConfig) {
const now = Date.now();
const livePosts = (post) => post.date <= now;

eleventyConfig.addFilter("tagsList", (arr = []) => {
const tagsSet = new Set();
arr.forEach((item) => item.data.tags?.forEach((tag) => tagsSet.add(tag)));
return [...tagsSet].sort((b, a) => b.localeCompare(a));
});

eleventyConfig.addFilter("inspect", require("util").inspect);

// SIGHTS - custom collections
const nowsights = new Date();
const sightslivePosts = (post) => post.date <= nowsights;
eleventyConfig.addCollection("sights", (collection) => {
return [
...collection.getFilteredByGlob("./sights/*.njk").filter(sightslivePosts),
].reverse();
const posts = collection.getFilteredByGlob("./sights/*.njk");
return [...posts].filter(livePosts).reverse();
});

// SOUNDS - custom collections
const nowsounds = new Date();
const soundslivePosts = (post) => post.date <= nowsounds;
eleventyConfig.addCollection("sounds", (collection) => {
return [
...collection.getFilteredByGlob("./sounds/*.njk").filter(soundslivePosts),
].reverse();
const posts = collection.getFilteredByGlob("./sounds/*.njk");
return [...posts].filter(livePosts).reverse();
});

// generate a list of all tags collections
// with alphabetical sorting - had to invert to "b, a" because it was sorting upside down
eleventyConfig.addCollection('tagsList', (collectionApi) => {
const tagsSet = new Set()
collectionApi.getAll().forEach((item) => {
if (!item.data.tags) return
item.data.tags.forEach((tag) => tagsSet.add(tag))
})
return [...tagsSet].sort((b, a) => b.localeCompare(a))
eleventyConfig.addCollection("sightsTagsList", (collection) => {
const tagsListFilter = eleventyConfig.getFilter("tagsList");
const items = collection
.getFilteredByGlob("./sights/*.njk")
.filter(livePosts);
return tagsListFilter(items);
});

eleventyConfig.addCollection("soundsTagsList", (collection) => {
const tagsListFilter = eleventyConfig.getFilter("tagsList");
const items = collection
.getFilteredByGlob("./sounds/*.njk")
.filter(livePosts);
return tagsListFilter(items);
});

// https://www.11ty.dev/docs/copy/#manual-passthrough-file-copy-(faster)
eleventyConfig.addPassthroughCopy('assets');
eleventyConfig.addPassthroughCopy('sitemap.xml');
eleventyConfig.addPassthroughCopy('robots.txt');


// format dates on SIGHTS and SOUNDS Articles
const dateformat = require('./lib/filters/dateformat');
eleventyConfig.addFilter('datefriendly', dateformat.friendly);
eleventyConfig.addFilter('dateymd', dateformat.ymd);


// "return" in order to keep the previously entered configuration values
// taken from: https://github.com/11ty/eleventy-base-blog/blob/master/.eleventy.js
return {
// Control which files Eleventy will process
// e.g.: *.md, *.njk, *.html, *.liquid
templateFormats: [
"md",
"njk",
"html",
"liquid"
],

// added
passthroughFileCopy: true,

// -----------------------------------------------------------------
// If your site deploys to a subdirectory, change `pathPrefix`.
// Don’t worry about leading and trailing slashes, we normalize these.

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

// You can also pass this in on the command line using `--pathprefix`

// Optional (default is shown)
pathPrefix: "/",
// -----------------------------------------------------------------

// Pre-process *.md files with: (default: `liquid`)
markdownTemplateEngine: "njk",

// Pre-process *.html files with: (default: `liquid`)
htmlTemplateEngine: "njk",

// Opt-out of pre-processing global data JSON files: (default: `liquid`)
dataTemplateEngine: false,
eleventyConfig.addFilter("head", (arr = [], idx = 0) => {
if (idx < 0) {
return arr.slice(idx);
}
return arr.slice(0, idx);
});

// These are all optional (defaults are shown):
dir: {
input: ".",
includes: "_includes",
data: "_data",
output: "_site"
}
};
// https://www.11ty.dev/docs/copy/#manual-passthrough-file-copy-(faster)
eleventyConfig.addPassthroughCopy("assets");
eleventyConfig.addPassthroughCopy("sitemap.xml");
eleventyConfig.addPassthroughCopy("robots.txt");

// format dates on SIGHTS and SOUNDS Articles
const dateformat = require("./lib/filters/dateformat");
eleventyConfig.addFilter("datefriendly", dateformat.friendly);
eleventyConfig.addFilter("dateymd", dateformat.ymd);

// "return" in order to keep the previously entered configuration values
// taken from: https://github.com/11ty/eleventy-base-blog/blob/master/.eleventy.js
return {
// Control which files Eleventy will process
// e.g.: *.md, *.njk, *.html, *.liquid
templateFormats: ["md", "njk", "html", "liquid"],

// added
passthroughFileCopy: true,

// -----------------------------------------------------------------
// If your site deploys to a subdirectory, change `pathPrefix`.
// Don’t worry about leading and trailing slashes, we normalize these.

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

// You can also pass this in on the command line using `--pathprefix`

// Optional (default is shown)
pathPrefix: "/",
// -----------------------------------------------------------------

// Pre-process *.md files with: (default: `liquid`)
markdownTemplateEngine: "njk",

// Pre-process *.html files with: (default: `liquid`)
htmlTemplateEngine: "njk",

// Opt-out of pre-processing global data JSON files: (default: `liquid`)
dataTemplateEngine: false,

// These are all optional (defaults are shown):
dir: {
input: ".",
includes: "_includes",
data: "_data",
output: "_site",
},
};
};
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
_site
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ But instead I get:
Raymond Camden (Thank you very much Raymond), noticed that both tags pages are still paginating over collections, not collections.sights or collections.sounds;
once I've tried to resolve the pagination as above - which is the right change - I get errors.

Raymond noticed that this is because it's exposing another error: the issue is that permalink, as I have it, cannot use dynamic data like that,
Raymond noticed that this is because it's exposing another error: the issue is that permalink, as I have it, cannot use dynamic data like that,
and he suggested a workaround: to use eleventyComputed to define the permalink (https://www.11ty.dev/docs/data-computed/#using-a-template-string).

After several attempts there are still errors. Is clear that the problem is my incompetence in how to remove the dynamic value from the front matter.

You can see my original question and suggestions from Raymond Camden on:
You can see my original question and suggestions from Raymond Camden on:
https://stackoverflow.com/questions/68814674/eleventy-11ty-tag-list-sorting-for-2-independent-directories


37 changes: 15 additions & 22 deletions _includes/partials/components/post-list.njk
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@

{% if postListItems.length %}

{% for item in postListItems %}


<!-- this is a list of posts which serves the pagination and tag overview pages -->


<!-- START articles -->
<article>
<div>
<a href="{{ item.url }}">
<p>{% if item.data.date %}<time datetime="{{ item.data.date | dateymd }}">{{ item.data.date | datefriendly }}</time>{% endif %}</p>
<h2>{{ item.data.title }}</h2>
<p>{{ item.data.details }}</p></a>
</div>
</article>
<!-- END articles -->

{% endfor %}

{% endif %}
{% for item in postListItems %}
<!-- this is a list of posts which serves the pagination and tag overview pages -->
<!-- START articles -->
<article>
<div>
<a href="{{ item.url }}">
<p>{% if item.date %}<time datetime="{{ item.data.date | dateymd }}">{{ item.data.date | datefriendly }}</time>{% endif %}</p>
<h2>{{ item.data.title }}</h2>
<p>{{ item.data.details }}</p>
</a>
</div>
</article>
<!-- END articles -->
{% endfor %}
{% endif %}
7 changes: 2 additions & 5 deletions index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ local: en

<h1>HOMEPAGE</h1>

<!--

TO DO
{# TO DO

Here I want to FILTER RECENT POSTS
respectively for categories SIGHTS + SOUNDS with the latest 4 posts for each category
Expand Down Expand Up @@ -42,6 +40,5 @@ as explained here: https://github.com/11ty/eleventy/issues/503 ( pdkaizer commen
pagination:
data: collections... // the problem here is that I can specify only 1 collection and I have two of them to output on this page: how to solve this?
size: 4
reverse: true
reverse: true #}

-->
32 changes: 16 additions & 16 deletions lib/filters/dateformat.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@

// date formatting functions
const toMonth = new Intl.DateTimeFormat('en', { month: 'long' });

const toMonth = new Intl.DateTimeFormat("en", { month: "long" });

// format a date to YYYY-MM-DD
module.exports.ymd = date => (

date instanceof Date ?
`${ date.getUTCFullYear() }-${ String(date.getUTCMonth() + 1).padStart(2, '0') }-${ String(date.getUTCDate()).padStart(2, '0') }` : ''

);
module.exports.ymd = (date) =>
date instanceof Date
? `${date.getUTCFullYear()}-${String(date.getUTCMonth() + 1).padStart(
2,
"0"
)}-${String(date.getUTCDate()).padStart(2, "0")}`
: "";

// format a date to DD MMMM, YYYY
module.exports.friendly = date => (

date instanceof Date ?

date.getUTCDate() + ' ' + toMonth.format(date) + ', ' + date.getUTCFullYear() : ''

);
module.exports.friendly = (date) =>
date instanceof Date
? date.getUTCDate() +
" " +
toMonth.format(date) +
", " +
date.getUTCFullYear()
: "";
Loading