Skip to content
Merged
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
15 changes: 9 additions & 6 deletions components/lib/wrap-code-terms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ export default function wrapCodeTerms() {
if (!codeTerms) return

codeTerms.forEach((node) => {
// Return early if a child node is an anchor element
const hasChildAnchor = Array.from(node.childNodes).some((child) => child.nodeName === 'A')
if (hasChildAnchor) return

// Do the wrapping on the inner text only
// Do the wrapping on the inner text only. With anchor element children
// we'll only handle the case where the code term only has a single child
// and that child is an anchor element.
const oldText = escape(node.textContent || '')
const anchorChild = node.querySelector('a')

const newText = oldText.replace(wordsLongerThan18Chars, (str) => {
return (
Expand All @@ -33,6 +32,10 @@ export default function wrapCodeTerms() {
)
})

node.innerHTML = node.innerHTML.replace(oldText, newText)
if (anchorChild && node.childNodes.length === 1) {
anchorChild.innerHTML = anchorChild.innerHTML.replace(oldText, newText)
} else {
node.innerHTML = node.innerHTML.replace(oldText, newText)
}
})
}