厚全 發表於 2025-12-27 16:43:00

Java 操作 Markdown(1)--commonmark-java 使用

<p>commonmark-java 是一个用于解析和渲染 Markdown 文本的 Java 库;本文主要介绍其基本使用,文中所使用到的软件版本:Java 17.0.5、commonmark-java 0.25.1。</p>
<h2>1、简介</h2>
<p>commonmark-java 可以用来解析抽象语法树(AST),访问和操作其节点,以及生成&nbsp;HTML 或&nbsp;Markdown。它最初是 commonmark.js 的移植版,但后来发展成为一个可扩展的库,具备以下特点:</p>
<ul>
<li>小巧(核心没有依赖,扩展以独立的组件形式提供)</li>
<li>快速(比曾经流行的 Markdown 库&nbsp;<code>pegdown</code>&nbsp;快 10 到 20 倍)</li>
<li>灵活(解析后可操作 AST,定制 HTML 渲染)</li>
<li>可扩展(支持表格、删除线、自动链接等)</li>
</ul>
<p>commonmark-java 需要使用 Java 11 及以上版本。</p>
<h2>2、commonmark-java 使用</h2>
<h3>2.1、样例文件</h3>
<p>解析&nbsp;Markdown 时使用该样例文件。</p>
<div class="cnblogs_code"><img src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" id="code_img_closed_60469022-89e1-4135-9310-7b25050f4b4b" class="code_img_closed"><img src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" id="code_img_opened_60469022-89e1-4135-9310-7b25050f4b4b" class="code_img_opened" style="display: none">
<div id="cnblogs_code_open_60469022-89e1-4135-9310-7b25050f4b4b" 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 引用

引用的格式是在符号`&gt;`后面书写文字。如下:

&gt; 读一本好书,就是在和高尚的人谈话。 ——歌德

&gt; 雇用制度对工人不利,但工人根本无力摆脱这个制度。 ——阮一峰

### 1.7 链接

微信公众号仅支持公众号文章链接,即域名为`https://mp.weixin.qq.com/`的合法链接。使用方法如下所示:

对于该论述,欢迎读者查阅之前发过的文章,[你是《未来世界的幸存者》么?](https://mp.weixin.qq.com/s/s5IhxV2ooX3JN_X416nidA)

### 1.8 图片

插入图片,格式如下:

![这里写图片描述](https://markdown.com.cn/images/qrcode_for_gh_82cf87d482f0_258.jpg)

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

![](https://markdown.com.cn/images/i-am-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)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.commonmark<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>commonmark<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>0.26.0<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.commonmark<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>commonmark-ext-gfm-tables<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>0.26.0<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<h3>2.3、API 方式生成&nbsp;HTML 或&nbsp;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><span style="color: rgba(0, 0, 0, 1)"> Document();
    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)">));
    paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Code("#"<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)">));
    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> Emphasis("*"<span style="color: rgba(0, 0, 0, 1)">);
    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> Emphasis("*"<span style="color: rgba(0, 0, 0, 1)">);
    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);

    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();
    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)">));
    paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Code("-"<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)">));
    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();
    ListItem listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
    listItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 1"<span style="color: rgba(0, 0, 0, 1)">));
    bulletList.appendChild(listItem);
    bulletList.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SoftLineBreak());
    listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
    listItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 2"<span style="color: rgba(0, 0, 0, 1)">));
    bulletList.appendChild(listItem);
    bulletList.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SoftLineBreak());
    listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
    listItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 3"<span style="color: rgba(0, 0, 0, 1)">));
    bulletList.appendChild(listItem);
    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)">));
    paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Code("-"<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)">));
    document.appendChild(paragraph);

    bulletList </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletList();
    listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
    listItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 1"<span style="color: rgba(0, 0, 0, 1)">));
    bulletList.appendChild(listItem);
    bulletList.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SoftLineBreak());
    listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
    listItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 2"<span style="color: rgba(0, 0, 0, 1)">));
    bulletList.appendChild(listItem);
    bulletList.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SoftLineBreak());
    BulletList bulletList2 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletList();
    listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
    listItem.setMarkerIndent(</span>2<span style="color: rgba(0, 0, 0, 1)">);
    listItem.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(listItem);
    bulletList2.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SoftLineBreak());
    listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
    listItem.setMarkerIndent(</span>2<span style="color: rgba(0, 0, 0, 1)">);
    listItem.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(listItem);
    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)">));
    paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Code("."<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)">));
    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();
    listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
    listItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("有序列表 1"<span style="color: rgba(0, 0, 0, 1)">));
    orderedList.appendChild(listItem);
    orderedList.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SoftLineBreak());
    listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
    listItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("有序列表 2"<span style="color: rgba(0, 0, 0, 1)">));
    orderedList.appendChild(listItem);
    orderedList.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SoftLineBreak());
    listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
    listItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("有序列表 3"<span style="color: rgba(0, 0, 0, 1)">));
    orderedList.appendChild(listItem);
    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)">));
    paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Code("&gt;"<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)">));
    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();
    blockQuote.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("读一本好书,就是在和高尚的人谈话。 ——歌德"<span style="color: rgba(0, 0, 0, 1)">));
    document.appendChild(blockQuote);

    blockQuote </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BlockQuote();
    blockQuote.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("雇用制度对工人不利,但工人根本无力摆脱这个制度。 ——阮一峰"<span style="color: rgba(0, 0, 0, 1)">));
    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)">));
    paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Code("https://mp.weixin.qq.com/"<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)">));
    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> Link("https://mp.weixin.qq.com/s/s5IhxV2ooX3JN_X416nidA", <span style="color: rgba(0, 0, 255, 1)">null</span><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> Image("https://markdown.com.cn/images/qrcode_for_gh_82cf87d482f0_258.jpg", <span style="color: rgba(0, 0, 255, 1)">null</span><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)">));
    emphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Emphasis("**"<span style="color: rgba(0, 0, 0, 1)">);
    emphasis.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("其中 svg 文件仅可在微信公众平台中使用"<span style="color: rgba(0, 0, 0, 1)">));
    paragraph.appendChild(emphasis);
    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> Image("https://markdown.com.cn/images/i-am-svg.svg", <span style="color: rgba(0, 0, 255, 1)">null</span><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> Emphasis("*"<span style="color: rgba(0, 0, 0, 1)">);
    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();
    thematicBreak.setLiteral(</span>"---"<span style="color: rgba(0, 0, 0, 1)">);
    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);

    List</span>&lt;Extension&gt; extensions =<span style="color: rgba(0, 0, 0, 1)"> List.of(TablesExtension.create());
    MarkdownRenderer markdownRenderer </span>=<span style="color: rgba(0, 0, 0, 1)"> MarkdownRenderer.builder().extensions(extensions).build();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">转成markdown格式</span>
    String md =<span style="color: rgba(0, 0, 0, 1)"> markdownRenderer.render(document);
    System.out.println(md);

    HtmlRenderer htmlRenderer </span>=<span style="color: rgba(0, 0, 0, 1)"> HtmlRenderer.builder().extensions(extensions).build();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">转成html</span>
    String html =<span style="color: rgba(0, 0, 0, 1)"> htmlRenderer.render(document);
    System.out.println(html);
}</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 {
    List</span>&lt;Extension&gt; extensions =<span style="color: rgba(0, 0, 0, 1)"> List.of(TablesExtension.create());
    Parser parser </span>=<span style="color: rgba(0, 0, 0, 1)"> Parser.builder().extensions(extensions).build();
    InputStreamReader reader </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> InputStreamReader(CommonmarkCase.<span style="color: rgba(0, 0, 255, 1)">class</span>.getClassLoader().getResourceAsStream("test.md"<span style="color: rgba(0, 0, 0, 1)">));
    Node document </span>=<span style="color: rgba(0, 0, 0, 1)"> parser.parseReader(reader);

    Node node </span>=<span style="color: rgba(0, 0, 0, 1)"> document.getFirstChild();
    </span><span style="color: rgba(0, 0, 255, 1)">while</span> (node != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
      System.out.println(node.getClass());
      </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, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">处理标题</span>
            Heading heading =<span style="color: rgba(0, 0, 0, 1)"> (Heading) node;
            Text text </span>=<span style="color: rgba(0, 0, 0, 1)"> (Text)heading.getFirstChild();
            System.out.println(text.getLiteral());
      } </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, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">处理无序列表</span>
            BulletList bulletList =<span style="color: rgba(0, 0, 0, 1)"> (BulletList) node;
            Node node2 </span>=<span style="color: rgba(0, 0, 0, 1)"> bulletList.getFirstChild();
            </span><span style="color: rgba(0, 0, 255, 1)">while</span> (node2 != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (node2.getClass().equals(ListItem.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">)) {
                  Node node3 </span>=<span style="color: rgba(0, 0, 0, 1)"> node2.getFirstChild();
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span> (node3.getClass().equals(Paragraph.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">)) {
                        Text text </span>=<span style="color: rgba(0, 0, 0, 1)"> (Text)node3.getFirstChild();
                        System.out.println(text.getLiteral());
                  }

                  </span><span style="color: rgba(0, 0, 255, 1)">if</span> (node3.getNext() != <span style="color: rgba(0, 0, 255, 1)">null</span> &amp;&amp; node3.getNext().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, 0, 1)">                  }
                }
                node2 </span>=<span style="color: rgba(0, 0, 0, 1)"> node2.getNext();
            }
      }
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">else if...</span>
      node =<span style="color: rgba(0, 0, 0, 1)"> node.getNext();
    }
}</span></pre>
</div>
<h3>2.5、完整代码</h3>
<div class="cnblogs_code"><img src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" id="code_img_closed_d1644b9c-4635-496a-8498-02cf9e078fca" class="code_img_closed"><img src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" id="code_img_opened_d1644b9c-4635-496a-8498-02cf9e078fca" class="code_img_opened" style="display: none">
<div id="cnblogs_code_open_d1644b9c-4635-496a-8498-02cf9e078fca" 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><span style="color: rgba(0, 0, 0, 1)"> lombok.extern.slf4j.Slf4j;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.commonmark.Extension;
</span><span style="color: rgba(0, 0, 255, 1)">import</span> org.commonmark.ext.gfm.tables.*<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">import</span> org.commonmark.node.*<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)"> org.commonmark.parser.Parser;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.commonmark.renderer.html.HtmlRenderer;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.commonmark.renderer.markdown.MarkdownRenderer;
</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.List;


@Slf4j
</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)"> CommonmarkCase {
    @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><span style="color: rgba(0, 0, 0, 1)"> Document();
      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)">));
      paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Code("#"<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)">));
      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> Emphasis("*"<span style="color: rgba(0, 0, 0, 1)">);
      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> Emphasis("*"<span style="color: rgba(0, 0, 0, 1)">);
      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);

      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();
      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)">));
      paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Code("-"<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)">));
      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();
      ListItem listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
      listItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 1"<span style="color: rgba(0, 0, 0, 1)">));
      bulletList.appendChild(listItem);
      bulletList.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SoftLineBreak());
      listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
      listItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 2"<span style="color: rgba(0, 0, 0, 1)">));
      bulletList.appendChild(listItem);
      bulletList.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SoftLineBreak());
      listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
      listItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 3"<span style="color: rgba(0, 0, 0, 1)">));
      bulletList.appendChild(listItem);
      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)">));
      paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Code("-"<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)">));
      document.appendChild(paragraph);

      bulletList </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletList();
      listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
      listItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 1"<span style="color: rgba(0, 0, 0, 1)">));
      bulletList.appendChild(listItem);
      bulletList.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SoftLineBreak());
      listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
      listItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("无序列表 2"<span style="color: rgba(0, 0, 0, 1)">));
      bulletList.appendChild(listItem);
      bulletList.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SoftLineBreak());
      BulletList bulletList2 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BulletList();
      listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
      listItem.setMarkerIndent(</span>2<span style="color: rgba(0, 0, 0, 1)">);
      listItem.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(listItem);
      bulletList2.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SoftLineBreak());
      listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
      listItem.setMarkerIndent(</span>2<span style="color: rgba(0, 0, 0, 1)">);
      listItem.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(listItem);
      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)">));
      paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Code("."<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)">));
      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();
      listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
      listItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("有序列表 1"<span style="color: rgba(0, 0, 0, 1)">));
      orderedList.appendChild(listItem);
      orderedList.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SoftLineBreak());
      listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
      listItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("有序列表 2"<span style="color: rgba(0, 0, 0, 1)">));
      orderedList.appendChild(listItem);
      orderedList.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SoftLineBreak());
      listItem </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ListItem();
      listItem.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("有序列表 3"<span style="color: rgba(0, 0, 0, 1)">));
      orderedList.appendChild(listItem);
      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)">));
      paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Code("&gt;"<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)">));
      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();
      blockQuote.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("读一本好书,就是在和高尚的人谈话。 ——歌德"<span style="color: rgba(0, 0, 0, 1)">));
      document.appendChild(blockQuote);

      blockQuote </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BlockQuote();
      blockQuote.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("雇用制度对工人不利,但工人根本无力摆脱这个制度。 ——阮一峰"<span style="color: rgba(0, 0, 0, 1)">));
      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)">));
      paragraph.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Code("https://mp.weixin.qq.com/"<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)">));
      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> Link("https://mp.weixin.qq.com/s/s5IhxV2ooX3JN_X416nidA", <span style="color: rgba(0, 0, 255, 1)">null</span><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> Image("https://markdown.com.cn/images/qrcode_for_gh_82cf87d482f0_258.jpg", <span style="color: rgba(0, 0, 255, 1)">null</span><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)">));
      emphasis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Emphasis("**"<span style="color: rgba(0, 0, 0, 1)">);
      emphasis.appendChild(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Text("其中 svg 文件仅可在微信公众平台中使用"<span style="color: rgba(0, 0, 0, 1)">));
      paragraph.appendChild(emphasis);
      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> Image("https://markdown.com.cn/images/i-am-svg.svg", <span style="color: rgba(0, 0, 255, 1)">null</span><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> Emphasis("*"<span style="color: rgba(0, 0, 0, 1)">);
      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();
      thematicBreak.setLiteral(</span>"---"<span style="color: rgba(0, 0, 0, 1)">);
      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);

      List</span>&lt;Extension&gt; extensions =<span style="color: rgba(0, 0, 0, 1)"> List.of(TablesExtension.create());
      MarkdownRenderer markdownRenderer </span>=<span style="color: rgba(0, 0, 0, 1)"> MarkdownRenderer.builder().extensions(extensions).build();
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">转成markdown格式</span>
      String md =<span style="color: rgba(0, 0, 0, 1)"> markdownRenderer.render(document);
      System.out.println(md);

      HtmlRenderer htmlRenderer </span>=<span style="color: rgba(0, 0, 0, 1)"> HtmlRenderer.builder().extensions(extensions).build();
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">转成html</span>
      String html =<span style="color: rgba(0, 0, 0, 1)"> htmlRenderer.render(document);
      System.out.println(html);
    }

    @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 {
      List</span>&lt;Extension&gt; extensions =<span style="color: rgba(0, 0, 0, 1)"> List.of(TablesExtension.create());
      Parser parser </span>=<span style="color: rgba(0, 0, 0, 1)"> Parser.builder().extensions(extensions).build();
      InputStreamReader reader </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> InputStreamReader(CommonmarkCase.<span style="color: rgba(0, 0, 255, 1)">class</span>.getClassLoader().getResourceAsStream("test.md"<span style="color: rgba(0, 0, 0, 1)">));
      Node document </span>=<span style="color: rgba(0, 0, 0, 1)"> parser.parseReader(reader);

      Node node </span>=<span style="color: rgba(0, 0, 0, 1)"> document.getFirstChild();
      </span><span style="color: rgba(0, 0, 255, 1)">while</span> (node != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
            System.out.println(node.getClass());
            </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, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">处理标题</span>
                Heading heading =<span style="color: rgba(0, 0, 0, 1)"> (Heading) node;
                Text text </span>=<span style="color: rgba(0, 0, 0, 1)"> (Text)heading.getFirstChild();
                System.out.println(text.getLiteral());
            } </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, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">处理无序列表</span>
                BulletList bulletList =<span style="color: rgba(0, 0, 0, 1)"> (BulletList) node;
                Node node2 </span>=<span style="color: rgba(0, 0, 0, 1)"> bulletList.getFirstChild();
                </span><span style="color: rgba(0, 0, 255, 1)">while</span> (node2 != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span> (node2.getClass().equals(ListItem.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">)) {
                        Node node3 </span>=<span style="color: rgba(0, 0, 0, 1)"> node2.getFirstChild();
                        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (node3.getClass().equals(Paragraph.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">)) {
                            Text text </span>=<span style="color: rgba(0, 0, 0, 1)"> (Text)node3.getFirstChild();
                            System.out.println(text.getLiteral());
                        }

                        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (node3.getNext() != <span style="color: rgba(0, 0, 255, 1)">null</span> &amp;&amp; node3.getNext().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, 0, 1)">                        }
                  }
                  node2 </span>=<span style="color: rgba(0, 0, 0, 1)"> node2.getNext();
                }
            }
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">else if...</span>
            node =<span style="color: rgba(0, 0, 0, 1)"> node.getNext();
      }
    }
}</span></pre>
</div>
<span class="cnblogs_code_collapse">CommonmarkCase.java</span></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>参考:https://github.com/commonmark/commonmark-java。</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/wuyongyin/p/19087564
頁: [1]
查看完整版本: Java 操作 Markdown(1)--commonmark-java 使用