Skip to content

Commit

Permalink
Merge pull request #31 from hashicorp/ks.remove-alert-role
Browse files Browse the repository at this point in the history
Removes alert role from custom plugin
  • Loading branch information
kendallstrautman authored Oct 6, 2021
2 parents aac07b5 + 0f6273b commit 0d92e4c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
15 changes: 7 additions & 8 deletions plugins/paragraph-custom-alerts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ const sigils = {
'=>': 'success',
'->': 'info',
'~>': 'warning',
'!>': 'danger'
'!>': 'danger',
}

module.exports = function paragraphCustomAlertsPlugin() {
return function transformer(tree) {
visit(tree, 'paragraph', (pNode, _, parent) => {
visit(pNode, 'text', textNode => {
Object.keys(sigils).forEach(symbol => {
visit(pNode, 'text', (textNode) => {
Object.keys(sigils).forEach((symbol) => {
if (textNode.value.startsWith(`${symbol} `)) {
// Remove the literal sigil symbol from string contents
textNode.value = textNode.value.replace(`${symbol} `, '')

// Wrap matched nodes with <div> (containing proper attributes)
parent.children = parent.children.map(node => {
parent.children = parent.children.map((node) => {
return is(pNode, node)
? {
type: 'wrapper',
Expand All @@ -29,11 +29,10 @@ module.exports = function paragraphCustomAlertsPlugin() {
className: [
'alert',
`alert-${sigils[symbol]}`,
'g-type-body'
'g-type-body',
],
role: 'alert'
}
}
},
},
}
: node
})
Expand Down
12 changes: 4 additions & 8 deletions plugins/paragraph-custom-alerts/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('paragraph-custom-alerts', () => {
.processSync(`=> this is a success paragraph`)
.toString()
).toMatch(
'<div class="alert alert-success g-type-body" role="alert"><p>this is a success paragraph</p></div>'
'<div class="alert alert-success g-type-body"><p>this is a success paragraph</p></div>'
)
})

Expand All @@ -24,16 +24,12 @@ this is another "normal" block
=> success block here! yeah!`
expect(
remark()
.use(paragraphCustomAlerts)
.use(html)
.processSync(md)
.toString()
remark().use(paragraphCustomAlerts).use(html).processSync(md).toString()
).toMatch(
`<p>this is a normal, non-alert paragraph</p>
<div class="alert alert-warning g-type-body" role="alert"><p>this is a warning block</p></div>
<div class="alert alert-warning g-type-body"><p>this is a warning block</p></div>
<p>this is another "normal" block</p>
<div class="alert alert-success g-type-body" role="alert"><p>success block here! yeah!</p></div>`
<div class="alert alert-success g-type-body"><p>success block here! yeah!</p></div>`
)
})
})

0 comments on commit 0d92e4c

Please sign in to comment.