astro-ghostcms/.pnpm-store/v3/files/26/2eadbc70c87cf669c1b068ae2cc...

63 lines
1.6 KiB
Plaintext
Raw Permalink Normal View History

2024-02-14 14:10:47 +00:00
import {toString} from 'nlcst-to-string'
import {modifyChildren} from 'unist-util-modify-children'
import {numerical} from '../expressions.js'
// Merge initialisms.
export const mergeInitialisms = modifyChildren(function (child, index, parent) {
if (index > 0 && toString(child) === '.') {
const siblings = parent.children
const previous = siblings[index - 1]
const children = previous.children
if (
previous.type === 'WordNode' &&
children &&
children.length !== 1 &&
children.length % 2 !== 0
) {
let position = children.length
let isAllDigits = true
while (children[--position]) {
const otherChild = children[position]
const value = toString(otherChild)
if (position % 2 === 0) {
// Initialisms consist of one character values.
if (value.length > 1) {
return
}
if (!numerical.test(value)) {
isAllDigits = false
}
} else if (value !== '.') {
if (position < children.length - 2) {
break
} else {
return
}
}
}
if (!isAllDigits) {
// Remove `child` from parent.
siblings.splice(index, 1)
// Add child to the previous children.
children.push(child)
// Update position.
if (previous.position && child.position) {
previous.position.end = child.position.end
}
// Next, iterate over the node *now* at the current position.
return index
}
}
}
})