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

[SPIKE] [WIP] 11ty spike #2750

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
41 changes: 41 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const nunjucks = require('nunjucks')

const highlighter = require('./lib/highlighter.js')
const { getFingerprint, getFrontmatter, getHTMLCode, getNunjucksCode } = require('./lib/file-helper.js') // helper functions to operate on files
const paths = require('./config/paths.json') // specify paths to main working directories
const getMacroOptions = require('./lib/get-macro-options/index.js')

const templatePaths = [
paths.layouts,
paths.partials,
paths.components,
paths.govukfrontend
]

module.exports = eleventyConfig => {
const nunjucksEnvironment = new nunjucks.Environment(
new nunjucks.FileSystemLoader(templatePaths)
)

nunjucksEnvironment.addFilter('highlight', highlighter)
nunjucksEnvironment.addFilter('kebabCase', string => {
return string.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2').toLowerCase()
})
nunjucksEnvironment.addGlobal('getFingerprint', getFingerprint)
nunjucksEnvironment.addGlobal('getFrontmatter', getFrontmatter)
nunjucksEnvironment.addGlobal('getHTMLCode', getHTMLCode)
nunjucksEnvironment.addGlobal('getMacroOptions', getMacroOptions)
nunjucksEnvironment.addGlobal('getNunjucksCode', getNunjucksCode)

eleventyConfig.setLibrary('njk', nunjucksEnvironment)

return {
markdownTemplateEngine: 'njk',
dir: {
input: 'src',
includes: '../views/partials',
layouts: '../views/layouts',
output: 'deploy/public'
}
}
}
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module.exports = {
extends: 'standard',
ignorePatterns: [
'!.*'
],
overrides: [
{
files: ['**/*.test.{cjs,js,mjs}'],
Expand Down
24 changes: 17 additions & 7 deletions lib/file-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ exports.getFrontmatter = path => {

// Get 'fingerprinted' version of a given asset file.
exports.getFingerprint = function (file) {
// Grab fingerprint array from the template context
return file
/* // Grab fingerprint array from the template context
const filePath = this.lookup('path')
const fingerprints = this.lookup('fingerprint')

Expand All @@ -73,9 +74,9 @@ exports.getFingerprint = function (file) {
// calls to this function with a relative path will fail if made from the
// source files themselves.
if (!fingerprints[file] && filePath) {
// Use path.join to correctly join, but metalsmith-fingerprint-ignore
// always expects forward slashes, so replace any backslashes (Windows)
// with a forward slashes.
// Use path.join to correctly join, but metalsmith-fingerprint-ignore
// always expects forward slashes, so replace any backslashes (Windows)
// with a forward slashes.
file = path.join(filePath, file).replace(/\\/g, '/')
}

Expand All @@ -87,7 +88,7 @@ exports.getFingerprint = function (file) {
}

// Look for a fingerprinted asset at this path relative to the site root
return '/' + fingerprints[file]
return '/' + fingerprints[file] */
}

// This helper function takes a path of a *.md.njk file and
Expand All @@ -100,10 +101,19 @@ exports.getHTMLCode = path => {

let html
try {
html = nunjucks.renderString(content)
const templatePaths = [
paths.layouts,
paths.partials,
paths.components,
paths.govukfrontend
]
const nunjucksEnvironment = new nunjucks.Environment(
new nunjucks.FileSystemLoader(templatePaths)
)
html = nunjucksEnvironment.renderString(content)
} catch (err) {
if (err) {
console.log('Could not get HTML code from ' + path)
console.log('Could not get HTML code from ' + path + ':\n' + err)
}
}

Expand Down
Loading