@@ -27,7 +27,7 @@ {% block content %} {{ appProseScope(content) if content }} - {% include "layouts/shared/related.njk" %} + {% include "templates/shared/related.njk" %} {% endblock %} diff --git a/layouts/search-index.njk b/lib/templates/search-index.njk similarity index 100% rename from layouts/search-index.njk rename to lib/templates/search-index.njk diff --git a/layouts/shared/related.njk b/lib/templates/shared/related.njk similarity index 100% rename from layouts/shared/related.njk rename to lib/templates/shared/related.njk diff --git a/layouts/sitemap.njk b/lib/templates/sitemap.njk similarity index 97% rename from layouts/sitemap.njk rename to lib/templates/sitemap.njk index 60ea4717..7aee797d 100644 --- a/layouts/sitemap.njk +++ b/lib/templates/sitemap.njk @@ -1,4 +1,4 @@ -{% extends "layouts/base.njk" %} +{% extends "templates/base.njk" %} {# Create breadcrumbs manually as sitemap should be excluded from collections #} {% block beforeContent %} diff --git a/layouts/sub-navigation.njk b/lib/templates/sub-navigation.njk similarity index 89% rename from layouts/sub-navigation.njk rename to lib/templates/sub-navigation.njk index 5fd2567b..e64c4589 100644 --- a/layouts/sub-navigation.njk +++ b/lib/templates/sub-navigation.njk @@ -1,4 +1,4 @@ -{% extends "layouts/base.njk" %} +{% extends "templates/base.njk" %} {% block beforeContent %} {{ govukBreadcrumbs({ @@ -22,7 +22,7 @@ {{ appProseScope(content) if content }} - {% include "layouts/shared/related.njk" %} + {% include "templates/shared/related.njk" %} {% endblock %} diff --git a/layouts/tag.njk b/lib/templates/tag.njk similarity index 94% rename from layouts/tag.njk rename to lib/templates/tag.njk index 48aaa703..23cecaf9 100644 --- a/layouts/tag.njk +++ b/lib/templates/tag.njk @@ -1,4 +1,4 @@ -{% extends "layouts/base.njk" %} +{% extends "templates/base.njk" %} {% block beforeContent %} {{ govukBreadcrumbs({ diff --git a/layouts/tags.njk b/lib/templates/tags.njk similarity index 94% rename from layouts/tags.njk rename to lib/templates/tags.njk index e89444a9..ea2cef72 100644 --- a/layouts/tags.njk +++ b/lib/templates/tags.njk @@ -1,4 +1,4 @@ -{% extends "layouts/base.njk" %} +{% extends "templates/base.njk" %} {% block beforeContent %} {{ govukBreadcrumbs({ diff --git a/lib/utils.js b/lib/utils.js index f66e431b..099f34ce 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,6 +1,17 @@ import path from 'node:path' import fs from 'node:fs/promises' +/** + * Get file contents + * @param {string} filePath - Path to file + * @returns {string} File contents + */ +export async function getFileContents(filePath) { + filePath = path.join(import.meta.dirname, '..', filePath) + + return await fs.readFile(filePath, { encoding: 'utf8' }) +} + /** * Read contents of SCSS settings file * @param {object} dir - Eleventy directories @@ -27,6 +38,37 @@ export async function getScssSettings(dir, options) { return settings } +/** + * Get virtual templates + * @param {object} eleventyConfig - Eleventy config + * @returns {object} Template names and strings + */ +export async function getTemplates(eleventyConfig) { + const { includes, layouts } = eleventyConfig.dir + const layoutDir = layouts || includes + const layoutNames = [ + 'base', + 'collection', + 'feed', + 'page', + 'post', + 'product', + 'search-index', + 'sitemap', + 'sub-navigation', + 'tag', + 'tags' + ] + const templates = {} + + for (const name of layoutNames) { + const templateString = await getFileContents(`lib/templates/${name}.njk`) + templates[`${layoutDir}/${name}.njk`] = templateString + } + + return templates +} + /** * Normalise value provided to a filter. Checks that a given value exists * before performing a transformation.