mirror of
https://gitee.com/bookshelfplus/bookshelfplus
synced 2025-09-04 08:01:39 +08:00
基本实现MarkdownUtils(还差表格部分)
This commit is contained in:
@@ -0,0 +1,288 @@
|
||||
package plus.bookshelf.Common.MarkdownUtils;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.yaml.snakeyaml.error.Mark;
|
||||
|
||||
public class MarkdownUtils {
|
||||
|
||||
/**
|
||||
* markdown 内容
|
||||
*/
|
||||
private StringBuilder markdown;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
private MarkdownUtils() {
|
||||
this.markdown = new StringBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取对象实例
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static MarkdownUtils getInstance() {
|
||||
return new MarkdownUtils();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加内容
|
||||
*
|
||||
* @param str
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils append(String str) {
|
||||
markdown.append(str);
|
||||
markdown.append("\n\n");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取markdown内容
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String getMarkdown() {
|
||||
// 去除最后的两个换行符
|
||||
markdown.delete(markdown.length() - 2, markdown.length());
|
||||
return markdown.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 一级标题
|
||||
*
|
||||
* @param str
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
|
||||
public MarkdownUtils h1(String str) {
|
||||
this.append("# " + str);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 二级标题
|
||||
*
|
||||
* @param str
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils h2(String str) {
|
||||
this.append("## " + str);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 三级标题
|
||||
*
|
||||
* @param str
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils h3(String str) {
|
||||
this.append("### " + str);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 四级标题
|
||||
*
|
||||
* @param str
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils h4(String str) {
|
||||
this.append("#### " + str);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 五级标题
|
||||
*
|
||||
* @param str
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils h5(String str) {
|
||||
this.append("##### " + str);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 六级标题
|
||||
*
|
||||
* @param str
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils h6(String str) {
|
||||
this.append("###### " + str);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加粗
|
||||
*
|
||||
* @param text
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils bold(String text) {
|
||||
this.append("**" + text + "**");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 斜体
|
||||
*
|
||||
* @param text
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils italic(String text) {
|
||||
this.append("*" + text + "*");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 行内代码
|
||||
*
|
||||
* @param text
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils inlineCode(String text) {
|
||||
this.append("`" + text + "`");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 代码片段
|
||||
*
|
||||
* @param text
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils code(String text) {
|
||||
this.append("```\n" + text + "\n```");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 引用
|
||||
*
|
||||
* @param markdown
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils blockquote(MarkdownUtils markdown) {
|
||||
this.append("> " + markdown.getMarkdown().replaceAll("\n", "\n> "));
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownUtils blockquote(String str) {
|
||||
this.append("> " + str.replaceAll("\n", "\n> "));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 链接
|
||||
*
|
||||
* @param text
|
||||
* @param url
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils link(String text, String url) {
|
||||
this.append("[" + text + "](" + url + ")");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片
|
||||
*
|
||||
* @param url
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils image(String url) {
|
||||
this.append("");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分割线
|
||||
*
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils hr() {
|
||||
this.append("---");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 换行
|
||||
*
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils br() {
|
||||
this.append("");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 无序列表
|
||||
*
|
||||
* @param text
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils ul(String text) {
|
||||
this.append("- " + text);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 无序列表 (子分类)
|
||||
*
|
||||
* @param text
|
||||
* @param level
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils ul(String text, int level) {
|
||||
this.append(StringUtils.repeat(" ", level) + "- " + text);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 有序列表
|
||||
*
|
||||
* @param text
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils ol(String text) {
|
||||
this.append("1. " + text);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 有序列表 (子分类)
|
||||
*
|
||||
* @param text
|
||||
* @param level
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils ol(String text, int level) {
|
||||
this.append(StringUtils.repeat(" ", level) + "1. " + text);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 段落
|
||||
*
|
||||
* @param text
|
||||
* @return MarkdownUtils
|
||||
*/
|
||||
public MarkdownUtils p(String text) {
|
||||
this.append(text);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 表格 TODO
|
||||
*
|
||||
* @param text
|
||||
* @return
|
||||
*/
|
||||
public MarkdownUtils table(String text) {
|
||||
return this;
|
||||
}
|
||||
}
|
54
bookshelfplus/src/test/java/plus/bookshelf/markDownTest.java
Normal file
54
bookshelfplus/src/test/java/plus/bookshelf/markDownTest.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package plus.bookshelf;
|
||||
|
||||
import org.junit.Test;
|
||||
import plus.bookshelf.Common.MarkdownUtils.MarkdownUtils;
|
||||
|
||||
public class markDownTest {
|
||||
|
||||
@Test
|
||||
public void xiu() {
|
||||
String markdown = MarkdownUtils.getInstance()
|
||||
.h1("标题1")
|
||||
.p("这是一段段落")
|
||||
.h2("标题2")
|
||||
.p("这是一段段落")
|
||||
.h3("标题3")
|
||||
.p("这是一段段落")
|
||||
.h4("标题4")
|
||||
.p("这是一段段落")
|
||||
.h5("标题5")
|
||||
.p("这是一段段落")
|
||||
.h6("标题6")
|
||||
.p("这是一段段落")
|
||||
.hr()
|
||||
.p("这是一段段落")
|
||||
.bold("这是一段加粗的段落")
|
||||
.italic("这是一段斜体的段落")
|
||||
.code("这是一段代码的段落")
|
||||
.blockquote("这是一段引用的段落")
|
||||
.ul("这是一段无序列表的段落")
|
||||
.ul("这是一段无序列表的段落", 1)
|
||||
.ul("这是一段无序列表的段落", 2)
|
||||
.ul("这是一段无序列表的段落", 3)
|
||||
.ul("这是一段无序列表的段落", 4)
|
||||
.ul("这是一段无序列表的段落", 5)
|
||||
.ul("这是一段无序列表的段落", 6)
|
||||
.ul("这是一段无序列表的段落", 7)
|
||||
.ol("这是一段有序列表的段落")
|
||||
.ol("这是一段有序列表的段落", 1)
|
||||
.ol("这是一段有序列表的段落", 2)
|
||||
.ol("这是一段有序列表的段落", 3)
|
||||
.ol("这是一段有序列表的段落", 4)
|
||||
.ol("这是一段有序列表的段落", 5)
|
||||
.ol("这是一段有序列表的段落", 6)
|
||||
.ol("这是一段有序列表的段落", 7)
|
||||
.table("这是一段表格的段落")
|
||||
.link("这是一段链接的段落", "https://www.baidu.com")
|
||||
.image("https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF")
|
||||
.blockquote(MarkdownUtils.getInstance()
|
||||
.p("这是一段段落")
|
||||
.p("这是一段段落"))
|
||||
.getMarkdown();
|
||||
System.out.println(markdown);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user