markdown table
时间: 2023-10-08 20:12:23 浏览: 38
Markdown可以使用简单的语法创建表格。下面是一个示例:
```
| 列1 | 列2 | 列3 |
|-----|-----|-----|
| A | B | C |
| D | E | F |
```
该示例将创建一个包含3列的表格,每列的内容分别为 "列1"、"列2" 和 "列3"。第二行用于分隔标题行和内容行。接下来的两行是表格的内容,包含了两行数据。
这是渲染后的表格:
| 列1 | 列2 | 列3 |
|-----|-----|-----|
| A | B | C |
| D | E | F |
你可以根据需要自定义表格的内容和样式。
相关问题
markdown table 语法
Here's the syntax for creating a Markdown table:
1. Start the first line with a vertical bar (`|`) and separate each column with a vertical bar. End the line with a vertical bar to close the table header.
2. On the second line, create a separator row using hyphens (`-`) and vertical bars (`|`). The hyphens should be in the same positions as the column headers in the first line.
3. On the following lines, add the table content using the same format as the first line. Separate each cell with a vertical bar and end the line with a vertical bar.
Here's an example of the syntax:
```
| Column 1 Header | Column 2 Header | Column 3 Header |
| --------------- | --------------- | --------------- |
| Row 1 Column 1 | Row 1 Column 2 | Row 1 Column 3 |
| Row 2 Column 1 | Row 2 Column 2 | Row 2 Column 3 |
```
You can also add formatting to the content within the table cells, such as bold or italic text, using Markdown syntax.
java 将markdown table 转成html 的table标签
可以使用开源的Java Markdown库 CommonMark,然后使用正则表达式将Markdown中的表格转换成HTML的table标签。
以下是一个示例代码:
```
import org.commonmark.node.*;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MarkdownToHtml {
public static void main(String[] args) {
String markdown = "| Header 1 | Header 2 |\n| --- | --- |\n| Cell 1 | Cell 2 |";
String html = markdownToHtml(markdown);
System.out.println(html);
}
public static String markdownToHtml(String markdown) {
Parser parser = Parser.builder().build();
Node document = parser.parse(markdown);
HtmlRenderer renderer = HtmlRenderer.builder().build();
String html = renderer.render(document);
// Convert Markdown table to HTML table
Pattern pattern = Pattern.compile("<p>\\|(.*)\\|</p>");
Matcher matcher = pattern.matcher(html);
while (matcher.find()) {
String markdownTable = matcher.group(1);
String[] rows = markdownTable.split("\\|\\|");
StringBuilder htmlTable = new StringBuilder("<table>");
for (String row : rows) {
String[] cells = row.split("\\|");
htmlTable.append("<tr>");
for (String cell : cells) {
htmlTable.append("<td>").append(cell.trim()).append("</td>");
}
htmlTable.append("</tr>");
}
htmlTable.append("</table>");
html = html.replace(matcher.group(), htmlTable.toString());
}
return html;
}
}
```
输入的Markdown字符串为:
```
| Header 1 | Header 2 |
| --- | --- |
| Cell 1 | Cell 2 |
```
输出的HTML字符串为:
```
<table>
<tr>
<td>Header 1</td>
<td>Header 2</td>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
</table>
```