mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
43 lines
1.7 KiB
TypeScript
43 lines
1.7 KiB
TypeScript
import { registerAndGetData } from '@/plugins/data'
|
|
import { getComponentSettings } from '@/core/settings'
|
|
import { CommentContentReplaceHandler } from './types'
|
|
import { CommentContentReplaceOptions } from '../options'
|
|
import { NodeContentReplacer } from './node-content-replacer'
|
|
import { EmoticonToEmoticonReplacer } from './emoticon-to-emoticon'
|
|
import { EmoticonToTextReplacer } from './emoticon-to-text'
|
|
import { RecursiveReplacer } from './recursive'
|
|
import { TextToEmoticonReplacer } from './text-to-emoticon'
|
|
import { TextToTextReplacer } from './text-to-text'
|
|
|
|
const { options } = getComponentSettings<CommentContentReplaceOptions>('commentContentReplace')
|
|
|
|
const contentReplacers: NodeContentReplacer[] = [
|
|
new TextToEmoticonReplacer(),
|
|
new EmoticonToEmoticonReplacer(),
|
|
new TextToTextReplacer(),
|
|
new EmoticonToTextReplacer(),
|
|
new RecursiveReplacer(),
|
|
]
|
|
|
|
export const CommentContentReplaceMap = 'commentContentReplace.map'
|
|
const defaultHandler: CommentContentReplaceHandler = content => {
|
|
const { replaceMap } = options
|
|
const [finalReplaceMap] = registerAndGetData(CommentContentReplaceMap, replaceMap)
|
|
content.forEach(node => {
|
|
Object.entries(finalReplaceMap).forEach(([from, to]) => {
|
|
if (from === to || from === '') {
|
|
return
|
|
}
|
|
const replacer = contentReplacers.find(r => r.isKeywordMatch(node, from, to))
|
|
if (!replacer) {
|
|
return
|
|
}
|
|
const restParts = replacer.replaceContent(node, from, to)
|
|
defaultHandler(restParts)
|
|
})
|
|
})
|
|
}
|
|
|
|
export const CommentContentReplaceHandlers = 'commentContentReplace.handlers'
|
|
export const handlers = registerAndGetData(CommentContentReplaceHandlers, [defaultHandler])
|