fix node walking

This commit is contained in:
Eric Bailey
2023-09-26 17:43:01 -05:00
parent 19ec26c9fa
commit 615ecec0cc
@@ -16,34 +16,34 @@
import {Mark} from '@tiptap/core' import {Mark} from '@tiptap/core'
import {Plugin, PluginKey} from '@tiptap/pm/state' import {Plugin, PluginKey} from '@tiptap/pm/state'
import {findChildren} from '@tiptap/core'
import {Node as ProsemirrorNode} from '@tiptap/pm/model' import {Node as ProsemirrorNode} from '@tiptap/pm/model'
import {Decoration, DecorationSet} from '@tiptap/pm/view' import {Decoration, DecorationSet} from '@tiptap/pm/view'
function getDecorations(doc: ProsemirrorNode) { function getDecorations(doc: ProsemirrorNode) {
const decorations: Decoration[] = [] const decorations: Decoration[] = []
findChildren(doc, node => node.type.name === 'paragraph').forEach( doc.descendants((node, pos) => {
paragraphNode => { if (node.isText && node.text) {
const textContent = paragraphNode.node.textContent
const regex = /(?:^|\s)(#[^\d\s]\S*)(?=\s)?/g const regex = /(?:^|\s)(#[^\d\s]\S*)(?=\s)?/g
const textContent = node.textContent
let match let match
while ((match = regex.exec(textContent))) { while ((match = regex.exec(textContent))) {
const [m] = match const [matchedString, tag] = match
const hasLeadingSpace = /^\s/.test(m)
const tag = m.trim().replace(/\p{P}+$/gu, '')
if (tag.length > 66) continue if (tag.length > 66) continue
const from = match.index + (hasLeadingSpace ? 2 : 1)
const to = from + tag.length + 1 const from = match.index + matchedString.indexOf(tag)
const to = from + tag.length
decorations.push( decorations.push(
Decoration.inline(paragraphNode.pos + from, paragraphNode.pos + to, { Decoration.inline(pos + from, pos + to, {
class: 'autolink', class: 'autolink',
}), }),
) )
} }
}, }
) })
return DecorationSet.create(doc, decorations) return DecorationSet.create(doc, decorations)
} }