fix cleanHtmlForTelegram (post di Claudio)
This commit is contained in:
@@ -7,6 +7,8 @@ require('../models/subscribers');
|
||||
|
||||
const printf = require('util').format;
|
||||
|
||||
const { JSDOM } = require('jsdom');
|
||||
|
||||
const axios = require('axios');
|
||||
|
||||
const CryptoJS = require('crypto-js');
|
||||
@@ -4420,6 +4422,10 @@ module.exports = {
|
||||
value = this.removeSpanAndDivTags(value);
|
||||
truncatedValue = this.truncateString(value, numchars);
|
||||
|
||||
if (str.length < maxLength) {
|
||||
continua = false;
|
||||
}
|
||||
|
||||
// Aggiungi il testo aggiuntivo per indicare il troncamento
|
||||
if (continua) {
|
||||
if (link) {
|
||||
@@ -4861,6 +4867,8 @@ module.exports = {
|
||||
|
||||
let descrcontent = this.firstchars(this.removeLastSpaceAndACapo(note), 500, true, url);
|
||||
|
||||
descrcontent = this.cleanHtmlForTelegram(descrcontent);
|
||||
|
||||
// descrcontent = '<span size="3"><b>Prova Pao</b> Ciaooo</span>';
|
||||
|
||||
if (descrcontent)
|
||||
@@ -5111,5 +5119,51 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
cleanHtmlForTelegram(htmlContent) {
|
||||
try {
|
||||
// Assicurati che l'HTML abbia un elemento body
|
||||
const dom = new JSDOM(`<body>${htmlContent}</body>`);
|
||||
const document = dom.window.document;
|
||||
|
||||
// Definisci i tag permessi su Telegram
|
||||
const allowedTags = ['b', 'strong', 'i', 'em', 'u', 's', 'strike', 'del', 'a', 'code'];
|
||||
|
||||
// Seleziona tutti gli elementi all'interno del body
|
||||
const allElements = document.body.querySelectorAll('*');
|
||||
|
||||
allElements.forEach(element => {
|
||||
const tagName = element.tagName.toLowerCase();
|
||||
|
||||
if (!allowedTags.includes(tagName)) {
|
||||
// Crea un nodo di testo e sostituisci l'elemento con quel nodo di testo
|
||||
const textNode = document.createTextNode(element.textContent || '');
|
||||
element.replaceWith(textNode);
|
||||
} else {
|
||||
// Se il tag è permesso, rimuovi tutti gli attributi ad eccezione di 'href' per i link
|
||||
if (tagName === 'a') {
|
||||
const href = element.getAttribute('href');
|
||||
while (element.attributes.length > 0) {
|
||||
element.removeAttribute(element.attributes[0].name);
|
||||
}
|
||||
if (href) {
|
||||
element.setAttribute('href', href);
|
||||
}
|
||||
} else {
|
||||
while (element.attributes.length > 0) {
|
||||
element.removeAttribute(element.attributes[0].name);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Ritorna l'HTML pulito
|
||||
return document.body.innerHTML;
|
||||
} catch (error) {
|
||||
console.error('Errore durante la pulizia dell\'HTML:', error);
|
||||
throw error; // Oppure, gestisci l'errore come preferisci
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Scriviere qui SOPRA le funzioni
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user