Skip to content

Commit

Permalink
Fix links after translation and in tg (#7788)
Browse files Browse the repository at this point in the history
Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
  • Loading branch information
kristina-fefelova authored Jan 24, 2025
1 parent 0958c24 commit 8b10ebc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/text-core/src/markup/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ function addMarks (builder: NodeBuilder, marks: MarkupMark[], next?: () => void)
addMark(builder, mark, () => {
addMarks(builder, others, next)
})
} else {
addMark(builder, mark, next)
}
}
}
Expand Down
37 changes: 31 additions & 6 deletions services/telegram-bot/pod-telegram-bot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8b10ebc

Please sign in to comment.