【发布时间】:2021-06-27 13:32:46
【问题描述】:
我使用 react 和 redux 创建了一个博客 Web 应用程序,在我的博客中有 4 个字段 id、title、seoName 和 description,对于在描述中的输入,我使用的是微型 Mce 文本编辑器 现在我想显示保存在 HTML 中的 Blog 类的描述字段中的文本 并使用椭圆截断它
这是存储在数据库中
{
"_id" : ObjectId("60640e75f7bafb14f6563d52"),
"title" : "Improve Your Developer Experience With Nuxt Components",
"seoName" : "improve-your-developer-experience-with-nuxt-components",
"description" : "<p><img src=\"https://webconnect-upload.s3.amazonaws.com/160x00u0xj1ny179o3a1fd07kl26/screenshot-from-2021-03-31-11-22-39.png\" alt=\"\" width=\"802\" height=\"274\" /></p>\n<h2 id=\"introduction\">Introduction</h2>\n<p>The Nuxt team has introduced <strong><a href=\"https://www.npmjs.com/package/@nuxt/components\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">@nuxt/components</a></strong> module with the purpose to make Nuxt development faster and to make you, as a developer, more productive. This module comes with amazing features and options that will improve your development experience with Nuxt. No matter if you’re just starting out or an advanced user, <a href=\"https://github.com/nuxt/components\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">@nuxt/components</a> provides a range of options from the simplest setup to advance configurations that will certainly benefit your projects.</p>\n<p>In a nutshell, this module automatically scans, imports and registers Vue components found in the <strong><code>~/components</code></strong> directory, so that we don't have to write import statements when we use them in either pages, layouts or even within components.</p>\n<p> </p>\n<blockquote>\n<p><em>This module parses your template and automatically includes the component in the file where you are using it such as a page, layout or even a component. Because Nuxt.js uses automatic code splitting to split your pages by default this module works perfect as it will only contain the components that are used on that page. Also, if you use a component in more than 2 pages, Nuxt.js will automatically create a shared chunk for them thanks to the magic of WebPack.</em></p>\n</blockquote>",
}
我只想显示截断的文本(从描述中删除图像) 其余内容请点击阅读更多。
现在我正在使用 建议有没有其他有效的方法
<p className="m-0px truncateBlogDesc"
dangerouslySetInnerHTML={{ __html: e.description }}
></p>
【问题讨论】:
-
我知道这不是您问题的答案。但是,你为什么不直接用 css 隐藏 truncateBlogDesc 中的
标签呢?
-
您想要更高效还是更安全的东西? (或两者兼有?)如果您觉得使用
dangerouslySetInnerHTML不安全,请不要使用它。你能澄清你在找什么吗?如果担心安全问题,您可以查看DOMPurify。听起来您还想截断原始 HTML 字符串。为此,您需要处理字符串并保留处理的标签堆栈,因此如果您在打开的标签中间截断,您可以关闭它。 -
我用 DomPurify 解决了这个问题,谢谢 Drew :-) 另外请看这个问题@DrewReese stackoverflow.com/questions/66963413/…
标签: javascript reactjs innerhtml dangerouslysetinnerhtml