From 7a7fdd4e87fe39360c573b23da06606ae5e9b072 Mon Sep 17 00:00:00 2001 From: Peter deHaan Date: Mon, 5 Jul 2021 18:06:39 -0700 Subject: [PATCH] Tweaking custom Nunjucks env --- .eleventy.js | 55 ++++++++++++++++++-------------------- src/_includes/base.njk | 4 +-- src/_layouts/index.njk | 1 + src/_layouts/rss2-feed.njk | 14 +++++----- src/posts/hello-day-8.md | 8 ++++-- 5 files changed, 42 insertions(+), 40 deletions(-) diff --git a/.eleventy.js b/.eleventy.js index 731ecdd12..52ae1ae61 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -96,12 +96,12 @@ module.exports = function (eleventyConfig) { }, }); - sassBuild(domain_name); - eleventyConfig.on("beforeWatch", (changedFiles) => { - // changedFiles is an array of files that changed - // to trigger the watch/serve build - sassBuild(domain_name); - }); + // sassBuild(domain_name); + // eleventyConfig.on("beforeWatch", (changedFiles) => { + // // changedFiles is an array of files that changed + // // to trigger the watch/serve build + // sassBuild(domain_name); + // }); // https://www.npmjs.com/package/@quasibit/eleventy-plugin-sitemap @@ -115,35 +115,32 @@ module.exports = function (eleventyConfig) { eleventyConfig.addPassthroughCopy("src/img"); eleventyConfig.addPassthroughCopy("./CNAME"); // eleventyConfig.addPassthroughCopy("src/.gitignore"); - eleventyConfig.addPassthroughCopy({ "dinky/assets/js": "assets/js" }); - eleventyConfig.addPassthroughCopy({ - "dinky/assets/images": "assets/images", - }); - eleventyConfig.addPassthroughCopy({ - "dinky/_sass": "sass/dinky/_sass", - }); + // eleventyConfig.addPassthroughCopy({ "dinky/assets/js": "assets/js" }); + // eleventyConfig.addPassthroughCopy({ + // "dinky/assets/images": "assets/images", + // }); + // eleventyConfig.addPassthroughCopy({ + // "dinky/_sass": "sass/dinky/_sass", + // }); eleventyConfig.addPassthroughCopy({ "src/_sass": "sass/src/_sass", }); - const pathNormalizer = function(pathString){ - return normalize(path.normalize(path.resolve("."))) +const njkEngine = require("nunjucks").configure( + [ + path.join(siteConfiguration.dir.input, siteConfiguration.dir.includes), + path.join(siteConfiguration.dir.input, siteConfiguration.dir.layouts), + siteConfiguration.dir.input, + ], + { + autoescape: false, + throwOnUndefined: true } +); - // Nunjucks Filters - let nunjucksEnvironment = new Nunjucks.Environment( - new Nunjucks.FileSystemLoader([ - pathNormalizer(siteConfiguration.dir.includes), - pathNormalizer(siteConfiguration.dir.input), - pathNormalizer(".") - ]), - { - throwOnUndefined: throwOnUndefinedSetting, - autoescape: true - } - ); - eleventyConfig.setLibrary("njk", nunjucksEnvironment); - eleventyConfig.addNunjucksFilter("interpolate", function(value) { +eleventyConfig.setLibrary('njk', njkEngine ); //: autoescape for CSS rules + +eleventyConfig.addNunjucksFilter("interpolate", function(value) { return Nunjucks.renderString(text, this.ctx); }); diff --git a/src/_includes/base.njk b/src/_includes/base.njk index 1f31aa273..6e5d11270 100644 --- a/src/_includes/base.njk +++ b/src/_includes/base.njk @@ -4,7 +4,7 @@ {% block head %} - {{ title }} + {{ title or "NO TITLE" }} @@ -32,7 +32,7 @@ {% else %} {% endif %} -

{{ title }}

+

{{ title or "NO TITLE" }}

{{ description }}

{% endblock %} diff --git a/src/_layouts/index.njk b/src/_layouts/index.njk index 589542139..709adccf7 100644 --- a/src/_layouts/index.njk +++ b/src/_layouts/index.njk @@ -1,4 +1,5 @@ {% extends "base.njk" %} + {%- from "postList/macros.njk" import postList -%} {% block postcontent %} diff --git a/src/_layouts/rss2-feed.njk b/src/_layouts/rss2-feed.njk index 2f53ec705..90ce26f97 100644 --- a/src/_layouts/rss2-feed.njk +++ b/src/_layouts/rss2-feed.njk @@ -6,10 +6,10 @@ "subtitle": "Notes on various projects", "url": (function(){ return process.env.DOMAIN + "/" })(), "feedUrl": (function(){ return process.env.DOMAIN + "/rss/" })(), - "lastBuildDate": (function(){ - var date = new Date(); - return date.toUTCString() - })(), + "lastBuildDate": (function(){ + var date = new Date(); + return date.toUTCString() + })(), "author": { "name": "Aram Zucker-Scharff", "email": "aramdevblog@aramzs.me" @@ -31,7 +31,7 @@ {{ metadata.title }} {{ metadata.url }} - {{ subtitle }} + {{ metadata.subtitle }} {{ metadata.lastBuildDate }} en-US hourly @@ -48,10 +48,10 @@ {{ post.date.toUTCString() }} {{ metadata.author.name }} {{ absolutePostUrl }} - {{ post.data.excerpt }} + {{ post.data.excerpt or "???" }} {%- for tag in post.tags %} - + {%- endfor %} {%- endfor %} diff --git a/src/posts/hello-day-8.md b/src/posts/hello-day-8.md index 6755f419d..31f321190 100644 --- a/src/posts/hello-day-8.md +++ b/src/posts/hello-day-8.md @@ -111,10 +111,10 @@ And I can also change my passthroughs in `.eleventy.js`. ```javascript eleventyConfig.addPassthroughCopy({ - "dinky/_sass": "sass/dinky/_sass", + "dinky/_sass": "sass/dinky/_sass" }); eleventyConfig.addPassthroughCopy({ - "src/_sass": "sass/src/_sass", + "src/_sass": "sass/src/_sass" }); ``` @@ -124,9 +124,11 @@ If this works correctly on publish, it will resolve the last of my base requirem Ok, I was thinking about how to handle build-time cache-breaking and realized that there's likely a way to handle getting a cache-break variable at the build stage. There's [a plugin for Jekyll to do it](https://github.com/jekyll/github-metadata), it looks like [it does so at least partially via the Github API](https://github.com/jekyll/github-metadata/blob/master/docs/authentication.md). It gets [a pretty good list of data too](https://github.com/jekyll/github-metadata/blob/master/docs/site.github.md). There's also [the "Github Context" which is available to GitHub actions](https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#github-context). I could call the API during build time, which is what it appears that Jekyll is doing (I didn't really look too deeply into the plugin). But if this data is available in the Actions context... couldn't I export it as a environment variable? Why not try adding that to the Github Actions script? +{% raw %} ```yaml - run: export GITHUB_HEAD_SHA=${{ github.run_id }} ``` +{% endraw %} Now I should be able to call this in my site data, right? So I'll update the file at `src/_data/site.js`. @@ -150,6 +152,7 @@ Well... the Sass sitemaps built properly, but none of the Github Actions env stu What if I set the `env` at the level of job? I think this means I could prob use `GITHUB_SHA`, but I want to see what works. +{% raw %} ```yaml jobs: deploy: @@ -157,6 +160,7 @@ jobs: env: MY_GITHUB_RUN_ID: ${{ github.run_id }} ``` +{% endraw %} Ah, that did it, so now I know how to use both!