From ca519baad446dbb157570678e8e6e8e8993f730e Mon Sep 17 00:00:00 2001 From: Surya Paolo Date: Wed, 18 Sep 2024 13:11:47 +0200 Subject: [PATCH] - Tag HTML sugli annunci non vengono visualizzati correttamente...
...
--- .env.development | 11 ++-- src/server/tools/general.js | 116 ++++++++++++++++++++++++++---------- 2 files changed, 88 insertions(+), 39 deletions(-) diff --git a/.env.development b/.env.development index 7c02111..7d444cb 100644 --- a/.env.development +++ b/.env.development @@ -11,12 +11,14 @@ AUTH_MONGODB=1 MONGODB_USER=admin MONGODB_PWD=mypwadmin@1A ENABLE_PUSHNOTIFICATION=1 +URLBASE_APP1=https://localhost +PORT_APP1=8080 SIGNCODE=SU2134hncaskjdbo231oihJSDNHEI$1@AB HTTPS_LOCALHOST=true DEBUG=0 +DEBUG=0 TESTING_ON=1 LOCALE=1 -PROD=0 DELAY_SENDEMAIL=2000 VAPI_KEY_SUBJECT=mailto:paolo@freeplanet.app PUBLIC_VAPI_KEY=BDncvMiUZmjaCG2Kr1V9N0_33hOG-AuNSbHSvL24y2dzBiUjAxKm02emx5SeJvz2IGmtRf6YqCgopeQwCwUmZw8 @@ -26,6 +28,7 @@ PATH_SERVER_CRT=localhost.crt PATH_SSL_ROOT_PEM=root.pem PATH_SSL_CHAIN_PEM=chain.pem GCM_API_KEY="" +PROD=0 PROJECT_DESCR_MAIN='__PROJECTS' SECRK=Askb38v23jjDFaoskBOWj92axXCQ SECRTK=prova123prova567ASDADASDAS @@ -36,9 +39,5 @@ FTPSERVER_PORT=21 FTPSERVER_USER=ftpusrsrv_ FTPSERVER_PWD=ftpmypwd@1A_ AUTH_NEW_SITES=123123123 -DOMAINS=[{"hostname":"riso.app","port":"3000"}] SCRIPTS_DIR=admin_scripts -CLOUDFLARE_TOKENS=[{"label":"Paolo.arena77@gmail.com","value":"M9EM309v8WFquJKpYgZCw-TViM2wX6vB3wlK6GD0"},{"label":"gruppomacro.com","value":"bqmzGShoX7WqOBzkXocoECyBkPq3GfqcM5t6VFd8"}] -MIAB_HOST=box.lamiaposta.org -MIAB_ADMIN_EMAIL=admin@lamiaposta.org -MIAB_ADMIN_PASSWORD=passpao1pabox@1A \ No newline at end of file +CLOUDFLARE_TOKENS=[{"label":"Paolo.arena77@gmail.com","value":"M9EM309v8WFquJKpYgZCw-TViM2wX6vB3wlK6GD0"},{"label":"gruppomacro.com","value":"bqmzGShoX7WqOBzkXocoECyBkPq3GfqcM5t6VFd8"}] \ No newline at end of file diff --git a/src/server/tools/general.js b/src/server/tools/general.js index a4e5352..4e15943 100755 --- a/src/server/tools/general.js +++ b/src/server/tools/general.js @@ -4735,26 +4735,6 @@ module.exports = { } }, - convertHTMLTagsForTelegram(text) { - const htmlTagsMap = { - '': '', - '': '', - '': '', - '': '', - '': '', - }; - - // Sostituzione dei tag HTML con quelli Markdown supportati da Telegram - let convertedText = text; - for (const htmlTag in htmlTagsMap) { - const regex = new RegExp(htmlTag, 'g'); - convertedText = convertedText.replace(regex, htmlTagsMap[htmlTag]); - } - - return convertedText; - }, - removeFontTags(text) { // Rimozione dei tag e dalla stringa di testo const regex = /]*>|<\/font>/gi; @@ -5042,22 +5022,92 @@ module.exports = { } }, - convertHTMLTagsToText(input) { + convertAndTruncateHTMLForTelegram(text, maxLength = 200, link = '') { const tagMap = { - "
    ": "", - "
      ": "1. ", - "
    1. ": "\n- ", - "
": "", - "": "", - "": "" + "
    ": "", "
      ": "", "
    1. ": "\n• ", "
": "", "": "", "": "", + "
": "\n", "
": "", "
": "\n" }; - // Replace supported HTML tags with their corresponding text representations - let output = input.replace(/
    |
      |
    1. |<\/ul>|<\/ol>|<\/li>/g, (match) => { - return tagMap[match]; + // Rimuoviamo specificamente \n + text = text.replace(/\n<\/b>/g, ''); + + Object.keys(tagMap).forEach(tag => { + const replacement = tagMap[tag]; + text = text.replaceAll(tag, replacement); }); - return output; + text = text.split('\n').map(line => line.trim()).join('\n'); + text = text.replace(/\n{3,}/g, '\n\n').replace(/\n+$/, '').trim(); + + if (text.length <= maxLength) { + return text; + } + + // Tronchiamo il testo + let truncated = text.slice(0, maxLength); + let openTags = []; + + // Troviamo tutti i tag aperti + const regex = /<(\w+)[^>]*>/g; + let match; + while ((match = regex.exec(truncated)) !== null) { + if (!truncated.includes(``, match.index)) { + openTags.push(match[1]); + } + } + + // Rimuoviamo l'ultima parola parziale + truncated = truncated.replace(/\S+$/, ''); + + let strcontinua = ''; + if (link) { + strcontinua = ` (... continua sulla App)`; + } else { + strcontinua = ` (... continua sulla App)`; + } + + // Aggiungiamo '...' e chiudiamo i tag aperti + truncated += strcontinua; + while (openTags.length > 0) { + truncated += ``; + } + + return truncated; + }, + + convertHTMLTagsForTelegram(text) { + const tagMap = { + "
        ": "", + "
          ": "", + "
        1. ": "\n• ", + "
      ": "", + "
    ": "", + "": "", + "
    ": "\n", + "
    ": "", + "
    ": "\n" + }; + + // Rimuoviamo i tag vuoti o contenenti solo spazi bianchi o newline + text = text.replace(/<(b|i|strong|em)>(\s|\n)*<\/\1>/g, ''); + + Object.keys(tagMap).forEach(tag => { + const replacement = tagMap[tag]; + text = text.replaceAll(tag, replacement); + }); + + // Rimuoviamo gli spazi bianchi all'inizio e alla fine di ogni riga + text = text.split('\n').map(line => line.trim()).join('\n'); + + // Rimuoviamo le righe vuote consecutive, lasciandone solo una + text = text.replace(/\n{3,}/g, '\n\n'); + + // Rimuoviamo le righe vuote alla fine del testo + text = text.replace(/\n+$/, ''); + + text = text.replace(/\n<\/b>/g, ''); + + return text.trim(); }, async getAnnuncioForTelegram(myrec, tablerec, mydescr, userorig, nuovo) { @@ -5155,9 +5205,9 @@ module.exports = { if (cat) out += this.addRowTelegram('⭐️', 'Categoria', cat, true); - let note = this.convertHTMLTagsToText(myrec.note) + let descrcontent = this.convertAndTruncateHTMLForTelegram(myrec.note, 500, url) - let descrcontent = this.firstchars(this.removeLastSpaceAndACapo(note), 500, true, url); + // let descrcontent = this.firstchars(this.removeLastSpaceAndACapo(note), 200, true, url); descrcontent = this.cleanHtmlForTelegram(descrcontent);