-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support markdown for payment link details
- Loading branch information
1 parent
35224b6
commit 1fc08b2
Showing
3 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* Credit: https://github.com/ministryofjustice/opg-performance-data/blob/63fac0c26a701f37cfa2d4d8229cf82937ad5047/markdown-it-gds.js#L4 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const markdownIt = require('markdown-it') | ||
|
||
const linkExternalStack = [] | ||
|
||
/** | ||
* Rules that can be enabled/disabled for markdown-it can be found here: | ||
* https://github.com/markdown-it/markdown-it/blob/0fe7ccb4b7f30236fb05f623be6924961d296d3d/lib/parser_block.mjs | ||
* https://github.com/markdown-it/markdown-it/blob/HEAD/lib/parser_inline.mjs | ||
*/ | ||
const md = markdownIt({ | ||
html: false, | ||
breaks: true | ||
}) | ||
.disable(['heading', 'lheading', 'image', 'table', 'code', 'fence', 'blockquote', 'hr', 'html_block', 'reference', 'emphasis', 'backticks', 'strikethrough', 'html_inline', 'autolink', 'entity']) | ||
|
||
function defaultRender (tokens, idx, options, env, self) { | ||
return self.renderToken(tokens, idx, options); | ||
} | ||
|
||
md.renderer.rules.paragraph_open = function (tokens, idx, options, env, self) { | ||
tokens[idx].attrPush(["class", "govuk-body"]); | ||
return defaultRender(tokens, idx, options, env, self); | ||
} | ||
|
||
md.renderer.rules.link_open = function (tokens, idx, options, env, self) { | ||
let externalLink = false; | ||
tokens[idx].attrPush(["class", "govuk-link"]); | ||
if (tokens[idx].attrGet("href").indexOf("http") === 0) { | ||
externalLink = true; | ||
tokens[idx].attrPush(["target", "_blank"]); | ||
tokens[idx].attrPush(["rel", "noreferrer noopener"]); | ||
|
||
linkExternalStack.push(externalLink); | ||
} | ||
return defaultRender(tokens, idx, options, env, self); | ||
} | ||
|
||
md.renderer.rules.link_close = function (tokens, idx, options, env, self) { | ||
const externalLink = linkExternalStack.pop(); | ||
if (externalLink) { | ||
return `<span class="govuk-visually-hidden">(opens in new tab)</span></a>`; | ||
} | ||
return defaultRender(tokens, idx, options, env, self); | ||
}; | ||
|
||
md.renderer.rules.bullet_list_open = function (tokens, idx, options, env, self) { | ||
tokens[idx].attrPush(["class", "govuk-list govuk-list--bullet"]); | ||
return defaultRender(tokens, idx, options, env, self); | ||
} | ||
|
||
md.renderer.rules.ordered_list_open = function (tokens, idx, options, env, self) { | ||
tokens[idx].attrPush(["class", "govuk-list govuk-list--number"]); | ||
return defaultRender(tokens, idx, options, env, self); | ||
} | ||
|
||
function formatMarkdown (rawText) { | ||
return md.render(rawText) | ||
} | ||
|
||
module.exports = { | ||
formatMarkdown | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters