Markdown: What's the proper way to do a continued list inside of a list?

六月ゝ 毕业季﹏ 提交于 2019-12-11 17:45:01

问题


What's the proper way to do a continued list inside of a list in GFM?

Goal:

  1. One
    1. one
    2. two
  2. Two
    1. three
    2. four

I've looked at common posts regarding continued number lists and relevant issues but haven't seen anyone ask this question yet.


回答1:


Technically you can't. Markdown is a subset of HTML and HTML does not offer such a feature. However, you can fake it in HTML with something like <ol start="3"> which essentially tells a list to start numbering with 3 rather than the default 1.

Of course, you could always fall back to raw HTML, but lets proceed with the goal of accomplishing this in Markdown only. Whether you can do this in Markdown depends on which Markdown implementation you are using.

The traditional Markdown rules state:

It’s important to note that the actual numbers you use to mark the list have no effect on the HTML output Markdown produces.

...

If you do use lazy list numbering, however, you should still start the list with the number 1. At some point in the future, Markdown may support starting ordered lists at an arbitrary number.

From that, it is clear that the desired behavior is currently not possible. However, if the hinted change were to happen, things might be different...

As it turns out, Commonmark, which Github extends, does support starting ordered lists at an arbitrary number. As the spec states:

The start number of an ordered list is determined by the list number of its initial list item. The numbers of subsequent list items are disregarded.

Given the above, It might seem that the following would produce the desired output:

1. One
    1. one
    2. two
2. Two
    3. three
    4. four 

But, as Babelmark demonstrates, that only works for a few implementations (notice the implementations which include <ol start="3"> in the output).

Part of the problem is that the parent list contains a paragraph (with content Two) ont he line immediately before the nested list. So if you add a blank line between the different block level elements, like this:

1. One

    1. one
    2. two

2. Two

    3. three
    4. four

Then more implementations properly see the nested list. Specifically, as Babelmark demonstrates, this causes Commonmark to parse the list as you desire. In fact, you get this output (note the <ol start="3">):

<ol>
    <li>
        <p>One</p>
        <ol>
            <li>one</li>
            <li>two</li>
        </ol>
    </li>
    <li>
        <p>Two</p>
        <ol start="3">
            <li>three</li>
            <li>four</li>
        </ol>
    </li>
</ol>

And, in fact, I checked and this works correctly in GitHub.



来源:https://stackoverflow.com/questions/54101316/markdown-whats-the-proper-way-to-do-a-continued-list-inside-of-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!