Bilibili-Evolved/registry/lib/components/utils/comments/content-replace/handlers/text-to-text.ts
2024-10-20 10:45:21 +08:00

21 lines
659 B
TypeScript

import { NodeContentReplacer } from './node-content-replacer'
export class TextToTextReplacer extends NodeContentReplacer {
isKeywordMatch(node: Node, keyword: string) {
if (node instanceof Text) {
return node.textContent.includes(keyword)
}
return false
}
replaceContent(node: Node, keyword: string, target: string): Node[] {
const index = node.textContent.indexOf(keyword)
if (index === -1) {
return []
}
const leftPart = node.textContent.substring(0, index)
const rightPart = node.textContent.substring(index + keyword.length)
node.textContent = `${leftPart}${target}${rightPart}`
return []
}
}