在 Markdown 中,链接到页面片段(即#some_id)的最佳方式是什么?
我试图弄清楚如何使用 Markdown 引用页面的另一个区域。 添加一个 and ,我就可以让它工作
<div id="mylink" />
如果我为链接
[My link](#mylink)
:但我的猜测是,还有其他一些方法可以在 Markdown 中创建页内链接,而不涉及直接的 div
标签。
有什么想法吗?
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
<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.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请参阅此答案。
总之,
在 Markdown 标记中的任何位置插入一个目标(例如在标题中:
并使用 Markdown 链接链接到它:
或
不要使用
——这会搞砸许多渲染器的布局
(我已将上面的
id=
更改为name=
。请参阅See this answer.
In summary make a destination with
inserted anywhere in your markdown markup (for example in a header:
and link to it using the markdown linkage:
or
Don't use
<div>
-- this will mess up the layout for many renderers.(I have changed
id=
toname=
above. See this answer for the tedious explanation.)我想这取决于你用来从 markdown 生成 html 的内容。我注意到,jekyll(默认情况下由 gihub.io 页面使用)会自动将 id="" 属性添加到它生成的 html 中的标题中。
例如,如果你的 markdown 是
生成的 html 将如下所示:
因此你可以通过
[My link](#my-header)
链接到它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.
For example if you're markdown is
The resulting html will look like this:
So you can link to it simply by
[My link](#my-header)
通过 PHP 版本的 Markdown,您还可以使用类似于以下任一语法,如此处所述
,然后
不幸的是这个目前仅支持标题语法,但至少它对于构建目录很有用。
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
and then
Unfortunately this syntax is currently only supported for headers, but at least it could be useful for building a table of contents.
HTML 页面中链接的目标锚点可以是具有
id
属性的任何元素。请参阅 W3C 网站上的链接。这是相关部分的引用:Markdown 将 HTML 视为 HTML(请参阅 内联 HTML),因此您可以创建片段标识符来自任何你喜欢的元素。例如,如果您想链接到一个段落,只需将该段落包装在段落标记中,并包含一个 id:
然后使用标准 Markdown
[My link](#mylink)
创建一个链接到片段锚点。这将有助于保持 HTML 干净,因为不需要额外的标记。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: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:
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.对于任何使用 Visual Studio Team Foundation Server (TFS) 2015 的人来说,它确实不喜欢嵌入
或
元素,至少在标头中是如此。它也不喜欢标题中的表情符号:
For anyone use Visual Studio Team Foundation Server (TFS) 2015, it really does not like embedded
<a>
or<div>
elements, at least in headers. It also doesn't like emoji in headers either:Gets translated to:
And so links should either use that
id
(which breaks this and other preview extensions in Visual Studio), or remove the emoji:Where the latter version works both online in TFS and in the markdown preview of Visual Studio.
在 Pandoc Markdown 中,您可以使用语法
[span]{#anchor}
在段落内的任意跨度上设置锚点,例如:然后照常引用它:
[My link](#mylink)
。如果您想引用整个段落,那么最直接的方法是在段落开头添加一个空跨度:
In Pandoc Markdown you can set anchors on arbitrary spans inside a paragraph using syntax
[span]{#anchor}
, e.g.:And then reference it as usual:
[My link](#mylink)
.If you want to reference a whole paragraph then the most straightforward way is to add an empty span right in the beginning of the paragraph: