fix: can not choose vars

This commit is contained in:
Joel 2025-09-04 16:46:43 +08:00
parent 84709a7941
commit b1e123c3aa
2 changed files with 31 additions and 26 deletions

View File

@ -188,6 +188,7 @@ const Item: FC<ItemProps> = ({
}
onClick={handleChosen}
onMouseDown={(e) => {
e.preventDefault()
e.stopPropagation()
e.nativeEvent.stopImmediatePropagation()
}}

View File

@ -1,38 +1,42 @@
import React from 'react'
import { visit } from 'unist-util-visit'
export function rehypeVariable() {
return (tree: any) => {
visit(tree, 'text', (node: any, index: number, parent: any) => {
if (!parent || parent.type !== 'element') return
const tag = parent.tagName
if (tag === 'code' || tag === 'pre') return
const iterate = (node: any) => {
console.log(node.type)
}
tree.children.forEach(iterate)
const value: string = node.value
const regex = /\{\{#\$(.+?)#\}\}/g
let m: RegExpExecArray | null
let last = 0
const parts: any[] = []
// visit(tree, 'text', (node: any, index: number, parent: any) => {
// if (!parent || parent.type !== 'element') return
// const tag = parent.tagName
// if (tag === 'code' || tag === 'pre') return
while ((m = regex.exec(value))) {
if (m.index > last)
parts.push({ type: 'text', value: value.slice(last, m.index) })
// const value: string = node.value
// const regex = /\{\{#\$(.+?)#\}\}/g
// let m: RegExpExecArray | null
// let last = 0
// const parts: any[] = []
parts.push({
type: 'element',
tagName: 'variable',
properties: { path: m[1].trim() },
children: [], // 也可放文本 children
})
last = m.index + m[0].length
}
// while ((m = regex.exec(value))) {
// if (m.index > last)
// parts.push({ type: 'text', value: value.slice(last, m.index) })
if (!parts.length) return
if (last < value.length)
parts.push({ type: 'text', value: value.slice(last) })
// parts.push({
// type: 'element',
// tagName: 'variable',
// properties: { path: m[1].trim() },
// children: [],
// })
// last = m.index + m[0].length
// }
parent.children.splice(index, 1, ...parts)
})
// if (!parts.length) return
// if (last < value.length)
// parts.push({ type: 'text', value: value.slice(last) })
// parent.children.splice(index, 1, ...parts)
// })
}
}