如何在Markdown表格中编写列表?

296

在Markdown表格里能否创建一个列表(项目符号、编号或者不编号的)。

表格的样式如下:

| Tables        | Are           | Cool  |
| ------------- |:-------------:| -----:|
| col 3 is      | right-aligned | $1600 |
| col 2 is      | centered      |   $12 |
| zebra stripes | are neat      |    $1 |
  • Item 1
  • Item 2
  • Item 3
* one
* two
* three

有什么办法可以合并它们吗?

9个回答

373

是的,您可以使用HTML将它们合并。当我从Github创建.md文件中的表格时,我总是喜欢使用HTML代码而不是Markdown。

Github Flavored Markdown支持在.md文件中使用基本的HTML。因此,这将是答案:

混合使用Markdown和HTML:

| Tables        | Are           | Cool  |
| ------------- |:-------------:| -----:|
| col 3 is      | right-aligned | $1600 |
| col 2 is      | centered      |   $12 |
| zebra stripes | are neat      |    $1 |
| <ul><li>item1</li><li>item2</li></ul>| See the list | from the first column|

或者纯HTML:

<table>
  <tbody>
    <tr>
      <th>Tables</th>
      <th align="center">Are</th>
      <th align="right">Cool</th>
    </tr>
    <tr>
      <td>col 3 is</td>
      <td align="center">right-aligned</td>
      <td align="right">$1600</td>
    </tr>
    <tr>
      <td>col 2 is</td>
      <td align="center">centered</td>
      <td align="right">$12</td>
    </tr>
    <tr>
      <td>zebra stripes</td>
      <td align="center">are neat</td>
      <td align="right">$1</td>
    </tr>
    <tr>
      <td>
        <ul>
          <li>item1</li>
          <li>item2</li>
        </ul>
      </td>
      <td align="center">See the list</td>
      <td align="right">from the first column</td>
    </tr>
  </tbody>
</table>

这是在Github上的样子:

该图片展示了Github上的外观。

3
这很好,但是有没有办法也为列表添加样式呢?例如去掉项目符号、边距等等。例如Github似乎不接受在ul元素上使用style="list-style: none"标签。 - Trebor Rude
1
@TreborRude 不行,因为Markdown实际上不是HTML。但是如果你使用一个库(例如marked),你可能会有这个功能(将HTML与Markdown结合起来)。 - Ionică Bizău
1
如果需要有序列表,请使用 ol。例如 <ol><li>枚举1</li><li>枚举2</li></ol> - user3613932
完美。正是我所需要的。而且它保留了Markdown格式,而不是添加多余的东西来解决一个简单的问题。 - Ken Ingram
不确定,但这可以在rmarkdown中完成吗? - Manny Ma
显示剩余3条评论

119

如果您想要一个没有项目符号的列表(或任何其他非标准用法)或在单元格中添加更多行,请使用<br />

| Event         | Platform      | Description |
| ------------- |-----------| -----:|
| `message_received`| `facebook-messenger`<br/>`skype`|

2
可能是因为三年前这是唯一合理的答案吧?我同意你今天提供的更好的答案。 - William Daniel
21
这是对Markdown表格中的换行?的回答,而不是关于列表的问题。 - Bergi
2
@Bergi 我已经点赞了你的建议。 ;) 谷歌搜索引导我来到这个问题,这就是我需要的解决方案。我认为它还可以接受(例如非项目符号列表),所以我将其保留在这个位置。 - Amio.io
4
您可以使用HTML实体添加项目符号:• facebook-messenger<br/> • skype - shawnhcorey
Markdown Lint 标记此处为 无内联 HTML - andrei.ciprian

60

据我所知,所有我了解的Markdown参考资料,像这个,都提到:

单元格内容必须在同一行上

你可以尝试使用Markdown表格生成器(它的示例看起来像你在问题中提到的那个),所以你可能已经知道它了。

Pandoc

如果你正在使用Pandoc的Markdown(它扩展了John Gruber的Markdown语法,基于该语法的GitHub Flavored Markdown),你可以使用grid_tables

+---------------+---------------+--------------------+
| Fruit         | Price         | Advantages         |
+===============+===============+====================+
| Bananas       | $1.34         | - built-in wrapper |
|               |               | - bright color     |
+---------------+---------------+--------------------+
| Oranges       | $2.10         | - cures scurvy     |
|               |               | - tasty            |
+---------------+---------------+--------------------+
或者 multiline_tables
-------------------------------------------------------------
 Centered   Default           Right Left
  Header    Aligned         Aligned Aligned
----------- ------- --------------- -------------------------
   First    row                12.0 Example of a row that
                                    spans multiple lines.

  Second    row                 5.0 Here's another one. Note
                                    the blank line between
                                    rows.
-------------------------------------------------------------

1
Markdown表格生成器有误,因为它接受了新行,而正如您引用的那样,这是不被接受的。但感谢您提供的宝贵信息。 - Gabriel Petrovay
1
我倾向于接受你的答案。但我会再等待1-2天,也许会有人发布一个更好的解决方法(如果我接受了答案,其他人就不会再关注它,除非他们遇到相同的问题)。 - Gabriel Petrovay
1
@GabrielPetrovay 我同意。你也可以联系GitHub支持,看看他们对此有何说法。(然后更新我的答案或发表你自己的意见) - VonC
1
@イオニカビザウ 我显然没有提到HTML。使用HTML,您可以重新创建任何markdown功能,因此它不是有效的解决方案。问题是关于markdown,不是HTML。 - VonC
1
这在Quarto表格及其交叉引用机制中运作良好。 - wibeasley
显示剩余5条评论

11

另一个解决方法是,在您的表格中添加<br>标签。

    |Method name| Behavior |
    |--|--|
    | OnAwakeLogicController(); | Its called when MainLogicController is loaded into the memory , its also hold the following actions :- <br> 1. Checking Audio Settings <br>2. Initializing Level Controller|

enter image description here


4

一种替代方法是最近我实现的,即使用div-table插件panflute创建表格。

这将从一组围栏分隔的 div 中创建一个表格(在 pandoc markdown 实现中是标准的),其布局类似于 html:

---
panflute-filters: [div-table]
panflute-path: 'panflute/docs/source'
---

::::: {.divtable}
:::: {.tcaption}
a caption here (optional), only the first paragraph is used.
::::
:::: {.thead}
[Header 1]{width=0.4 align=center}
[Header 2]{width=0.6 align=default}
::::
:::: {.trow}
::: {.tcell}

1. any
2. normal markdown
3. can go in a cell

:::
::: {.tcell}
![](https://pixabay.com/get/e832b60e2cf7043ed1584d05fb0938c9bd22ffd41cb2144894f9c57aae/bird-1771435_1280.png?attachment){width=50%}

some text
:::
::::
:::: {.trow bypara=true}
If bypara=true

Then each paragraph will be treated as a separate column
::::
any text outside a div will be ignored
:::::

看起来像:

在这里输入图片描述


3
如果您正在使用Kramdown(Jekyll的默认Markdown渲染器),则必须使用nomarkdown扩展{::nomarkdown}...{:/}
例如:
| Tables        | Are           | Cool  |
| ------------- |:-------------:| -----:|
| col 3 is      | right-aligned | $1600 |
| col 2 is      | centered      |   $12 |
| {::nomarkdown}<ul><li>one</li><li>two</li><li>three</li></ul>{:/} | Kramdown | bullets |

2
如果您使用html方法:
不要添加空行 像这样:
最初的回答:如果你使用HTML方法,请勿添加空行。请参考以下示例:
<table>
    <tbody>

        <tr>
            <td>1</td>
            <td>2</td>
        </tr>

        <tr>
            <td>1</td>
            <td>2</td>
        </tr>

    </tbody>
</table>

最初的回答:标记将会破裂。 去除空行:
<table>
    <tbody>
        <tr>
            <td>1</td>
            <td>2</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
        </tr>
    </tbody>
</table>

4
HTML会忽略空白行和多个空格,所以我认为这没有任何问题。您可以解释一下这里会出现什么问题吗? - XenGi

1
对于 Python markdown,我建议使用 GridTables markdown 扩展。特别是,我链接的分支版本在 mkdocs-material 中表现最佳,并且支持列表和大多数 markdown 格式。多行代码块无法很好地呈现。

你是如何为mkdocs安装它的?我在默认的扩展列表中没有看到它:https://squidfunk.github.io/mkdocs-material/setup/extensions/python-markdown/ - nocibambi

0

还没有展示的一个解决方案:

  • 使用HTML的<table>标签创建表格
  • 在表格单元格中编写Markdown

通过在<td>标签后添加一个换行符来实现:

<table><tbody>
<tr>
  <th> Left column </th>
  <th> Right column </th>
<tr>
<tr>
  <td>

  * Just `write` **markdown**
  * In _here_
  </td>
  <td>

  ```python
  def or_here():
    pass
  ```
  </td>
<tbody></table>

看起来是这样的:

enter image description here

如何操作:
  • <td>标签后添加一个空行
  • "zero-indent"缩进与<td>标签处于同一级别
这个操作有点棘手,但比编写完整的HTML要好看。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接