Skip to content

Commit ee82fd3

Browse files
Merge pull request #4202 from alphagov/fix-preview-code
Fix Review app code examples using `example.options`
2 parents 261987d + e29ffd9 commit ee82fd3

File tree

7 files changed

+50
-20
lines changed

7 files changed

+50
-20
lines changed

package-lock.json

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/govuk-frontend-review/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"@govuk-frontend/lib": "*",
2626
"@govuk-frontend/stats": "*",
2727
"@prettier/sync": "^0.3.0",
28-
"chokidar": "^3.5.3",
2928
"cookie-parser": "^1.4.6",
3029
"express": "^4.18.2",
3130
"express-validator": "^7.0.1",

packages/govuk-frontend-review/src/app.mjs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ export default async () => {
4646
])
4747

4848
// Feature flags
49-
const flags = {
50-
isDeployedToHeroku: !!process.env.HEROKU_APP
51-
}
49+
const flags = /** @type {FeatureFlags} */ ({
50+
isDeployedToHeroku: !!process.env.HEROKU_APP,
51+
isDevelopment: !['test', 'production'].includes(process.env.NODE_ENV)
52+
})
5253

5354
// Set up Express.js
5455
app.set('flags', flags)
@@ -207,3 +208,9 @@ export default async () => {
207208
/**
208209
* @typedef {import('@govuk-frontend/lib/components').ComponentFixtures} ComponentFixtures
209210
*/
211+
212+
/**
213+
* @typedef {object} FeatureFlags
214+
* @property {boolean} isDeployedToHeroku - Review app using `HEROKU_APP`
215+
* @property {boolean} isDevelopment - Review app not using `NODE_ENV` production or test
216+
*/

packages/govuk-frontend-review/src/common/middleware/docs.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ router.get('/', (req, res) => {
1616
* Sass docs latest release (when deployed)
1717
*/
1818
router.use('/sass', ({ app }, res, next) => {
19-
const { isDeployedToHeroku } = app.get('flags')
19+
const { isDeployedToHeroku } =
20+
/** @type {import('../../app.mjs').FeatureFlags} */ (app.get('flags'))
2021

2122
if (isDeployedToHeroku) {
2223
return res.redirect(

packages/govuk-frontend-review/src/common/nunjucks/index.mjs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ import * as globals from './globals/index.mjs'
1313
* @returns {import('nunjucks').Environment} Nunjucks Environment
1414
*/
1515
export function renderer(app) {
16+
const flags = /** @type {import('../../app.mjs').FeatureFlags} */ (
17+
app.get('flags')
18+
)
19+
1620
const env = nunjucksEnv(
1721
[join(paths.app, 'src/views')],
1822
{
23+
dev: true, // log stack traces
1924
express: app, // the Express.js review app that nunjucks should install to
20-
noCache: true, // never use a cache and recompile templates each time
21-
watch: true // reload templates when they are changed. needs chokidar dependency to be installed
25+
noCache: !flags.isDeployedToHeroku, // use cache when deployed only
26+
watch: flags.isDevelopment // reload templates in development only
2227
},
2328
{
2429
moduleRoot: paths.app
@@ -29,7 +34,7 @@ export function renderer(app) {
2934
app.set('view engine', 'njk')
3035

3136
// Share feature flags with middleware
32-
env.addGlobal('flags', app.get('flags'))
37+
env.addGlobal('flags', flags)
3338

3439
// Custom filters
3540
for (const key in filters) {

packages/govuk-frontend-review/src/views/macros/showExamples.njk

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,22 @@
3535
</div>
3636

3737
<div class="govuk-width-container">
38-
{% set detailsHtml %}{% include "partials/code.njk" %}{% endset %}
39-
{{- govukDetails({ summaryText: "Code", html: detailsHtml }) -}}
38+
{% set codeExamplesHtml %}
39+
<h4 class="govuk-heading-s">Markup</h4>
40+
<pre class="app-code"><code tabindex="0" class="app-code__container hljs language-html">
41+
{{- getHTMLCode(componentName, example.options) | highlight("html") | safe -}}
42+
</code></pre>
43+
44+
<h4 class="govuk-heading-s">Macro</h4>
45+
<pre class="app-code"><code tabindex="0" class="app-code__container hljs language-js">
46+
{{- getNunjucksCode(componentName, example.options) | highlight("js") | safe -}}
47+
</code></pre>
48+
{% endset %}
49+
50+
{{ govukDetails({
51+
summaryText: "Code",
52+
html: codeExamplesHtml
53+
}) }}
4054
</div>
4155

4256
</section>

packages/govuk-frontend-review/src/views/partials/code.njk

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)