Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add virtual templates #343

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
2 changes: 0 additions & 2 deletions docs/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ export default function(eleventyConfig) {
dir: {
// The folder where all your content will live:
input: 'app',
// Use layouts from the plugin
layouts: '../node_modules/@x-govuk/govuk-eleventy-plugin/layouts'
}
}
};
Expand Down
3 changes: 1 addition & 2 deletions eleventy.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ export default function (eleventyConfig) {
htmlTemplateEngine: 'njk',
markdownTemplateEngine: 'njk',
dir: {
input: 'docs',
layouts: '../layouts'
input: 'docs'
},
pathPrefix: process.env.GITHUB_ACTIONS && '/govuk-eleventy-plugin/'
}
Expand Down
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import { generateAssets } from './lib/events/generate-govuk-assets.js'
import { md } from './lib/markdown-it.js'
import { nunjucksConfig } from './lib/nunjucks.js'
import { scssExtension } from './lib/extensions/scss.js'
import { getTemplates } from './lib/utils.js'

export default function (eleventyConfig, pluginOptions = {}) {
export default async function (eleventyConfig, pluginOptions = {}) {
const { pathPrefix } = eleventyConfig

// Plugin options
Expand All @@ -23,6 +24,12 @@ export default function (eleventyConfig, pluginOptions = {}) {
eleventyConfig.addExtension('scss', scssExtension)
eleventyConfig.addTemplateFormats('scss')

// Virtual templates
const templates = await getTemplates(eleventyConfig)
for (const [virtualPath, template] of Object.entries(templates)) {
eleventyConfig.addTemplate(virtualPath, template)
}

// Collections
for (const [name, collection] of Object.entries(collections)) {
eleventyConfig.addCollection(name, collection)
Expand Down
1 change: 1 addition & 0 deletions lib/nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function nunjucksConfig(eleventyConfig) {

const searchPaths = [
'./node_modules/@x-govuk/govuk-eleventy-plugin',
'./node_modules/@x-govuk/govuk-eleventy-plugin/lib',
'./node_modules/govuk-frontend/dist',
resolveNpmModule('@x-govuk/govuk-prototype-components'),
...(includes ? [path.join(input, includes)] : []),
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions layouts/collection.njk → lib/templates/collection.njk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "layouts/base.njk" %}
{% extends "templates/base.njk" %}

{% block main %}
{{ xGovukMasthead({
Expand Down Expand Up @@ -47,7 +47,7 @@

{% if aside or related %}
<div class="govuk-grid-column-one-third-from-desktop">
{% include "layouts/shared/related.njk" %}
{% include "templates/shared/related.njk" %}
</div>
{% endif %}
</div>
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions layouts/page.njk → lib/templates/page.njk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "layouts/base.njk" %}
{% extends "templates/base.njk" %}

{% block beforeContent %}
{{ govukBreadcrumbs({
Expand All @@ -15,5 +15,5 @@

{{ appProseScope(content) if content }}

{% include "layouts/shared/related.njk" %}
{% include "templates/shared/related.njk" %}
{% endblock %}
4 changes: 2 additions & 2 deletions layouts/post.njk → lib/templates/post.njk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "layouts/base.njk" %}
{% extends "templates/base.njk" %}

{% block beforeContent %}
{{ govukBreadcrumbs({
Expand Down Expand Up @@ -37,7 +37,7 @@

{% if aside or related %}
<div class="govuk-grid-column-one-third-from-desktop">
{% include "layouts/shared/related.njk" %}
{% include "templates/shared/related.njk" %}
</div>
{% endif %}
</div>
Expand Down
4 changes: 2 additions & 2 deletions layouts/product.njk → lib/templates/product.njk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "layouts/base.njk" %}
{% extends "templates/base.njk" %}

{% block main %}
<main id="main-content" role="main" {%- if mainLang %} lang="{{ mainLang }}"{% endif %}>
Expand Down Expand Up @@ -27,7 +27,7 @@
{% block content %}
{{ appProseScope(content) if content }}

{% include "layouts/shared/related.njk" %}
{% include "templates/shared/related.njk" %}
{% endblock %}
</div>
</div>
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion layouts/sitemap.njk → lib/templates/sitemap.njk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "layouts/base.njk" %}
{% extends "templates/base.njk" %}

{# Create breadcrumbs manually as sitemap should be excluded from collections #}
{% block beforeContent %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "layouts/base.njk" %}
{% extends "templates/base.njk" %}

{% block beforeContent %}
{{ govukBreadcrumbs({
Expand All @@ -22,7 +22,7 @@

{{ appProseScope(content) if content }}

{% include "layouts/shared/related.njk" %}
{% include "templates/shared/related.njk" %}
</div>
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion layouts/tag.njk → lib/templates/tag.njk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "layouts/base.njk" %}
{% extends "templates/base.njk" %}

{% block beforeContent %}
{{ govukBreadcrumbs({
Expand Down
2 changes: 1 addition & 1 deletion layouts/tags.njk → lib/templates/tags.njk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "layouts/base.njk" %}
{% extends "templates/base.njk" %}

{% block beforeContent %}
{{ govukBreadcrumbs({
Expand Down
42 changes: 42 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down