This commit is contained in:
k00b 2024-09-28 18:19:46 -05:00
parent cc4bbf99e4
commit f7010ee3f7
1 changed files with 16 additions and 5 deletions

View File

@ -20,6 +20,7 @@ export default function rehypeSN (options = {}) {
node.properties.inline = !(parent && parent.tagName === 'pre') node.properties.inline = !(parent && parent.tagName === 'pre')
} }
// handle headings
if (node.type === 'element' && ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].includes(node.tagName) && !node.properties.id) { if (node.type === 'element' && ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].includes(node.tagName) && !node.properties.id) {
const nodeText = toString(node) const nodeText = toString(node)
const headingId = slug(nodeText.replace(/[^\w\-\s]+/gi, '')) const headingId = slug(nodeText.replace(/[^\w\-\s]+/gi, ''))
@ -89,12 +90,22 @@ export default function rehypeSN (options = {}) {
const newChildren = [] const newChildren = []
let lastIndex = 0 let lastIndex = 0
let match let match
let childrenConsumed = 1
let text = toString(node)
const combinedRegex = new RegExp(mentionRegex.source + '|' + subRegex.source, 'gi') const combinedRegex = new RegExp(mentionRegex.source + '|' + subRegex.source, 'gi')
while ((match = combinedRegex.exec(node.value)) !== null) { // handle @__username__ or ~__sub__
if (['@', '~'].includes(node.value) &&
parent.children[index + 1]?.tagName === 'strong' &&
parent.children[index + 1].children[0]?.type === 'text') {
childrenConsumed = 2
text = node.value + '__' + toString(parent.children[index + 1]) + '__'
}
while ((match = combinedRegex.exec(text)) !== null) {
if (lastIndex < match.index) { if (lastIndex < match.index) {
newChildren.push({ type: 'text', value: node.value.slice(lastIndex, match.index) }) newChildren.push({ type: 'text', value: text.slice(lastIndex, match.index) })
} }
const [fullMatch, mentionMatch, subMatch] = match const [fullMatch, mentionMatch, subMatch] = match
@ -109,12 +120,12 @@ export default function rehypeSN (options = {}) {
lastIndex = combinedRegex.lastIndex lastIndex = combinedRegex.lastIndex
} }
if (lastIndex < node.value.length) { if (lastIndex < text.length) {
newChildren.push({ type: 'text', value: node.value.slice(lastIndex) }) newChildren.push({ type: 'text', value: text.slice(lastIndex) })
} }
if (newChildren.length > 0) { if (newChildren.length > 0) {
parent.children.splice(index, 1, ...newChildren) parent.children.splice(index, childrenConsumed, ...newChildren)
return index + newChildren.length return index + newChildren.length
} }
} }