同步
This commit is contained in:
@@ -54,8 +54,14 @@ function stripTrailingAtForMention(before) {
|
||||
* 将一个 HTML block 字符串解析为 segments 数组
|
||||
* 处理三种内联元素:mention / linkTag(span) / linkTag(a) / img
|
||||
*/
|
||||
function parseBlockToSegments(block) {
|
||||
function parseBlockToSegments(block, config) {
|
||||
const segs = []
|
||||
const normalize = s => (s || '').trim().toLowerCase()
|
||||
const personTokenSet = new Set()
|
||||
for (const p of ((config && config.persons) || [])) {
|
||||
const token = normalize((p && p.token) || '')
|
||||
if (token) personTokenSet.add(token)
|
||||
}
|
||||
// 合并匹配所有内联元素
|
||||
const tokenRe = /<span[^>]*data-type="mention"[^>]*>[\s\S]*?<\/span>|<span[^>]*data-type="linkTag"[^>]*>[\s\S]*?<\/span>|<a[^>]*href="([^"]*)"[^>]*>(#[^<]*)<\/a>|<img[^>]*\/?>/gi
|
||||
let lastEnd = 0
|
||||
@@ -78,8 +84,12 @@ function parseBlockToSegments(block) {
|
||||
const userId = idMatch ? idMatch[1].trim() : ''
|
||||
let nickname = labelMatch ? labelMatch[1] : innerText.replace(/^[@@]\s*/, '')
|
||||
nickname = cleanMentionNickname((nickname || '').trim())
|
||||
if (userId || nickname) {
|
||||
const userExists = !!normalize(userId) && personTokenSet.has(normalize(userId))
|
||||
if (userExists && nickname) {
|
||||
segs.push({ type: 'mention', userId, nickname, mentionDisplay: '@' + nickname })
|
||||
} else if (nickname) {
|
||||
// 被 @ 人物不存在时降级为普通文本,保持“静态 @某人”展示
|
||||
segs.push({ type: 'text', text: '@' + nickname })
|
||||
}
|
||||
|
||||
} else if (/data-type="linkTag"/i.test(tag)) {
|
||||
@@ -155,7 +165,7 @@ function parseHtmlToSegments(html, config) {
|
||||
for (const block of blocks) {
|
||||
if (!block.trim()) continue
|
||||
|
||||
let blockSegs = parseBlockToSegments(block)
|
||||
let blockSegs = parseBlockToSegments(block, config)
|
||||
if (!blockSegs.length) continue
|
||||
|
||||
// 纯图片行独立成段
|
||||
|
||||
Reference in New Issue
Block a user