Java 操作 Markdown(2)--flexmark-java 使用
<p>flexmark-java 是一个基于 Java 实现的 CommonMark (spec 0.28) 解析器,它采用了“先块后内联”的 Markdown 解析架构;本文主要介绍其基本使用,文中所使用到的软件版本:Java 17.0.5、flexmark 0.64.8。</p><h2>1、简介</h2>
<p>flexmark-java 的优势在于速度快、灵活性高、基于 Markdown 源元素的抽象语法树(AST)能够精确到组成元素的单个字符词素的源位置,以及强大的可扩展性。</p>
<p>flexmark-java 允许对解析过程进行细粒度控制,并针对安装大量扩展的解析场景进行了优化。解析器和扩展都提供了丰富的选项,用于调整解析器行为和 HTML 渲染变体。最终目标是让解析器和渲染器能够以极高的精度模拟其他解析器。现在,通过实现 “Markdown 处理器仿真” 功能,这一目标已部分完成。</p>
<div>该项目的动机是为了替换 JetBrains IDEs 的 Markdown Navigator 插件中使用的 pegdown 解析器。pegdown 的功能集非常强大,但其整体速度并不理想,并且在遇到病态输入时,解析过程会挂起或实际上处于挂起状态。</div>
<p>对于 0.62.2 及以下版本:需要 Java 8+;对于 0.64.0 及以上版本:需要 Java 11+。</p>
<h2>2、flexmark-java 使用</h2>
<div>
<h3>2.1、样例文件</h3>
<div>解析 Markdown 时使用该样例文件。</div>
<div>
<div class="cnblogs_code"><img src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" id="code_img_closed_359a1cfe-4ed0-4dfa-84f1-0e21597d73a3" class="code_img_closed"><img src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" id="code_img_opened_359a1cfe-4ed0-4dfa-84f1-0e21597d73a3" class="code_img_opened" style="display: none">
<div id="cnblogs_code_open_359a1cfe-4ed0-4dfa-84f1-0e21597d73a3" class="cnblogs_code_hide">
<pre>## 1 Markdown语法教程
### 1.1 标题
不同数量的`#`可以完成不同的标题,如下:
# 一级标题
## 二级标题
### 三级标题
### 1.2 字体
粗体、斜体、粗体和斜体,删除线,需要在文字前后加不同的标记符号。如下:
**这个是粗体**
*这个是斜体*
***这个是粗体加斜体***
~~这里是删除线~~
注:如果想给字体换颜色、字体或者居中显示,需要使用内嵌HTML来实现。
### 1.3 无序列表
无序列表的使用,在符号`-`后加空格使用。如下:
- 无序列表 1
- 无序列表 2
- 无序列表 3
如果要控制列表的层级,则需要在符号`-`前使用空格。如下:
- 无序列表 1
- 无序列表 2
- 无序列表 2.1
- 无序列表 2.2
### 1.4 有序列表
有序列表的使用,在数字及符号`.`后加空格后输入内容,如下:
1. 有序列表 1
2. 有序列表 2
3. 有序列表 3
### 1.5 引用
引用的格式是在符号`>`后面书写文字。如下:
> 读一本好书,就是在和高尚的人谈话。 ——歌德
> 雇用制度对工人不利,但工人根本无力摆脱这个制度。 ——阮一峰
### 1.7 链接
微信公众号仅支持公众号文章链接,即域名为`https://mp.weixin.qq.com/`的合法链接。使用方法如下所示:
对于该论述,欢迎读者查阅之前发过的文章,[你是《未来世界的幸存者》么?](https://mp.weixin.qq.com/s/s5IhxV2ooX3JN_X416nidA)
### 1.8 图片
插入图片,格式如下:

支持 jpg、png、gif、svg 等图片格式,**其中 svg 文件仅可在微信公众平台中使用**,svg 文件示例如下:

支持图片**拖拽和截图粘贴**到编辑器中。
注:支持图片 ***拖拽和截图粘贴*** 到编辑器中,仅支持 https 的图片,图片粘贴到微信时会自动上传微信服务器。
### 1.9 分割线
可以在一行中用三个以上的减号来建立一个分隔线,同时需要在分隔线的上面空一行。如下:
---
### 1.10 表格
可以使用冒号来定义表格的对齐方式,如下:
| 姓名 | 年龄 | 工作 |
| :----- | :--: | -------: |
| 小可爱 |18| 吃可爱多 |
| 小小勇敢 |20| 爬棵勇敢树 |
| 小小小机智 |22| 看一本机智书 |</pre>
</div>
<span class="cnblogs_code_collapse">test.md</span></div>
<h3>2.2、引入依赖</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span>com.vladsch.flexmark<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span>flexmark-all<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">></span>0.64.8<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span></pre>
</div>
<h3>2.3、API 方式生成 HTML 或 Markdown 文档</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> serialize() {
Document document </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Document(<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">, BasedSequence.EMPTY);
Heading heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>2<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1 Markdown语法教程"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.1 标题"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
Paragraph paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("不同数量的"<span style="color: rgba(0, 0, 0, 1)">));
Code code </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Code();
code.setText(BasedSequence.of(</span>"#"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(code);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("可以完成不同的标题,如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>1<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("一级标题"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>2<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("二级标题"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("三级标题"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.2 字体"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("粗体、斜体、粗体和斜体,删除线,需要在文字前后加不同的标记符号。如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
StrongEmphasis strongEmphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StrongEmphasis();
strongEmphasis.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("这个是粗体"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(strongEmphasis);
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
Emphasis emphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Emphasis();
emphasis.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("这个是斜体"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(emphasis);
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
strongEmphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StrongEmphasis();
emphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Emphasis();
emphasis.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("这个是粗体加斜体"<span style="color: rgba(0, 0, 0, 1)">));
strongEmphasis.appendChild(emphasis);
paragraph.appendChild(strongEmphasis);
document.appendChild(paragraph);
Strikethrough strikethrough </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Strikethrough();
strikethrough.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("这里是删除线"<span style="color: rgba(0, 0, 0, 1)">));
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(strikethrough);
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("注:如果想给字体换颜色、字体或者居中显示,需要使用内嵌HTML来实现。"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.3 无序列表"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表的使用,在符号"<span style="color: rgba(0, 0, 0, 1)">));
code </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Code();
code.setText(BasedSequence.of(</span>"-"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(code);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("后加空格使用。如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
BulletList bulletList </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletList();
BulletListItem bulletListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletListItem();
bulletListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 1"<span style="color: rgba(0, 0, 0, 1)">));
bulletList.appendChild(bulletListItem);
bulletListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletListItem();
bulletListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 2"<span style="color: rgba(0, 0, 0, 1)">));
bulletList.appendChild(bulletListItem);
bulletListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletListItem();
bulletListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 3"<span style="color: rgba(0, 0, 0, 1)">));
bulletList.appendChild(bulletListItem);
document.appendChild(bulletList);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("如果要控制列表的层级,则需要在符号"<span style="color: rgba(0, 0, 0, 1)">));
code </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Code();
code.setText(BasedSequence.of(</span>"-"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(code);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("前使用空格。如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
bulletList </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletList();
bulletListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletListItem();
bulletListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 1"<span style="color: rgba(0, 0, 0, 1)">));
bulletList.appendChild(bulletListItem);
bulletListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletListItem();
bulletListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 2"<span style="color: rgba(0, 0, 0, 1)">));
bulletList.appendChild(bulletListItem);
BulletList bulletList2 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletList();
bulletListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletListItem();
bulletListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 2.1"<span style="color: rgba(0, 0, 0, 1)">));
bulletList2.appendChild(bulletListItem);
bulletListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletListItem();
bulletListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 2.2"<span style="color: rgba(0, 0, 0, 1)">));
bulletList2.appendChild(bulletListItem);
bulletList.appendChild(bulletList2);
document.appendChild(bulletList);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.4 有序列表"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("有序列表的使用,在数字及符号"<span style="color: rgba(0, 0, 0, 1)">));
code </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Code();
code.setText(BasedSequence.of(</span>"."<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(code);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("后加空格后输入内容,如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
OrderedList orderedList </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> OrderedList();
orderedList.setStartNumber(</span>1<span style="color: rgba(0, 0, 0, 1)">);
OrderedListItem orderedListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> OrderedListItem();
orderedListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("有序列表 1"<span style="color: rgba(0, 0, 0, 1)">));
orderedList.appendChild(orderedListItem);
orderedListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> OrderedListItem();
orderedListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("有序列表 2"<span style="color: rgba(0, 0, 0, 1)">));
orderedList.appendChild(orderedListItem);
orderedListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> OrderedListItem();
orderedListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("有序列表 3"<span style="color: rgba(0, 0, 0, 1)">));
orderedList.appendChild(orderedListItem);
document.appendChild(orderedList);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.5 引用"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("引用的格式是在符号"<span style="color: rgba(0, 0, 0, 1)">));
code </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Code();
code.setText(BasedSequence.of(</span>">"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(code);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("后面书写文字。如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text(" "<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
BlockQuote blockQuote </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BlockQuote();
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("读一本好书,就是在和高尚的人谈话。 ——歌德"<span style="color: rgba(0, 0, 0, 1)">));
blockQuote.appendChild(paragraph);
document.appendChild(blockQuote);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text(" "<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
blockQuote </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BlockQuote();
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("雇用制度对工人不利,但工人根本无力摆脱这个制度。 ——阮一峰"<span style="color: rgba(0, 0, 0, 1)">));
blockQuote.appendChild(paragraph);
document.appendChild(blockQuote);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.7 链接"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("微信公众号仅支持公众号文章链接,即域名为"<span style="color: rgba(0, 0, 0, 1)">));
code </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Code();
code.setText(BasedSequence.of(</span>"https://mp.weixin.qq.com/"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(code);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("的合法链接。使用方法如下所示:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
Link link </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Link();
link.setUrl(BasedSequence.of(</span>"https://mp.weixin.qq.com/s/s5IhxV2ooX3JN_X416nidA"<span style="color: rgba(0, 0, 0, 1)">));
link.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("你是《未来世界的幸存者》么?"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("对于该论述,欢迎读者查阅之前发过的文章,"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(link);
document.appendChild(paragraph);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.8 图片"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("插入图片,格式如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
Image image </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Image();
image.setUrl(BasedSequence.of(</span>"https://markdown.com.cn/images/qrcode_for_gh_82cf87d482f0_258.jpg"<span style="color: rgba(0, 0, 0, 1)">));
image.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("这里写图片描述"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(image);
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("支持 jpg、png、gif、svg 等图片格式,"<span style="color: rgba(0, 0, 0, 1)">));
strongEmphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StrongEmphasis();
strongEmphasis.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("其中 svg 文件仅可在微信公众平台中使用"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(strongEmphasis);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text(",svg 文件示例如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
image </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Image();
image.setUrl(BasedSequence.of(</span>"https://markdown.com.cn/images/i-am-svg.svg"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(image);
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("支持图片"<span style="color: rgba(0, 0, 0, 1)">));
strongEmphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StrongEmphasis();
strongEmphasis.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("拖拽和截图粘贴"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(strongEmphasis);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("到编辑器中。"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("注:支持图片 "<span style="color: rgba(0, 0, 0, 1)">));
strongEmphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StrongEmphasis();
emphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Emphasis();
emphasis.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("拖拽和截图粘贴"<span style="color: rgba(0, 0, 0, 1)">));
strongEmphasis.appendChild(emphasis);
paragraph.appendChild(strongEmphasis);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text(" 到编辑器中,仅支持 https 的图片,图片粘贴到微信时会自动上传微信服务器。"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.9 分割线"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("可以在一行中用三个以上的减号来建立一个分隔线,同时需要在分隔线的上面空一行。如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
ThematicBreak thematicBreak </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ThematicBreak();
document.appendChild(thematicBreak);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.10 表格"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("可以使用冒号来定义表格的对齐方式,如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
TableBlock tableBlock </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableBlock();
TableHead tableHead </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableHead();
TableRow tableRow </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableRow();
TableCell tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("姓名"<span style="color: rgba(0, 0, 0, 1)">));
tableCell.setAlignment(TableCell.Alignment.LEFT);
tableRow.appendChild(tableCell);
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("年龄"<span style="color: rgba(0, 0, 0, 1)">));
tableCell.setAlignment(TableCell.Alignment.CENTER);
tableRow.appendChild(tableCell);
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("工作"<span style="color: rgba(0, 0, 0, 1)">));
tableCell.setAlignment(TableCell.Alignment.RIGHT);
tableRow.appendChild(tableCell);
tableHead.appendChild(tableRow);
TableBody tableBody </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableBody();
tableRow </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableRow();
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("小可爱"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("18"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("吃可爱多"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableBody.appendChild(tableRow);
tableRow </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableRow();
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("小小勇敢"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("20"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("爬棵勇敢树"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableBody.appendChild(tableRow);
tableRow </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableRow();
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("小小小机智"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("22"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("看一本机智书"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableBody.appendChild(tableRow);
tableBlock.appendChild(tableHead);
tableBlock.appendChild(tableBody);
document.appendChild(tableBlock);
MutableDataSet options </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MutableDataSet();
options.set(Parser.EXTENSIONS, Arrays.asList(
StrikethroughExtension.create(), </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除线扩展</span>
TablesExtension.create() <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">表格扩展</span>
<span style="color: rgba(0, 0, 0, 1)"> ));
HtmlRenderer htmlRenderer </span>=<span style="color: rgba(0, 0, 0, 1)"> HtmlRenderer.builder(options).build();
String html </span>=<span style="color: rgba(0, 0, 0, 1)"> htmlRenderer.render(document);
System.out.println(html);
MutableDataSet options2 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MutableDataSet();
options2.set(FlexmarkHtmlConverter.SETEXT_HEADINGS, </span><span style="color: rgba(0, 0, 255, 1)">false</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">标题是否使用下划线格式</span>
options2.set(FlexmarkHtmlConverter.SKIP_CHAR_ESCAPE, <span style="color: rgba(0, 0, 255, 1)">true</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">是否跳过转义符</span>
options2.set(FlexmarkHtmlConverter.UNORDERED_LIST_DELIMITER, '-');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">无序列表的符号</span>
options2.set(FlexmarkHtmlConverter.TYPOGRAPHIC_SMARTS, <span style="color: rgba(0, 0, 255, 1)">false</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">是否启用智能排版,它会处理文本中的一些常见的标点符号,并将其转换为更符合排版习惯的字符</span>
options2.set(FlexmarkHtmlConverter.THEMATIC_BREAK, "---"<span style="color: rgba(0, 0, 0, 1)">);
String markdown </span>=<span style="color: rgba(0, 0, 0, 1)"> FlexmarkHtmlConverter.builder(options2).build().convert(html);
System.out.println(markdown);
}</span></pre>
</div>
<h3>2.4、解析 Markdown 文档</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> deserialize() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException {
MutableDataSet options </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MutableDataSet();
options.set(Parser.EXTENSIONS, Arrays.asList(
StrikethroughExtension.create(), </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除线扩展</span>
TablesExtension.create() <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">表格扩展</span>
<span style="color: rgba(0, 0, 0, 1)"> ));
Parser parser </span>=<span style="color: rgba(0, 0, 0, 1)"> Parser.builder(options).build();
InputStreamReader reader </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> InputStreamReader(FlexmarkCase.<span style="color: rgba(0, 0, 255, 1)">class</span>.getClassLoader().getResourceAsStream("test.md"<span style="color: rgba(0, 0, 0, 1)">));
Document document </span>=<span style="color: rgba(0, 0, 0, 1)"> parser.parseReader(reader);
ReversiblePeekingIterator</span><Node> iterator =<span style="color: rgba(0, 0, 0, 1)"> document.getChildIterator();
</span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (iterator.hasNext()) {
Node node </span>=<span style="color: rgba(0, 0, 0, 1)"> iterator.next();
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (node.getClass().equals(Heading.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">)) {
Heading heading </span>=<span style="color: rgba(0, 0, 0, 1)"> (Heading) node;
System.out.println(heading.getText());
} </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (node.getClass().equals(BulletList.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">)) {
BulletList bulletList </span>=<span style="color: rgba(0, 0, 0, 1)"> (BulletList) node;
ReversiblePeekingIterator</span><Node> iterator2 =<span style="color: rgba(0, 0, 0, 1)"> bulletList.getChildIterator();
</span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (iterator2.hasNext()) {
Node node2 </span>=<span style="color: rgba(0, 0, 0, 1)"> iterator2.next();
BulletListItem bulletListItem </span>=<span style="color: rgba(0, 0, 0, 1)"> (BulletListItem) node2;
ReversiblePeekingIterator</span><Node> iterator3 =<span style="color: rgba(0, 0, 0, 1)"> bulletListItem.getChildIterator();
</span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (iterator3.hasNext()) {
Node node3 </span>=<span style="color: rgba(0, 0, 0, 1)"> iterator3.next();
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (node3.getClass().equals(BulletList.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">)) {
System.out.println(</span>"二级无序列表"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">TODO: 继续解析</span>
} <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
System.out.println(node3.getChars());
}
}
}
} </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (node.getClass().equals(ThematicBreak.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">)) {
System.out.println(node.getChars());
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">else if...</span>
<span style="color: rgba(0, 0, 0, 1)"> }
}</span></pre>
</div>
<h3>2.5、完整代码</h3>
<div>
<div class="cnblogs_code"><img src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" id="code_img_closed_d9ccee20-aabb-43de-a059-f468776cee31" class="code_img_closed"><img src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" id="code_img_opened_d9ccee20-aabb-43de-a059-f468776cee31" class="code_img_opened" style="display: none">
<div id="cnblogs_code_open_d9ccee20-aabb-43de-a059-f468776cee31" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.abc.md;
</span><span style="color: rgba(0, 0, 255, 1)">import</span> com.vladsch.flexmark.ast.*<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.vladsch.flexmark.ext.gfm.strikethrough.Strikethrough;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension;
</span><span style="color: rgba(0, 0, 255, 1)">import</span> com.vladsch.flexmark.ext.tables.*<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.vladsch.flexmark.html.HtmlRenderer;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.vladsch.flexmark.parser.Parser;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.vladsch.flexmark.util.ast.Document;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.vladsch.flexmark.util.ast.Node;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.vladsch.flexmark.util.collection.iteration.ReversiblePeekingIterator;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.vladsch.flexmark.util.data.MutableDataSet;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.vladsch.flexmark.util.sequence.BasedSequence;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.junit.Test;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.IOException;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.InputStreamReader;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.Arrays;
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> FlexmarkCase {
@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> serialize() {
Document document </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Document(<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">, BasedSequence.EMPTY);
Heading heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>2<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1 Markdown语法教程"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.1 标题"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
Paragraph paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("不同数量的"<span style="color: rgba(0, 0, 0, 1)">));
Code code </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Code();
code.setText(BasedSequence.of(</span>"#"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(code);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("可以完成不同的标题,如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>1<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("一级标题"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>2<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("二级标题"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("三级标题"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.2 字体"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("粗体、斜体、粗体和斜体,删除线,需要在文字前后加不同的标记符号。如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
StrongEmphasis strongEmphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StrongEmphasis();
strongEmphasis.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("这个是粗体"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(strongEmphasis);
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
Emphasis emphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Emphasis();
emphasis.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("这个是斜体"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(emphasis);
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
strongEmphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StrongEmphasis();
emphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Emphasis();
emphasis.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("这个是粗体加斜体"<span style="color: rgba(0, 0, 0, 1)">));
strongEmphasis.appendChild(emphasis);
paragraph.appendChild(strongEmphasis);
document.appendChild(paragraph);
Strikethrough strikethrough </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Strikethrough();
strikethrough.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("这里是删除线"<span style="color: rgba(0, 0, 0, 1)">));
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(strikethrough);
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("注:如果想给字体换颜色、字体或者居中显示,需要使用内嵌HTML来实现。"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.3 无序列表"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表的使用,在符号"<span style="color: rgba(0, 0, 0, 1)">));
code </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Code();
code.setText(BasedSequence.of(</span>"-"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(code);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("后加空格使用。如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
BulletList bulletList </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletList();
BulletListItem bulletListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletListItem();
bulletListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 1"<span style="color: rgba(0, 0, 0, 1)">));
bulletList.appendChild(bulletListItem);
bulletListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletListItem();
bulletListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 2"<span style="color: rgba(0, 0, 0, 1)">));
bulletList.appendChild(bulletListItem);
bulletListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletListItem();
bulletListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 3"<span style="color: rgba(0, 0, 0, 1)">));
bulletList.appendChild(bulletListItem);
document.appendChild(bulletList);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("如果要控制列表的层级,则需要在符号"<span style="color: rgba(0, 0, 0, 1)">));
code </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Code();
code.setText(BasedSequence.of(</span>"-"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(code);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("前使用空格。如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
bulletList </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletList();
bulletListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletListItem();
bulletListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 1"<span style="color: rgba(0, 0, 0, 1)">));
bulletList.appendChild(bulletListItem);
bulletListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletListItem();
bulletListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 2"<span style="color: rgba(0, 0, 0, 1)">));
bulletList.appendChild(bulletListItem);
BulletList bulletList2 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletList();
bulletListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletListItem();
bulletListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 2.1"<span style="color: rgba(0, 0, 0, 1)">));
bulletList2.appendChild(bulletListItem);
bulletListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletListItem();
bulletListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 2.2"<span style="color: rgba(0, 0, 0, 1)">));
bulletList2.appendChild(bulletListItem);
bulletList.appendChild(bulletList2);
document.appendChild(bulletList);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.4 有序列表"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("有序列表的使用,在数字及符号"<span style="color: rgba(0, 0, 0, 1)">));
code </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Code();
code.setText(BasedSequence.of(</span>"."<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(code);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("后加空格后输入内容,如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
OrderedList orderedList </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> OrderedList();
orderedList.setStartNumber(</span>1<span style="color: rgba(0, 0, 0, 1)">);
OrderedListItem orderedListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> OrderedListItem();
orderedListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("有序列表 1"<span style="color: rgba(0, 0, 0, 1)">));
orderedList.appendChild(orderedListItem);
orderedListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> OrderedListItem();
orderedListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("有序列表 2"<span style="color: rgba(0, 0, 0, 1)">));
orderedList.appendChild(orderedListItem);
orderedListItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> OrderedListItem();
orderedListItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("有序列表 3"<span style="color: rgba(0, 0, 0, 1)">));
orderedList.appendChild(orderedListItem);
document.appendChild(orderedList);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.5 引用"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("引用的格式是在符号"<span style="color: rgba(0, 0, 0, 1)">));
code </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Code();
code.setText(BasedSequence.of(</span>">"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(code);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("后面书写文字。如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text(" "<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
BlockQuote blockQuote </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BlockQuote();
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("读一本好书,就是在和高尚的人谈话。 ——歌德"<span style="color: rgba(0, 0, 0, 1)">));
blockQuote.appendChild(paragraph);
document.appendChild(blockQuote);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text(" "<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
blockQuote </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BlockQuote();
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("雇用制度对工人不利,但工人根本无力摆脱这个制度。 ——阮一峰"<span style="color: rgba(0, 0, 0, 1)">));
blockQuote.appendChild(paragraph);
document.appendChild(blockQuote);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.7 链接"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("微信公众号仅支持公众号文章链接,即域名为"<span style="color: rgba(0, 0, 0, 1)">));
code </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Code();
code.setText(BasedSequence.of(</span>"https://mp.weixin.qq.com/"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(code);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("的合法链接。使用方法如下所示:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
Link link </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Link();
link.setUrl(BasedSequence.of(</span>"https://mp.weixin.qq.com/s/s5IhxV2ooX3JN_X416nidA"<span style="color: rgba(0, 0, 0, 1)">));
link.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("你是《未来世界的幸存者》么?"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("对于该论述,欢迎读者查阅之前发过的文章,"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(link);
document.appendChild(paragraph);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.8 图片"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("插入图片,格式如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
Image image </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Image();
image.setUrl(BasedSequence.of(</span>"https://markdown.com.cn/images/qrcode_for_gh_82cf87d482f0_258.jpg"<span style="color: rgba(0, 0, 0, 1)">));
image.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("这里写图片描述"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(image);
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("支持 jpg、png、gif、svg 等图片格式,"<span style="color: rgba(0, 0, 0, 1)">));
strongEmphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StrongEmphasis();
strongEmphasis.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("其中 svg 文件仅可在微信公众平台中使用"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(strongEmphasis);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text(",svg 文件示例如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
image </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Image();
image.setUrl(BasedSequence.of(</span>"https://markdown.com.cn/images/i-am-svg.svg"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(image);
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("支持图片"<span style="color: rgba(0, 0, 0, 1)">));
strongEmphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StrongEmphasis();
strongEmphasis.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("拖拽和截图粘贴"<span style="color: rgba(0, 0, 0, 1)">));
paragraph.appendChild(strongEmphasis);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("到编辑器中。"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("注:支持图片 "<span style="color: rgba(0, 0, 0, 1)">));
strongEmphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StrongEmphasis();
emphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Emphasis();
emphasis.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("拖拽和截图粘贴"<span style="color: rgba(0, 0, 0, 1)">));
strongEmphasis.appendChild(emphasis);
paragraph.appendChild(strongEmphasis);
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text(" 到编辑器中,仅支持 https 的图片,图片粘贴到微信时会自动上传微信服务器。"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.9 分割线"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("可以在一行中用三个以上的减号来建立一个分隔线,同时需要在分隔线的上面空一行。如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
ThematicBreak thematicBreak </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ThematicBreak();
document.appendChild(thematicBreak);
heading </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Heading();
heading.setLevel(</span>3<span style="color: rgba(0, 0, 0, 1)">);
heading.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("1.10 表格"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(heading);
paragraph </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paragraph();
paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("可以使用冒号来定义表格的对齐方式,如下:"<span style="color: rgba(0, 0, 0, 1)">));
document.appendChild(paragraph);
TableBlock tableBlock </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableBlock();
TableHead tableHead </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableHead();
TableRow tableRow </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableRow();
TableCell tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("姓名"<span style="color: rgba(0, 0, 0, 1)">));
tableCell.setAlignment(TableCell.Alignment.LEFT);
tableRow.appendChild(tableCell);
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("年龄"<span style="color: rgba(0, 0, 0, 1)">));
tableCell.setAlignment(TableCell.Alignment.CENTER);
tableRow.appendChild(tableCell);
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("工作"<span style="color: rgba(0, 0, 0, 1)">));
tableCell.setAlignment(TableCell.Alignment.RIGHT);
tableRow.appendChild(tableCell);
tableHead.appendChild(tableRow);
TableBody tableBody </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableBody();
tableRow </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableRow();
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("小可爱"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("18"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("吃可爱多"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableBody.appendChild(tableRow);
tableRow </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableRow();
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("小小勇敢"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("20"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("爬棵勇敢树"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableBody.appendChild(tableRow);
tableRow </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableRow();
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("小小小机智"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("22"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableCell </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TableCell();
tableCell.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("看一本机智书"<span style="color: rgba(0, 0, 0, 1)">));
tableRow.appendChild(tableCell);
tableBody.appendChild(tableRow);
tableBlock.appendChild(tableHead);
tableBlock.appendChild(tableBody);
document.appendChild(tableBlock);
MutableDataSet options </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MutableDataSet();
options.set(Parser.EXTENSIONS, Arrays.asList(
StrikethroughExtension.create(), </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除线扩展</span>
TablesExtension.create() <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">表格扩展</span>
<span style="color: rgba(0, 0, 0, 1)"> ));
HtmlRenderer htmlRenderer </span>=<span style="color: rgba(0, 0, 0, 1)"> HtmlRenderer.builder(options).build();
String html </span>=<span style="color: rgba(0, 0, 0, 1)"> htmlRenderer.render(document);
System.out.println(html);
MutableDataSet options2 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MutableDataSet();
options2.set(FlexmarkHtmlConverter.SETEXT_HEADINGS, </span><span style="color: rgba(0, 0, 255, 1)">false</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">标题是否使用下划线格式</span>
options2.set(FlexmarkHtmlConverter.SKIP_CHAR_ESCAPE, <span style="color: rgba(0, 0, 255, 1)">true</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">是否跳过转义符</span>
options2.set(FlexmarkHtmlConverter.UNORDERED_LIST_DELIMITER, '-');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">无序列表的符号</span>
options2.set(FlexmarkHtmlConverter.TYPOGRAPHIC_SMARTS, <span style="color: rgba(0, 0, 255, 1)">false</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">是否启用智能排版,它会处理文本中的一些常见的标点符号,并将其转换为更符合排版习惯的字符</span>
options2.set(FlexmarkHtmlConverter.THEMATIC_BREAK, "---"<span style="color: rgba(0, 0, 0, 1)">);
String markdown </span>=<span style="color: rgba(0, 0, 0, 1)"> FlexmarkHtmlConverter.builder(options2).build().convert(html);
System.out.println(markdown);
}
@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> deserialize() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException {
MutableDataSet options </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MutableDataSet();
options.set(Parser.EXTENSIONS, Arrays.asList(
StrikethroughExtension.create(), </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除线扩展</span>
TablesExtension.create() <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">表格扩展</span>
<span style="color: rgba(0, 0, 0, 1)"> ));
Parser parser </span>=<span style="color: rgba(0, 0, 0, 1)"> Parser.builder(options).build();
InputStreamReader reader </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> InputStreamReader(FlexmarkCase.<span style="color: rgba(0, 0, 255, 1)">class</span>.getClassLoader().getResourceAsStream("test.md"<span style="color: rgba(0, 0, 0, 1)">));
Document document </span>=<span style="color: rgba(0, 0, 0, 1)"> parser.parseReader(reader);
ReversiblePeekingIterator</span><Node> iterator =<span style="color: rgba(0, 0, 0, 1)"> document.getChildIterator();
</span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (iterator.hasNext()) {
Node node </span>=<span style="color: rgba(0, 0, 0, 1)"> iterator.next();
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (node.getClass().equals(Heading.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">)) {
Heading heading </span>=<span style="color: rgba(0, 0, 0, 1)"> (Heading) node;
System.out.println(heading.getText());
} </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (node.getClass().equals(BulletList.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">)) {
BulletList bulletList </span>=<span style="color: rgba(0, 0, 0, 1)"> (BulletList) node;
ReversiblePeekingIterator</span><Node> iterator2 =<span style="color: rgba(0, 0, 0, 1)"> bulletList.getChildIterator();
</span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (iterator2.hasNext()) {
Node node2 </span>=<span style="color: rgba(0, 0, 0, 1)"> iterator2.next();
BulletListItem bulletListItem </span>=<span style="color: rgba(0, 0, 0, 1)"> (BulletListItem) node2;
ReversiblePeekingIterator</span><Node> iterator3 =<span style="color: rgba(0, 0, 0, 1)"> bulletListItem.getChildIterator();
</span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (iterator3.hasNext()) {
Node node3 </span>=<span style="color: rgba(0, 0, 0, 1)"> iterator3.next();
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (node3.getClass().equals(BulletList.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">)) {
System.out.println(</span>"二级无序列表"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">TODO: 继续解析</span>
} <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
System.out.println(node3.getChars());
}
}
}
} </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (node.getClass().equals(ThematicBreak.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">)) {
System.out.println(node.getChars());
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">else if...</span>
<span style="color: rgba(0, 0, 0, 1)"> }
}
}</span></pre>
</div>
<span class="cnblogs_code_collapse">FlexmarkCase.java</span></div>
<p> </p>
<p> </p>
<p>参考:https://github.com/vsch/flexmark-java。</p>
</div>
</div>
</div>
<div> </div><br><br>
来源:https://www.cnblogs.com/wuyongyin/p/19106805
頁:
[1]