Markdown中表格行内的代码块

时间:2015-02-13 20:22:50

标签: markdown github-flavored-markdown

我在Github风味的Markdown中整理了一些文档,我想把一个有两行的表放在一起。一个只有文本,一个带有json代码块。这是一个例子。

| Status | Response  |
|---|---|
| 200 |  |
| 400 |   |

我想在响应行中获取此代码,但是当我尝试完全中断时。

json
  {
    "id": 10,
    "username": "alanpartridge",
    "email": "alan@alan.com",
    "password_hash": "$2a$10$uhUIUmVWVnrBWx9rrDWhS.CPCWCZsyqqa8./whhfzBZydX7yvahHS",
    "password_salt": "$2a$10$uhUIUmVWVnrBWx9rrDWhS.",
    "created_at": "2015-02-14T20:45:26.433Z",
    "updated_at": "2015-02-14T20:45:26.540Z"
}

我是Markdown的新手,如果有人能指出我正确的方向,我将非常感激。

5 个答案:

答案 0 :(得分:16)

github markdown doc声明您可以在表格单元格中包含内联/跨度降价标记。除了少数试图建立对表格布局的更多控制之外,对于大多数降价标记都是如此。

您可以使用内联代码元素,但不会使用语法着色或行缩进进行格式化。

| Status | Response  |
| ------ | --------- |
| 200    | `json`                          |
|        | `   {`                          |
|        | ` "id": 10,`                    |
|        | ` "username": "alanpartridge",` |
|        | ` more code...`                 |
|        | `}`                             |
| 400    |                                 |

或者,使用html以老式的方式创建表格,这样可以rowspan进行更好的布局控制。

答案 1 :(得分:12)

正如其他人指出的那样,您必须使用HTML <table>标签来创建表。但是,可以仅使用Markdown格式化表格单元格的内容

如果您在HTML标记周围留下多余的空白行,则HTML块中的Markdown语法将起作用:在<td>之后留一个空白行,在</td>之前留一个空白行,否则内部的Markdown不会'不能解析!这将创建一个新的<p>段落,在该段落中将在表单元格内重新启用Markdown解析。

<table>
<tr>
<td> Status </td> <td> Response </td>
</tr>
<tr>
<td> 200 </td>
<td>

^ Extra blank line above!
```json
json
{
    "id": 10,
    "username": "alanpartridge",
    "email": "alan@alan.com",
    "password_hash": "$2a$10$uhUIUmVWVnrBWx9rrDWhS.CPCWCZsyqqa8./whhfzBZydX7yvahHS",
    "password_salt": "$2a$10$uhUIUmVWVnrBWx9rrDWhS.",
    "created_at": "2015-02-14T20:45:26.433Z",
    "updated_at": "2015-02-14T20:45:26.540Z"
}
```
V Extra blank line below!

</td>
</tr>
<tr>
<td> 400 </td>
<td>

**Markdown** _here_. (Blank lines needed before and after!)

</td>
</tr>
</table>

Preview image of the table created with the code above on GitHub

(如果您要解决“ 400”和“此处的Markdown”之间的垂直对齐问题,请在“ 400”周围添加空白行,也将其包裹在<p>中。)

答案 2 :(得分:7)

Github风格的Markdown支持HTML标记

简单地说明这一点(尽管StackOverflow将不支持)-

<table>
<tr>
<th>
Status
</th>
<th>
Response
</th>
</tr>

<tr>

<td>
<pre>
<br/><br/><br/>200<br/><br/><br/><br/><br/>400<br/>
</pre>
</td>

<td>
<pre>
json
  {
    "id": 10,
    "username": "alanpartridge",
    "email": "alan@alan.com",
    "password_hash": "$2a$10$uhUIUmVWVnrBWx9rrDWhS.CPCWCZsyqqa8./whhfzBZydX7yvahHS",
    "password_salt": "$2a$10$uhUIUmVWVnrBWx9rrDWhS.",
    "created_at": "2015-02-14T20:45:26.433Z",
    "updated_at": "2015-02-14T20:45:26.540Z"
}
</pre>
</td>

</tr>
</table>

输出:

github markdown table-code


您还可以尝试这样的基于文本的表(对StackOverflow有用)-

+---------------+--------+---------+
|       \       | Rating | Comment |
+---------------+--------+---------+
| One Piece     |  A | B |       ♢ |
+---------------+----+---+---------+
| Naruto        |  A | C |       ♧ |
+---------------+----+---+---------+
| One Punch Man |  A | A |       ♥ |
+---------------+----+---+---------+
| Death Note    |  A | B |       ♠ |
+---------------+----+---+---------+

Text Tables Generator 是一个很棒的网站。

答案 3 :(得分:1)

这也许更好:

| Status | Response  |
| ------ | --------- |
| 200    |<pre lang="json">{<br>  "id": 10,<br>  "username": "alanpartridge",<br>  "email": "alan@alan.com",<br>  "password_hash": "$2a$10$uhUIUmVWVnrBWx9rrDWhS.CPCWCZsyqqa8./whhfzBZydX7yvahHS",<br>  "password_salt": "$2a$10$uhUIUmVWVnrBWx9rrDWhS.",<br>  "created_at": "2015-02-14T20:45:26.433Z",<br>  "updated_at": "2015-02-14T20:45:26.540Z"<br>}</pre>|
| 400    |<code>{<br>  "code": 400,<br>  "msg": balabala"<br>}</code>|

他们两个都需要<br>,这取决于您喜欢<pre>还是<code>

答案 4 :(得分:0)

Status | Response
:----- | :-------
200    | <code>json {"id": 10,"username": "alanpartridge", "email": "alan@alan.com",<br>"password_hash": "$2a$10$uhUIUmVWVnrBWx9rrDWhS.CPCWCZsyqqa8./whhfzBZydX7yvahHS", "password_salt": "$2a$10$uhUIUmVWVnrBWx9rrDWhS.",<br> "created_at": "2015-02-14T20:45:26.433Z", "updated_at": "2015-02-14T20:45:26.540Z" }</code>
400    | <code>json {"id": 10,"username": "alanpartridge", "email": "alan@alan.com",<br>"password_hash": "$2a$10$uhUIUmVWVnrBWx9rrDWhS.CPCWCZsyqqa8./whhfzBZydX7yvahHS", "password_salt": "$2a$10$uhUIUmVWVnrBWx9rrDWhS.",<br> "created_at": "2015-02-14T20:45:26.433Z", "updated_at": "2015-02-14T20:45:26.540Z" }</code>