From 353e7b8849fa6d76927c695f2bc085e0b44e22f2 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 25 Oct 2024 07:27:27 +0300 Subject: vnext: fix message formatting for Telegram --- vnext/src/utils/embed.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'vnext/src/utils') diff --git a/vnext/src/utils/embed.js b/vnext/src/utils/embed.js index 6a5c9c95..b6bbde14 100644 --- a/vnext/src/utils/embed.js +++ b/vnext/src/utils/embed.js @@ -122,27 +122,35 @@ function messageReplyReplace(messageId) { * @param {string} txt text message * @param {string} messageId current message id * @param {boolean} isCode set when message contains *code tag + * @param {boolean} isDurov skip rules non-compatible with Telegram * @returns {string} formatted message */ -function juickFormat(txt, messageId, isCode) { +function juickFormat(txt, messageId, isCode, isDurov) { const urlRe = /(?:\[([^\][]+)\](?:\[([^\]]+)\]|\(((?:[a-z]+:\/\/|www\.|ftp\.)(?:\([-\S+*&@#/%=~|$?!:;,.]*\)|[-\S+*&@#/%=~|$?!:;,.])*(?:\([-\S+*&@#/%=~|$?!:;,.]*\)|[\S+*&@#/%=~|$]))\))|\b(?:[a-z]+:\/\/|www\.|ftp\.)(?:\([-\S+*&@#/%=~|$?!:;,.]*\)|[-\S+*&@#/%=~|$?!:;,.])*(?:\([-\S+*&@#/%=~|$?!:;,.]*\)|[\S+*&@#/%=~|$]))/gi const bqReplace = m => m.replace(/^(?:>|>)\s?/gmi, '') - return (isCode) - ? formatText(txt, [ + if (isCode) { + return formatText(txt, [ { pr: 1, re: urlRe, with: urlReplaceInCode }, { pr: 1, re: /\B(?:#(\d+))?(?:\/(\d+))?\b/g, with: messageReplyReplace(messageId) }, { pr: 1, re: /\B@([\w-]+)\b/gi, with: '@$1' }, ]) - : formatText(txt, [ - { pr: 0, re: /((?:^(?:>|>)\s?[\s\S]+?$\n?)+)/gmi, brackets: true, with: ['', '', bqReplace] }, + } else { + const rules = [ + { pr: 0, re: /((?:^(?:>|>)\s?[\s\S]+?$\n?)+)/gmi, brackets: true, with: ['
', '
', bqReplace] }, { pr: 1, re: urlRe, with: urlReplace }, { pr: 1, re: /\B(?:#(\d+))?(?:\/(\d+))?\b/g, with: messageReplyReplace(messageId) }, { pr: 1, re: /\B@([\w-]+)\b/gi, with: '@$1' }, { pr: 2, re: /\B\*([^\n]+?)\*((?=\s)|(?=$)|(?=[!"#$%&'*+,\-./:;<=>?@[\]^_`{|}~()]+))/g, brackets: true, with: ['', ''] }, { pr: 2, re: /\B\/([^\n]+?)\/((?=\s)|(?=$)|(?=[!"#$%&'*+,\-./:;<=>?@[\]^_`{|}~()]+))/g, brackets: true, with: ['', ''] }, { pr: 2, re: /\b_([^\n]+?)_((?=\s)|(?=$)|(?=[!"#$%&'*+,\-./:;<=>?@[\]^_`{|}~()]+))/g, brackets: true, with: ['', ''] }, - { pr: 3, re: /\n/g, with: '
' }, - ]) + ] + if (!isDurov) { + rules.push( + { pr: 3, re: /\n/g, with: '
' } + ) + } + return formatText(txt, rules) + } } /** * @external RegExpExecArray -- cgit v1.2.3