【问题标题】:Converting HTML to text in NodeJs (outside of the browser)在 NodeJs 中将 HTML 转换为文本(在浏览器之外)
【发布时间】:2022-02-18 19:33:33
【问题描述】:

如何使用 NodeJS 有效地将 html 转换为文本,即在浏览器之外?我还想将 ä 等实体转换为 ä 等,而不仅仅是从 html 中删除标签。

这是一个函数 convertHtmlToText 的 JEST 单元测试,它执行此转换:

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(">")
});

【问题讨论】:

    标签: javascript html node.js


    【解决方案1】:

    这个问题的一个可能的解决方案是使用一个库,例如:jsdom

    这是删除标签并从任何 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
    }
    

    【讨论】:

      【解决方案2】:

      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;
      }

      试试这个

      【讨论】:

      • 您好,这还不错,但我也希望像&amp;nbsp;&amp;auml;&amp;uuml 这样的实体能够正确转换为文本。
      • 抱歉语法已修复,试试这个。谢谢
      • That's very fragile(例如,如果属性值包含&gt;,它将中断),支持的实体列表非常短,并且不能正确处理空格。
      • @Quentin 能给他提供更好的解决方案吗?他使用的是 node.js,而不是有 DOM 可操作的浏览器 js
      • @SegunAdeniji — gil.fernandes 已经拥有
      猜你喜欢
      • 1970-01-01
      • 2020-03-31
      • 2013-05-20
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      • 1970-01-01
      • 2012-10-30
      相关资源
      最近更新 更多