且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Markdown:如何通过编号引用编号列表中的项目(例如LaTeX的\ ref/\ label)?

更新时间:2022-12-31 22:44:56

HTML甚至无法做到这一点,而Markdown是HTML的子集,因此答案是否定的.

HTML can't even do that and Markdown is a subset of HTML, so the answer is no.

例如,您的列表将这样表示(由Markdown渲染时):

For example, your list would be represented like so (when rendered by Markdown):

<ol>
    <li>This is a numbered item</li>
    <li>This is another numbered item</li>
    <li>Same as 1</li>
</ol>

请注意,就编号而言,没有任何指示.所有这些都是在浏览器渲染时推断出来的.但是,数字值未存储在文档中,并且不可引用或不可链接.它们仅用于显示,没有其他目的.

Notice that there is no indication of which item is which as far as the numbering goes. That is all inferred at render time by the browser. However, the number values are not stored within the document and are not referancable or linkable. They are for display only and serve no other purpose.

现在,您可以编写一些自定义HTML来唯一地标识每个列表项并使它们可引用:

Now you could write some custom HTML to uniquely identify each list item and make them referencable:

<ol>
    <li id="item1">This is a numbered item</li>
    <li id="item2">This is another numbered item</li>
    <li id="item3">Same as <a href="#item1>1</a></li>
</ol>

但是,这些ID是硬编码的,与用于显示项目的数字无关.虽然,我想这就是您想要的.要进行更新的更改,请执行以下操作:

However, those IDs are hardcoded and have no relation to the numbers used to display the items. Although, I suppose that's what you want. To make your updated changes:

<ol>
    <li is="item0">This is the very first item</li>
    <li id="item1">This is a numbered item</li>
    <li id="item2">This is another numbered item</li>
    <li id="item3">Same as <a href="#item1>2</a></li>
</ol>

ID随项目保留在一起.但是,让我们转到这些列表项的链接.请注意,在第一次迭代中,我们有:

The IDs stay with the item as intended. However, lets move on to the links to those list items. Note that in the first iteration we had:

<a href="#item1>1</a>

有了更新,我们有了:

<a href="#item1>2</a>

唯一的区别是链接的标签(从"1"更改为"2").那实际上是通过某种宏魔术东西来更改文档文本. HTML不能做的事情,至少没有JavaScript和/或CSS不能帮助.

The only difference being the link's label (changed from "1" to "2"). That is actually changing the document text through some sort of macro magic stuff. Not something HTML can do, at least not without JavaScript and/or CSS to help.

换句话说,每次更新列表时,每次引用该项目的文本都需要在整个文档中进行手动更新.这是针对HTML的. Markdown呢?由于规则状态:

In other words, the text of every reference to the item would need to be manually updated throughout the document every time the list is updated. And that is for HTML. What about Markdown? As the rules state:

Markdown不能替代HTML,甚至不能替代HTML.它的语法非常小,仅对应一小部分HTML标签.

Markdown is not a replacement for HTML, or even close to it. Its syntax is very small, corresponding only to a very small subset of HTML tags.

因此,在标准Markdown中,甚至没有任何方法可以将ID分配给列表项.

Therefore in standard Markdown there is not even any way to assign IDs to the list items.

在我看来,您要么需要使用列表以外的其他内容,要么需要Markdown/HTML以外的其他内容.

Seems to me you either need to use something other than lists or use something other than Markdown/HTML.