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

Fix Review app code examples using example.options #4202

Merged
merged 6 commits into from
Sep 19, 2023
Merged
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
15 changes: 14 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/govuk-frontend-review/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"@govuk-frontend/lib": "*",
"@govuk-frontend/stats": "*",
"@prettier/sync": "^0.3.0",
"chokidar": "^3.5.3",
"cookie-parser": "^1.4.6",
"express": "^4.18.2",
"express-validator": "^7.0.1",
Expand Down
13 changes: 10 additions & 3 deletions packages/govuk-frontend-review/src/app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ export default async () => {
])

// Feature flags
const flags = {
isDeployedToHeroku: !!process.env.HEROKU_APP
}
const flags = /** @type {FeatureFlags} */ ({
isDeployedToHeroku: !!process.env.HEROKU_APP,
isDevelopment: !['test', 'production'].includes(process.env.NODE_ENV)
})

// Set up Express.js
app.set('flags', flags)
Expand Down Expand Up @@ -207,3 +208,9 @@ export default async () => {
/**
* @typedef {import('@govuk-frontend/lib/components').ComponentFixtures} ComponentFixtures
*/

/**
* @typedef {object} FeatureFlags
* @property {boolean} isDeployedToHeroku - Review app using `HEROKU_APP`
* @property {boolean} isDevelopment - Review app not using `NODE_ENV` production or test
*/
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ router.get('/', (req, res) => {
* Sass docs latest release (when deployed)
*/
router.use('/sass', ({ app }, res, next) => {
const { isDeployedToHeroku } = app.get('flags')
const { isDeployedToHeroku } =
/** @type {import('../../app.mjs').FeatureFlags} */ (app.get('flags'))

if (isDeployedToHeroku) {
return res.redirect(
Expand Down
11 changes: 8 additions & 3 deletions packages/govuk-frontend-review/src/common/nunjucks/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ import * as globals from './globals/index.mjs'
* @returns {import('nunjucks').Environment} Nunjucks Environment
*/
export function renderer(app) {
const flags = /** @type {import('../../app.mjs').FeatureFlags} */ (
app.get('flags')
)

const env = nunjucksEnv(
[join(paths.app, 'src/views')],
{
dev: true, // log stack traces
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh, that's going to be handy for debugging! 🥳

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I discovered it recently and I'm getting bored of adding it 😆

You get to see Nunjucks stack traces right into global/filter code too

express: app, // the Express.js review app that nunjucks should install to
noCache: true, // never use a cache and recompile templates each time
watch: true // reload templates when they are changed. needs chokidar dependency to be installed
noCache: !flags.isDeployedToHeroku, // use cache when deployed only
watch: flags.isDevelopment // reload templates in development only
},
{
moduleRoot: paths.app
Expand All @@ -29,7 +34,7 @@ export function renderer(app) {
app.set('view engine', 'njk')

// Share feature flags with middleware
env.addGlobal('flags', app.get('flags'))
env.addGlobal('flags', flags)

// Custom filters
for (const key in filters) {
Expand Down
18 changes: 16 additions & 2 deletions packages/govuk-frontend-review/src/views/macros/showExamples.njk
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,22 @@
</div>

<div class="govuk-width-container">
{% set detailsHtml %}{% include "partials/code.njk" %}{% endset %}
{{- govukDetails({ summaryText: "Code", html: detailsHtml }) -}}
{% set codeExamplesHtml %}
<h4 class="govuk-heading-s">Markup</h4>
<pre class="app-code"><code tabindex="0" class="app-code__container hljs language-html">
{{- getHTMLCode(componentName, example.options) | highlight("html") | safe -}}
</code></pre>

<h4 class="govuk-heading-s">Macro</h4>
<pre class="app-code"><code tabindex="0" class="app-code__container hljs language-js">
{{- getNunjucksCode(componentName, example.options) | highlight("js") | safe -}}
</code></pre>
{% endset %}

{{ govukDetails({
summaryText: "Code",
html: codeExamplesHtml
}) }}
</div>

</section>
Expand Down
9 changes: 0 additions & 9 deletions packages/govuk-frontend-review/src/views/partials/code.njk

This file was deleted.