在Markdown中,链接到页面片段(例如#some_id)的最佳方式是什么?

时间:2022-11-05 21:45:20

I'm trying to figure out how to reference another area of a page with Markdown. I can get it working if I add a

我想知道如何使用Markdown引用页面的另一个区域。如果我加上a,它就能工作

<div id="mylink" /> 

and for the link do:

链接是这样的:

[My link](#mylink)

But my guess is that there's some other way to do an in-page link in Markdown that doesn't involve the straight up div tag.

但是我猜还有其他方法可以在Markdown中进行页内链接,而不涉及直接向上的div标记。

Any ideas?

什么好主意吗?

4 个解决方案

#1


130  

See this answer.

看到这个答案。

In summary make a destination with

总而言之,做一个目的地

<a name="sometext"></a>

inserted anywhere in your markdown markup (for example in a header:

在markdown标记的任何地方插入(例如在header中:

## heading<a name="headin"></a>

and link to it using the markdown linkage:

链接到它使用markdown链接:

[This is the link text](#headin)

or

[some text](#sometext)

Don't use <div> -- this will mess up the layout for many renderers.

不要使用

——这会使许多渲染器的布局混乱。

(I have changed id= to name= above. See this answer for the tedious explanation.)

(我已将id=改为name=上面。请看这个冗长的解释。

#2


12  

With the PHP version of Markdown, you can also link headers to fragment identifiers within the page using a syntax like either of the following, as documented here

使用PHP版本的Markdown,您还可以使用如下所示的语法将页眉链接到页面中的片段标识符

Header 1            {#header1}
========

## Header 2 ##      {#header2}

and then

然后

[Link back to header 1](#header1)
[Link back to header 2](#header2)

Unfortunately this syntax is currently only supported for headers, but at least it could be useful for building a table of contents.

不幸的是,这种语法目前只支持header,但至少对于构建内容表是有用的。

#3


12  

I guess this depends on what you're using to generate html from your markdown. I noticed, that jekyll (it's used by gihub.io pages by default) automatically adds the id="" attribute to headings in the html it generates.

我猜这取决于你用什么从你的标记生成html。我注意到,jekyll(这是gihub用的)。io页面默认)自动将id=""属性添加到它生成的html中的标题中。

For example if you're markdown is

例如,如果你是markdown

My header
---------

The resulting html will look like this:

生成的html将如下所示:

<h2 id="my-header">My header</h2>

So you can link to it simply by [My link](#my-header)

你可以通过[My link](# My -header)链接到它

#4


1  

The destination anchor for a link in an HTML page may be any element with an id attribute. See Links on the W3C site. Here's a quote from the relevant section:

HTML页面中链接的目标锚点可以是任何具有id属性的元素。请参阅W3C站点上的链接。以下是相关章节的引用:

Destination anchors in HTML documents may be specified either by the A element (naming it with the name attribute), or by any other element (naming with the id attribute).

HTML文档中的目标锚可以由元素(用name属性命名)指定,也可以由任何其他元素(用id属性命名)指定。

Markdown treats HTML as HTML (see Inline HTML), so you can create your fragment identifiers from any element you like. If, for example, you want to link to a paragraph, just wrap the paragraph in a paragraph tag, and include an id:

Markdown将HTML视为HTML(参见内联HTML),因此您可以从任何您喜欢的元素创建片段标识符。例如,如果您想要链接到一个段落,只需在段落标记中包装段落,并包含一个id:

<p id="mylink">Lorem ipsum dolor sit amet...</p>

Then use your standard Markdown [My link](#mylink) to create a link to fragment anchor. This will help to keep your HTML clean, as there's no need for extra markup.

然后使用标准的Markdown [My link](#mylink)创建到片段锚的链接。这将有助于保持HTML的整洁,因为不需要额外的标记。

#1


130  

See this answer.

看到这个答案。

In summary make a destination with

总而言之,做一个目的地

<a name="sometext"></a>

inserted anywhere in your markdown markup (for example in a header:

在markdown标记的任何地方插入(例如在header中:

## heading<a name="headin"></a>

and link to it using the markdown linkage:

链接到它使用markdown链接:

[This is the link text](#headin)

or

[some text](#sometext)

Don't use <div> -- this will mess up the layout for many renderers.

不要使用

——这会使许多渲染器的布局混乱。

(I have changed id= to name= above. See this answer for the tedious explanation.)

(我已将id=改为name=上面。请看这个冗长的解释。

#2


12  

With the PHP version of Markdown, you can also link headers to fragment identifiers within the page using a syntax like either of the following, as documented here

使用PHP版本的Markdown,您还可以使用如下所示的语法将页眉链接到页面中的片段标识符

Header 1            {#header1}
========

## Header 2 ##      {#header2}

and then

然后

[Link back to header 1](#header1)
[Link back to header 2](#header2)

Unfortunately this syntax is currently only supported for headers, but at least it could be useful for building a table of contents.

不幸的是,这种语法目前只支持header,但至少对于构建内容表是有用的。

#3


12  

I guess this depends on what you're using to generate html from your markdown. I noticed, that jekyll (it's used by gihub.io pages by default) automatically adds the id="" attribute to headings in the html it generates.

我猜这取决于你用什么从你的标记生成html。我注意到,jekyll(这是gihub用的)。io页面默认)自动将id=""属性添加到它生成的html中的标题中。

For example if you're markdown is

例如,如果你是markdown

My header
---------

The resulting html will look like this:

生成的html将如下所示:

<h2 id="my-header">My header</h2>

So you can link to it simply by [My link](#my-header)

你可以通过[My link](# My -header)链接到它

#4


1  

The destination anchor for a link in an HTML page may be any element with an id attribute. See Links on the W3C site. Here's a quote from the relevant section:

HTML页面中链接的目标锚点可以是任何具有id属性的元素。请参阅W3C站点上的链接。以下是相关章节的引用:

Destination anchors in HTML documents may be specified either by the A element (naming it with the name attribute), or by any other element (naming with the id attribute).

HTML文档中的目标锚可以由元素(用name属性命名)指定,也可以由任何其他元素(用id属性命名)指定。

Markdown treats HTML as HTML (see Inline HTML), so you can create your fragment identifiers from any element you like. If, for example, you want to link to a paragraph, just wrap the paragraph in a paragraph tag, and include an id:

Markdown将HTML视为HTML(参见内联HTML),因此您可以从任何您喜欢的元素创建片段标识符。例如,如果您想要链接到一个段落,只需在段落标记中包装段落,并包含一个id:

<p id="mylink">Lorem ipsum dolor sit amet...</p>

Then use your standard Markdown [My link](#mylink) to create a link to fragment anchor. This will help to keep your HTML clean, as there's no need for extra markup.

然后使用标准的Markdown [My link](#mylink)创建到片段锚的链接。这将有助于保持HTML的整洁,因为不需要额外的标记。