项目遇到的新问题,原有数据是HTML的富文本内容,需要在钉钉上用MD格式显示出来。
找了一下午,在github上找到了一个很有用的包,支持Gradle和Maven安装依赖。
github链接:https://github.com/furstenheim/copy-down
方便懒人使用,简单贴一下使用方法(其实github上作者写的更清晰详细,我主要也是为了自己记录)。
<dependencies>
<dependency>
<groupId>io.github.furstenheim</groupId>
<artifactId>copy_down</artifactId>
<version>1.0</version>
</dependency>
</dependencies>String result = "<p>html富文本字符串</p>";
OptionsBuilder optionsBuilder = OptionsBuilder.anOptions();
Options options = optionsBuilder.withBr("-")
// more options
.build();
CopyDown converter = new CopyDown(options);
String markdown = converter.convert(result);
System.out.println(markdown);贴一下实际效果:
转换前的文本
<p>标题</p><h1>一级标题</h1><h2>二级标题</h2><h3>三级标题</h3><h4>四级标题</h4><h5>五级标题</h5><p>正文</p><p><br></p><p>引用</p><p>A man who stands <a href="http://www.baidu.com" target="_blank" style="background-color: rgb(255, 255, 255);">for</a> nothing will fall <a href="http://www.baidu.com" target="_blank" style="background-color: rgb(255, 255, 255);">for</a> anything.</p><p><br></p><p>文字加粗、斜体</p><p><span style="font-weight: bold;">bold</span></p><p><span style="font-style: italic;">italic</span></p><p><span style="font-style: italic;"><br></span></p><p>链接</p><p><a href="https://www.dingtalk.com" target="_blank">this is a link</a><span style="font-style: italic;"><br></span></p><p><br></p><p>图片</p><p><img src="http://127.0.0.1:9000/public/" style="max-width:100%;"><br></p>转换后的文本
标题
一级标题
====
二级标题
----
### 三级标题
#### 四级标题
##### 五级标题
正文
-
引用
A man who stands [for](http://www.baidu.com) nothing will fall [for](http://www.baidu.com) anything.
-
文字加粗、斜体
bold
italic
-
链接
[this is a link](https://www.dingtalk.com)-
-
图片
-转换前的页面



转换后的页面


别在意图片的问题。斜体和加粗效果没出来,目前还在摸索中,有搞出来的欢迎留言,大家共同学习进步。
版权声明:本文为sbin456原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。