你如何在 React 中截断 html 文本?

我正在尝试将以下 html 截断为 5 个字符:

<b>Hello</b> how are you today?

我正在寻找的结果是:

你好

但是,如何忽略截断中的 html 标签,使其不会导致以下结果?

<b>He

我正在使用一个 html 解析器,所以在使用它之后我不能截断字符串。这是我的代码,不用说,它不起作用!

import React from 'react';
import parse from 'html-react-parser';
import Typography from '@material-ui/core/Typography';

const Message= () => {
  const message= "<b>Hello</b> how are you today?"
  const messageParsed = parse(message);

  return (
          <Typography variant="body2">
            {messageParsed.substr(0, 5)}
          </Typography>
  );
};

export default Message;

什么是正确的方法?

非常感谢,

凯蒂

stack overflow How do you truncate html text in React?
原文答案

答案:

作者头像

You can keep the html structure by using Ranges.

To get the html with a certain length, call htmlToLength(html, length) below.

You can also get a DocumentFragment by calling htmlToNodeWithLength(html, length).

The heart of this algorithm is findRangeWithLength(range, length), which shrinks the range from the end (recursively), until it has the given length.

function htmlToLength(html, length) {
  const trimmedNode = htmlToNodeWithLength(html, length);

  const container = document.createElement("div");
  container.appendChild(trimmedNode);
  return container.innerHTML;
}

function htmlToNodeWithLength(html, length) {
  // Only for measurement. Never added to DOM.
  const container = document.createElement("div");
  container.innerHTML = html;

  const fullRange = document.createRange();
  fullRange.setStart(container, 0);
  fullRange.setEnd(container, 1);

  const range = findRangeWithLength(fullRange, length);
  return range.cloneContents();
}

function findRangeWithLength(range, length) {
  if (rangeLength(range) < length) return range;

  // Find the childNode with at least length content.
  for (const childNode of range.endContainer.childNodes) {
    range.setEnd(childNode, lastEndOffset(childNode));
    if (rangeLength(range) >= length) {
      return findRangeWithLength(range, length);
    }
  }

  // There are no child nodes long enough. It's a text node.
  const diff = length - rangeLength(range) + range.endOffset;
  range.setEnd(range.endContainer, diff);
  return range;
}

function lastEndOffset(node) {
  return node.childNodes.length || node.textContent.length;
}

function rangeLength(range) {
  return range.toString().length;
}

const html = "<p>No <span></span><b>Hello</b> <i>World</i></p>";
const length = 7;
const trimmedNode = htmlToNodeWithLength(html, length);

document.querySelector(".raw-input").textContent = html;
document.querySelector(".trimmed").appendChild(trimmedNode);
document.querySelector(".length").textContent = length;
document.querySelector(".input").innerHTML = html;
document.querySelector(".raw-output").textContent = htmlToLength(html, length);
<h2>Raw HTML input</h2>
<div class="raw-input"></div>
<h2>Rendered Input</h2>
<div class="input"></div>
<h2>Rendered output to length <span class="length">?</span></h2>
<div class="trimmed"></div>
<h2>Raw HTML output</h2>
<div class="raw-output"></div>

作者头像

what I understand is that you have an HTML text and the following operations are required

  1. Remove the HTML tags
  2. Truncate the string after step 1 to 5 characters.

In that case, it can be done using a regular expression.

import React from "react";
import "./styles.css";

export default function App() {
  const message = "<b>Hello</b> how are you today?";
  const messageString = message.replace(/<(.|\n)*?>/g, '');

  const subText = messageString.substring(0, 5);

  return <div>{subText}</div>;
}

Check here for a working sample.

Check out this & this link to know more about how to remove HTML tags from string in JS.

About the package html-react-parser this does not return the string hence the substring method will not work. It returns an array. In your case the line parse(<b>Hello</b> how are you today?); will return the following enter image description here

作者头像

One option is that you could use _truncate from lodash/truncate like so:

import _truncate from "lodash/truncate";
import ReactHtmlParser from "react-html-parser";

function ParsedText() {
  var text = "<b>Hello</b> how are you today?";


  return (
    <div> 
      {ReactHtmlParser(_truncate(text, { length: 5 }))}
    </div>
  )
作者头像

If you don't want truncate by character size, so below can help you

  • For 1 line:
p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
  • For multiple line (ex: 3)
p {
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
}

or

p {
  height: 4.5rem;
  line-height: 1.5rem;
  overflow: hidden;
}

If you want to truncate by character size

div {
  /* <div><p></p></div> */
  width: '50px'; /* equal pixels to characters width */
  overflow: hidden;
}