-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Nunjucks functions into
@govuk-frontend/lib/nunjucks
But leaves component-specific `render()` and `renderPreview()` functions in `@govuk-frontend/lib/components`
- Loading branch information
1 parent
130b1f5
commit 003fbb0
Showing
9 changed files
with
142 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
const components = require('./components') | ||
const names = require('./names') | ||
const nunjucks = require('./nunjucks') | ||
|
||
/** | ||
* Shared library | ||
*/ | ||
module.exports = { | ||
components, | ||
names | ||
names, | ||
nunjucks | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
const { join, dirname } = require('path') | ||
|
||
const nunjucks = require('nunjucks') | ||
const { outdent } = require('outdent') | ||
|
||
const { packageTypeToPath } = require('./names') | ||
|
||
// Nunjucks default environment | ||
const env = nunjucksEnv() | ||
|
||
/** | ||
* Nunjucks environment factory | ||
* | ||
* @param {string[]} [searchPaths] - Nunjucks search paths (optional) | ||
* @param {import('nunjucks').ConfigureOptions} [nunjucksOptions] - Nunjucks options (optional) | ||
* @param {import('./names').PackageOptions} [packageOptions] - Package options (optional) | ||
* @returns {import('nunjucks').Environment} Nunjucks environment | ||
*/ | ||
function nunjucksEnv(searchPaths = [], nunjucksOptions = {}, packageOptions) { | ||
const packagePath = dirname( | ||
packageTypeToPath('govuk-frontend', packageOptions) | ||
) | ||
|
||
// Add to Nunjucks search paths (without 'govuk' suffix) | ||
searchPaths.push(join(packagePath, '../')) | ||
|
||
// Nunjucks environment | ||
return nunjucks.configure(searchPaths, { | ||
trimBlocks: true, // automatically remove trailing newlines from a block/tag | ||
lstripBlocks: true, // automatically remove leading whitespace from a block/tag, | ||
...nunjucksOptions | ||
}) | ||
} | ||
|
||
/** | ||
* Render macro HTML | ||
* | ||
* @param {string} macroName - The name of the macro | ||
* @param {string} macroPath - The path to the file containing the macro | ||
* @param {MacroRenderOptions} [options] - Nunjucks macro render options | ||
* @returns {string} HTML rendered by the macro | ||
*/ | ||
function renderMacro(macroName, macroPath, options) { | ||
const paramsFormatted = JSON.stringify(options?.context ?? {}, undefined, 2) | ||
|
||
let macroString = `{%- from "${macroPath}" import ${macroName} -%}` | ||
|
||
// If we're nesting child components or text, pass the children to the macro | ||
// using the 'caller' Nunjucks feature | ||
macroString += options?.callBlock | ||
? `{%- call ${macroName}(${paramsFormatted}) -%}${options.callBlock}{%- endcall -%}` | ||
: `{{- ${macroName}(${paramsFormatted}) -}}` | ||
|
||
return renderString(macroString, options) | ||
} | ||
|
||
/** | ||
* Render string | ||
* | ||
* @param {string} string - Nunjucks string to render | ||
* @param {MacroRenderOptions | TemplateRenderOptions} [options] - Nunjucks render options | ||
* @returns {string} HTML rendered from the Nunjucks string | ||
*/ | ||
function renderString(string, options) { | ||
const nunjucksEnv = options?.env ?? env | ||
return nunjucksEnv.renderString(string, options?.context ?? {}) | ||
} | ||
|
||
/** | ||
* Render template HTML | ||
* | ||
* @param {string} templatePath - Nunjucks template path | ||
* @param {TemplateRenderOptions} [options] - Nunjucks template render options | ||
* @returns {string} HTML rendered by template.njk | ||
*/ | ||
function renderTemplate(templatePath, options = {}) { | ||
let viewString = `{% extends "${templatePath}" %}` | ||
|
||
for (const [name, content] of Object.entries(options?.blocks ?? {})) { | ||
viewString += outdent` | ||
{% block ${name} -%} | ||
${content} | ||
{%- endblock %}` | ||
} | ||
|
||
return renderString(viewString, options) | ||
} | ||
|
||
module.exports = { | ||
nunjucksEnv, | ||
renderMacro, | ||
renderString, | ||
renderTemplate | ||
} | ||
|
||
/** | ||
* Nunjucks macro options | ||
* | ||
* @typedef {{ [param: string]: unknown }} MacroOptions | ||
*/ | ||
|
||
/** | ||
* Nunjucks macro render options | ||
* | ||
* @typedef {object} MacroRenderOptions | ||
* @property {MacroOptions} [context] - Nunjucks context object (optional) | ||
* @property {string} [callBlock] - Nunjucks macro `caller()` content (optional) | ||
* @property {import('nunjucks').Environment} [env] - Nunjucks environment (optional) | ||
*/ | ||
|
||
/** | ||
* Nunjucks template render options | ||
* | ||
* @typedef {object} TemplateRenderOptions | ||
* @property {object} [context] - Nunjucks context object (optional) | ||
* @property {{ [blockName: string]: string }} [blocks] - Nunjucks blocks (optional) | ||
* @property {import('nunjucks').Environment} [env] - Nunjucks environment (optional) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters