html字符串转换jsx,javascript - Reactjs convert html string to jsx - Stack Overflow

I recommend using Interweave created by milesj. Its a phenomenal library that makes use of a number if ingenious techniques to parse and safely insert HTML into the DOM.

Interweave is a react library to safely render HTML, filter

attributes, autowrap text with matchers, render emoji characters, and

much more.

Interweave is a robust React library that can:

Safely render HTML without using dangerouslySetInnerHTML.

Safely strip HTML tags.

Automatic XSS and injection protection.

Clean HTML attributes using filters.

Interpolate components using matchers.

Autolink URLs, IPs, emails, and hashtags.

Render Emoji and emoticon characters.

And much more!

Usage Example:

import React from 'react';

import { Markup } from 'interweave';

const articleContent = "

Lorem ipsum dolor laboriosam.

Facere debitis impedit doloremque eveniet eligendi reiciendis ratione obcaecati repellendus culpa? Blanditiis enim cum tenetur non rem, atque, earum quis, reprehenderit accusantium iure quas beatae.

Lorem ipsum dolor sit amet this is a link, click me Sunt ducimus corrupti? Eveniet velit numquam deleniti, delectus

  1. reiciendis ratione obcaecati
  2. repellendus culpa? Blanditiis enim
  3. cum tenetur non rem, atque, earum quis,
reprehenderit accusantium iure quas beatae."

// this will take the articleContent string and convert it to HTML markup. See: https://milesj.gitbook.io/interweave

//to install package using npm, execute the command

npm install interweave

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值