简体   繁体   English

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

[英]In Markdown, what is the best way to link to a fragment of a page, i.e. #some_id?

I'm trying to figure out how to reference another area of a page with Markdown.我试图弄清楚如何使用 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.但我的猜测是,还有其他方法可以在 Markdown 中创建不涉及直接div标签的页内链接。

Any ideas?有任何想法吗?

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 标记中的任何位置(例如在标题中:

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

and link to it using the markdown linkage:并使用降价链接链接到它:

[This is the link text](#headin)

or或者

[some text](#sometext)

Don't use <div> -- this will mess up the layout for many renderers.不要使用<div> - 这会弄乱许多渲染器的布局。

(I have changed id= to name= above. See this answer for the tedious explanation.) (我已将id=更改name=上面的name= 。有关乏味的解释,请参阅此答案。)

I guess this depends on what you're using to generate html from your markdown.我想这取决于您用于从 Markdown 生成 html 的内容。 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.我注意到,jekyll(默认情况下由 gihub.io 页面使用)会自动将 id="" 属性添加到它生成的 html 中的标题。

For example if you're markdown is例如,如果你的降价是

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)链接到它

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使用MarkdownPHP 版本,您还可以使用以下任一语法将标题链接到页面内的片段标识符,如此处所述

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.不幸的是,此语法目前仅支持标题,但至少它对构建目录很有用。

The destination anchor for a link in an HTML page may be any element with an id attribute. HTML 页面中链接的目标锚点可以是任何具有id属性的元素。 See Links on the W3C site.请参阅 W3C 站点上的链接 Here's a quote from the relevant section:这是相关部分的引用:

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 文档中的目标锚可以由 A 元素(使用 name 属性命名)或任何其他元素(使用 id 属性命名)指定。

Markdown treats HTML as HTML (see Inline HTML ), so you can create your fragment identifiers from any element you like. Markdown 将 HTML 视为 HTML(请参阅内联 HTML ),因此您可以从您喜欢的任何元素创建片段标识符。 If, for example, you want to link to a paragraph, just wrap the paragraph in a paragraph tag, and include an id:例如,如果您想链接到一个段落,只需将该段落包装在一个段落标记中,并包含一个 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.然后使用您的标准 Markdown [My link](#mylink)创建一个链接到片段锚点。 This will help to keep your HTML clean, as there's no need for extra markup.这将有助于保持您的 HTML 干净,因为不需要额外的标记。

For anyone use Visual Studio Team Foundation Server (TFS) 2015, it really does not like embedded <a> or <div> elements, at least in headers.对于任何使用 Visual Studio Team Foundation Server (TFS) 2015 的人来说,它确实不喜欢嵌入的<a><div>元素,至少在标题中是这样。 It also doesn't like emoji in headers either:它也不喜欢标题中的表情符号:

### 🔧 Configuration 🔧

Lorem ipsum problem fixem.

Gets translated to:被翻译成:

<h3 id="-configuration-">🔧 Configuration 🔧</h3>
<p>Lorem ipsum problem fixem.</p>

And so links should either use that id (which breaks this and other preview extensions in Visual Studio), or remove the emoji:因此链接应该使用该id (这会破坏 Visual Studio 中的这个和其他预览扩展),或者删除表情符号:

Here's [how to setup](#-configuration-) //🔧 Configuration 🔧
Here's [how to setup](#configuration) //Configuration

Where the latter version works both online in TFS and in the markdown preview of Visual Studio.后一个版本在 TFSVisual Studio 的降价预览中都可以在线工作。

In Pandoc Markdown you can set anchors on arbitrary spans inside a paragraph using syntax [span]{#anchor} , eg:在 Pandoc Markdown 中,您可以使用语法[span]{#anchor}在段落内的任意跨度上设置锚点,例如:

Paragraph, containing [arbitrary text]{#mylink}.

And then reference it as usual: [My link](#mylink) .然后像往常一样引用它: [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:如果你想引用整个段落,那么最直接的方法是在段落的开头添加一个跨度:

[]{#mylink}
Paragraph text.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何不使用id链接到外部网站的特定元素? (即在锚标记的href网址末尾不使用“#some-id”)? - How can I link to a specific element of an external website WITHOUT using id? (i.e. NOT using “#some-id” at the end of my anchor tag's href url)? DOMPDF:如何强制 dompdf 忽略一些 html 页面内容(即链接、<a>标签)</a> - DOMPDF: how to force dompdf to ignore some html page content (i.e. links, <a> tags) 强制IE中的链接在Firefox中打开 - Force a link in I.E. to open in Firefox 有没有办法使用他们的变量(即 %%title%%)在页面内获得 yoast 标题 - Is there any way to get yoast title inside page using their variable ( i.e. %%title%%) 有没有简单的方法(即不是完全破解)来重新定位这些元素? - Is there a simple way (i.e. not a total hack) to reposition these elements? 获取页面中最大的嵌入/对象,即最大尺寸 - Get the largest embed/object in a page i.e. largest dimensions 降价样式的最佳方法 - Best way to style markdown 将降价作为道具传递给 Astro 组件的最佳方法是什么 - What's the best way to pass markdown to an Astro component as a prop CSS的哪一部分需要修改以使此工作即 - What part of CSS needs to be modified to make this work on i.e. 在页面中间进行一些控制以消耗剩余的顶点空间的最佳方法是什么? - What is the best way to have some control in the middle of a page to consume remaining verticle space?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM