gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:54:47 25 4
gpt4 key购买 nike

我正在尝试将以下 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;

什么是正确的做法?

非常感谢,

凯蒂

最佳答案

您可以使用 Range保持 html 结构

要获取特定长度的 html,请调用下面的 htmlToLength(html, length)

您还可以通过调用 htmlToNodeWithLength(html, length) 获取 DocumentFragment。

该算法的核心是findRangeWithLength(range, length),它从末尾(递归)缩小range,直到它具有给定的长度

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>

关于reactjs - 你如何在 React 中截断 html 文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65627253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com