Skip to content

Commit

Permalink
Merge pull request #4651 from alphagov/html-code-render
Browse files Browse the repository at this point in the history
Output HTML in code preview without formatter
  • Loading branch information
colinrotherham authored Jan 18, 2024
2 parents 39c0a23 + 9ce5db1 commit ced96cb
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 128 deletions.
113 changes: 4 additions & 109 deletions 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 @@ -34,7 +34,6 @@
"govuk-frontend": "*",
"highlight.js": "^11.9.0",
"iframe-resizer": "^4.3.9",
"js-beautify": "^1.14.11",
"marked": "^11.1.1",
"marked-linkify-it": "^3.1.8",
"marked-smartypants": "^1.1.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ import hljs from 'highlight.js'
* @returns {string} Code with syntax highlighting
*/
export function highlight(code, language) {
return hljs.highlight(code.trim(), { language: language || 'plaintext' })
.value
return hljs.highlight(code, { language: language || 'plaintext' }).value
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { render } from '@govuk-frontend/lib/components'
import beautify from 'js-beautify'

/**
* Component HTML code (formatted)
Expand All @@ -10,20 +9,7 @@ import beautify from 'js-beautify'
* @returns {string} HTML rendered by the component
*/
export function getHTMLCode(componentName, options) {
const html = render(componentName, { ...options, env: this.env })

// Default beautify options
const beautifyOptions = beautify.html.defaultOptions()

return beautify.html(html, {
indent_size: 2,
// Ensure nested labels in headings are indented properly
inline: beautifyOptions.inline.filter((tag) => !['label'].includes(tag)),
// Remove blank lines
max_preserve_newlines: 0,
// Ensure attribute wrapping in header SVG is preserved
wrap_attributes: 'preserve'
})
return render(componentName, { ...options, env: this.env })
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
<h4 class="govuk-heading-s">Markup</h4>
<pre class="app-code"><code tabindex="0" class="app-code__container hljs language-html">
{{- getHTMLCode(componentName, {
context: example.options
context: example.options,
fixture: example
}) | highlight("html") | safe -}}
</code></pre>

Expand Down

0 comments on commit ced96cb

Please sign in to comment.