Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

324
Vistas
Conversión de HTML a texto en NodeJs (fuera del navegador)

¿Cómo convierte html a texto de manera eficiente usando NodeJS, es decir, fuera del navegador? También quiero convertir entidades como ä a ä , etc. y no solo eliminar etiquetas del html.

Aquí hay una prueba de unidad JEST para una función convertHtmlToText que hace esta conversión:

 it('when extract from partial html should extract text', () => { const html = `<p>&nbsp;&auml;&uuml; \t<img alt="" src="http://www.test.org:80/imageupload/userfiles/2/images/world med new - 2022.jpg" style="width: 2000px; height: 1047px; max-width: 100%; height: auto;" /></p> <p> \tAn evening of music, silence and guiding thoughts to help us experience inner peace, connect with the Divine and share loving vibrations with the world. Join millions of people throughout the world to contribute in creating a wave of peace.</p> <div> \t&nbsp;</div> <div> \t<strong>Please join ....</strong></div> <div> \t&nbsp;</div> <div> \t<strong>Watch live:&nbsp;<a href="https://test.org/watchlive" target="_blank">test.org/watchlive</a></strong></div>` const text = convertHtmlToText(html) console.log(text) expect(text).toContain("ä"); expect(text).toContain("ü"); expect.not.stringContaining("<") expect.not.stringContaining(">") });
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Una posible solución para esta pregunta sería usar una biblioteca como, por ejemplo: jsdom

Esta es la función que elimina etiquetas y también convierte entidades de cualquier texto html:

 const jsdom = require("jsdom"); const { JSDOM } = jsdom; const convertHtmlToText = (html) => { if(!html) { return "" } const dom = new JSDOM(html) const textContent = dom.window.document.documentElement.textContent // removing unnecessary spaces return textContent.replace(/\s+/gm, ' ').trim() } module.exports = { convertHtmlToText }
about 4 years ago · Juan Pablo Isaza Denunciar

0

 let HTMLContent = `<div> my&apos; <a href="profile/lol">profile</a></div>`; let strippedHtml = decodeHTMLEntities(HTMLContent.replace(/<[^>]+>/g, '')); console.log(strippedHtml) function decodeHTMLEntities(text) { var entities = [ ['amp', '&'], ['apos', '\''], ['#x27', '\''], ['#x2F', '/'], ['#39', '\''], ['#47', '/'], ['lt', '<'], ['gt', '>'], ['nbsp', ' '], ['quot', '"'] ]; for (var i = 0, max = entities.length; i < max; ++i) { text = text.replace(new RegExp('&' + entities[i][0] + ';', 'g'), entities[i][1]); } return text; }

prueba esto

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda