From 53b536603afe960ad304131426c5211607ade49f Mon Sep 17 00:00:00 2001 From: Paul Robert Lloyd Date: Fri, 27 Dec 2024 22:04:37 +0000 Subject: [PATCH] Allow provided collections to be overridden in user config --- index.js | 13 +++---------- layouts/sub-navigation.njk | 2 +- lib/collections/index.js | 17 +++++++++++++++++ lib/collections/{ordered.js => navigation.js} | 0 4 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 lib/collections/index.js rename lib/collections/{ordered.js => navigation.js} (100%) diff --git a/index.js b/index.js index c7ecd5c4..df074875 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,7 @@ const { EleventyHtmlBasePlugin } = require('@11ty/eleventy') +const addCollections = require('./lib/collections/index.js') + module.exports = function (eleventyConfig, pluginOptions = {}) { const { pathPrefix } = eleventyConfig @@ -11,16 +13,7 @@ module.exports = function (eleventyConfig, pluginOptions = {}) { eleventyConfig.setLibrary('njk', require('./lib/nunjucks.js')(eleventyConfig)) // Collections - eleventyConfig.addCollection('all', require('./lib/collections/all.js')) - eleventyConfig.addCollection( - 'ordered', - require('./lib/collections/ordered.js') - ) - eleventyConfig.addCollection( - 'sitemap', - require('./lib/collections/sitemap.js') - ) - eleventyConfig.addCollection('tags', require('./lib/collections/tags.js')) + addCollections(eleventyConfig) // Extensions and template formats eleventyConfig.addExtension('scss', require('./lib/extensions/scss.js')) diff --git a/layouts/sub-navigation.njk b/layouts/sub-navigation.njk index 5fd2567b..2462ed99 100644 --- a/layouts/sub-navigation.njk +++ b/layouts/sub-navigation.njk @@ -11,7 +11,7 @@
{{ xGovukSubNavigation({ - items: collections.ordered | eleventyNavigation(sectionKey or options.homeKey) | itemsFromNavigation(page.url, { pathPrefix: options.pathPrefix }) + items: collections.navigation | eleventyNavigation(sectionKey or options.homeKey) | itemsFromNavigation(page.url, { pathPrefix: options.pathPrefix }) }) }}
diff --git a/lib/collections/index.js b/lib/collections/index.js new file mode 100644 index 00000000..c379518e --- /dev/null +++ b/lib/collections/index.js @@ -0,0 +1,17 @@ +module.exports = function (eleventyConfig) { + if (!eleventyConfig.collections.all) { + eleventyConfig.addCollection('all', require('./all.js')) + } + + if (!eleventyConfig.collections.navigation) { + eleventyConfig.addCollection('navigation', require('./navigation.js')) + } + + if (!eleventyConfig.collections.sitemap) { + eleventyConfig.addCollection('sitemap', require('./sitemap.js')) + } + + if (!eleventyConfig.collections.tag) { + eleventyConfig.addCollection('tags', require('./tags.js')) + } +} diff --git a/lib/collections/ordered.js b/lib/collections/navigation.js similarity index 100% rename from lib/collections/ordered.js rename to lib/collections/navigation.js