Fix links after translation and in tg (#7788)

Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
This commit is contained in:
Kristina
2025-01-24 21:56:14 +07:00
committed by GitHub
parent 0958c241cd
commit 8b10ebcf93
2 changed files with 33 additions and 6 deletions
+2
View File
@@ -255,6 +255,8 @@ function addMarks (builder: NodeBuilder, marks: MarkupMark[], next?: () => void)
addMark(builder, mark, () => {
addMarks(builder, others, next)
})
} else {
addMark(builder, mark, next)
}
}
}
@@ -80,11 +80,13 @@ export function platformToTelegram (message: string, limit: number): string {
string,
{
count: number
hasContent: boolean
defaultText: string
}
>()
const parser = new Parser({
onopentag: (tag) => {
onopentag: (tag, attributes) => {
if (tag === 'br' || tag === 'p') {
return
}
@@ -104,18 +106,37 @@ export function platformToTelegram (message: string, limit: number): string {
return
}
let defaultText = ''
if (tag === 'a' && 'href' in attributes) {
newMessage += `<a href="${attributes.href}">`
defaultText = attributes.href
} else {
newMessage += `<${tag}>`
}
openedTags.set(tag, {
count: 1
count: 1,
hasContent: false,
defaultText
})
newMessage += `<${tag}>`
},
ontext: (text) => {
ontext: (rawText) => {
if (textLength >= limit) {
return
}
textLength += unescape(text).length
newMessage += unescape(text)
const text = unescape(rawText)
textLength += text.length
newMessage += text
const lastOpenedTag = Array.from(openedTags.keys()).pop()
if (lastOpenedTag != null) {
const tagData = openedTags.get(lastOpenedTag)
if (tagData != null && text !== '') {
tagData.hasContent = true
}
}
if (textLength > limit) {
const extra = textLength - limit + 1
@@ -148,6 +169,10 @@ export function platformToTelegram (message: string, limit: number): string {
return
}
if (!existingTag.hasContent && existingTag.defaultText !== '') {
newMessage += existingTag.defaultText
}
existingTag.count -= 1
if (existingTag.count <= 0) {