diff --git a/packages/text-core/src/markup/utils.ts b/packages/text-core/src/markup/utils.ts index 82892734ebf..01e7fa62b9d 100644 --- a/packages/text-core/src/markup/utils.ts +++ b/packages/text-core/src/markup/utils.ts @@ -255,6 +255,8 @@ function addMarks (builder: NodeBuilder, marks: MarkupMark[], next?: () => void) addMark(builder, mark, () => { addMarks(builder, others, next) }) + } else { + addMark(builder, mark, next) } } } diff --git a/services/telegram-bot/pod-telegram-bot/src/utils.ts b/services/telegram-bot/pod-telegram-bot/src/utils.ts index f3eca3c2db7..cdd754eb031 100644 --- a/services/telegram-bot/pod-telegram-bot/src/utils.ts +++ b/services/telegram-bot/pod-telegram-bot/src/utils.ts @@ -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 += `` + 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) {