45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
'use strict'
|
|
|
|
var toString = require('nlcst-to-string')
|
|
var modifyChildren = require('unist-util-modify-children')
|
|
|
|
module.exports = modifyChildren(mergeFinalWordSymbol)
|
|
|
|
// Merge certain punctuation marks into their preceding words.
|
|
function mergeFinalWordSymbol(child, index, parent) {
|
|
var children
|
|
var prev
|
|
var next
|
|
|
|
if (
|
|
index !== 0 &&
|
|
(child.type === 'SymbolNode' || child.type === 'PunctuationNode') &&
|
|
toString(child) === '-'
|
|
) {
|
|
children = parent.children
|
|
|
|
prev = children[index - 1]
|
|
next = children[index + 1]
|
|
|
|
if (
|
|
(!next || next.type !== 'WordNode') &&
|
|
(prev && prev.type === 'WordNode')
|
|
) {
|
|
// Remove `child` from parent.
|
|
children.splice(index, 1)
|
|
|
|
// Add the punctuation mark at the end of the previous node.
|
|
prev.children.push(child)
|
|
|
|
// Update position.
|
|
if (prev.position && child.position) {
|
|
prev.position.end = child.position.end
|
|
}
|
|
|
|
// Next, iterate over the node *now* at the current position (which was
|
|
// the next node).
|
|
return index
|
|
}
|
|
}
|
|
}
|