Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<div class="govuk-form-group">
<div class="{{ model.declaration.classes }}">
{{ model.declaration.text | markdown | safe }}
{{ model.declaration.text | markdown({ startingHeaderLevel: 2 }) | safe }}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do we need this for the guidance/markdown component too?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nailed it, otherwise 👌

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point. It's been a while so I've forgotten what was going on. I'll check 😀

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Guidance/markdown is covered in the changes to file markdown.js in this PR. I tested the preview and it's all fine.

</div>
</div>

Expand Down
4 changes: 2 additions & 2 deletions model/src/form/form-editor/preview/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ export class Markdown extends Content {
constructor(htmlElements, questionRenderer) {
super(htmlElements, questionRenderer)
const { content } = htmlElements.values
this._content = markdownToHtml(content)
this._content = markdownToHtml(content, { startingHeaderLevel: 2 })
}

/**
* @param {string} value
* @protected
*/
_setContent(value) {
super._setContent(markdownToHtml(value))
super._setContent(markdownToHtml(value, { startingHeaderLevel: 2 }))
}
}

Expand Down
10 changes: 6 additions & 4 deletions model/src/form/form-editor/preview/markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ describe('markdown', () => {
const questionElements = new ContentElements(
buildMarkdownComponent({
title: 'Which quest would you like to pick?',
content: '# This is a heading'
content: '# This is a heading demoted'
})
)
describe('Markdown', () => {
it('should create class', () => {
expect(questionElements.values.content).toBe('# This is a heading')
expect(questionElements.values.content).toBe(
'# This is a heading demoted'
)
const res = new Markdown(questionElements, renderer)
expect(res.renderInput).toEqual({
id: 'markdown',
name: 'markdown',
classes: '',
content: '<h1>This is a heading</h1>\n'
content: '<h2>This is a heading demoted</h2>\n'
})
expect(res.titleText).toBe('Which quest would you like to pick?')
expect(res.question).toBe('Which quest would you like to pick?')
Expand All @@ -40,7 +42,7 @@ describe('markdown', () => {
res.optional = true
expect(res.titleText).toBe('New question (optional)')
res.content = '## This is a subheading'
expect(res.content).toBe('<h2>This is a subheading</h2>\n')
expect(res.content).toBe('<h3>This is a subheading</h3>\n')
})
})
})
47 changes: 46 additions & 1 deletion model/src/utils/markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('Helpers', () => {
html: '<p><a class="govuk-link" href="mailto:dummy@defra.gov.uk">link</a></p>\n'
}
])("formats '$markdown' to '$html'", ({ markdown, html }) => {
expect(markdownToHtml(markdown, exampleBaseUrl)).toBe(html)
expect(markdownToHtml(markdown, { baseUrl: exampleBaseUrl })).toBe(html)
})
})

Expand All @@ -63,4 +63,49 @@ describe('Helpers', () => {
expect(markdownToHtml(markdown)).toBe(html)
})
})

describe('markdown with demotion from speific levels', () => {
it.each([
{
level: 1,
markdown:
'# This is H1\n## This is H2\n### This is H3\n#### This is H4\n##### This is H5\n###### This is H6\n',
html: '<h1>This is H1</h1>\n<h2>This is H2</h2>\n<h3>This is H3</h3>\n<h4>This is H4</h4>\n<h5>This is H5</h5>\n<h6>This is H6</h6>\n'
},
{
level: 2,
markdown:
'# This is H1\n## This is H2\n### This is H3\n#### This is H4\n##### This is H5\n###### This is H6\n',
html: '<h2>This is H1</h2>\n<h3>This is H2</h3>\n<h4>This is H3</h4>\n<h5>This is H4</h5>\n<h6>This is H5</h6>\n<h6>This is H6</h6>\n'
},
{
level: 3,
markdown:
'# This is H1\n## This is H2\n### This is H3\n#### This is H4\n##### This is H5\n###### This is H6\n',
html: '<h3>This is H1</h3>\n<h4>This is H2</h4>\n<h5>This is H3</h5>\n<h6>This is H4</h6>\n<h6>This is H5</h6>\n<h6>This is H6</h6>\n'
},
{
level: 4,
markdown:
'# This is H1\n## This is H2\n### This is H3\n#### This is H4\n##### This is H5\n###### This is H6\n',
html: '<h4>This is H1</h4>\n<h5>This is H2</h5>\n<h6>This is H3</h6>\n<h6>This is H4</h6>\n<h6>This is H5</h6>\n<h6>This is H6</h6>\n'
},
{
level: 5,
markdown:
'# This is H1\n## This is H2\n### This is H3\n#### This is H4\n##### This is H5\n###### This is H6\n',
html: '<h5>This is H1</h5>\n<h6>This is H2</h6>\n<h6>This is H3</h6>\n<h6>This is H4</h6>\n<h6>This is H5</h6>\n<h6>This is H6</h6>\n'
},
{
level: 6,
markdown:
'# This is H1\n## This is H2\n### This is H3\n#### This is H4\n##### This is H5\n###### This is H6\n',
html: '<h6>This is H1</h6>\n<h6>This is H2</h6>\n<h6>This is H3</h6>\n<h6>This is H4</h6>\n<h6>This is H5</h6>\n<h6>This is H6</h6>\n'
}
])("formats '$markdown' to '$html'", ({ markdown, html, level }) => {
expect(markdownToHtml(markdown, { startingHeaderLevel: level })).toBe(
html
)
})
})
})
24 changes: 21 additions & 3 deletions model/src/utils/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,26 @@ function renderLink(href: string, text: string, baseUrl?: string) {
return `<a ${attrs.join(' ')}>${label}</a>`
}

function demoteHeading(
text: string,
depth: number,
startingHeaderLevel: number
) {
// Max heading is h6 so don't demote further than that
depth = Math.min(depth + startingHeaderLevel - 1, 6)
return `<h${depth}>${text}</h${depth}>
`
}

/**
* Convert markdown to HTML, escaping any HTML tags first
*/
export function markdownToHtml(
markdown: string | null | undefined,
baseUrl?: string // optional in some contexts, e.g. from the designer where it might not make sense
options?: {
baseUrl?: string // optional in some contexts, e.g. from the designer where it might not make sense,
startingHeaderLevel?: number
}
) {
if (markdown === undefined || markdown === null) {
return ''
Expand All @@ -46,8 +60,12 @@ export function markdownToHtml(

const renderer = new Renderer()
renderer.link = ({ href, text }: Tokens.Link): string => {
return renderLink(href, text, baseUrl)
return renderLink(href, text, options?.baseUrl)
}
if (options?.startingHeaderLevel) {
renderer.heading = ({ text, depth }: Tokens.Heading): string => {
return demoteHeading(text, depth, options.startingHeaderLevel ?? 1)
}
}

return marked.parse(escaped, { async: false, renderer })
}
Loading