在帖子或主题内链接到一个标题

:bookmark: This guide explains how to link directly to a heading within a Discourse post or topic, enabling users to navigate long posts efficiently.

:person_raising_hand: Required user level: All users

Adding headings within long posts can make the content more readable and allow users to link directly to specific sections. This can be particularly useful for navigating extensive discussions and documentation.

Summary

This guide covers:

  • Creating headings using Markdown
  • Linking directly to specific headings in a post

Creating headings using Markdown

To create a heading in Markdown, prefix a line of text with one or more # characters. The number of # characters corresponds to the heading level. Here are some examples:

## This is a level 2 heading
Here is a short paragraph afterwards.

### This is a level 3 heading
Here is another short paragraph.

When rendered, it looks like this:


This is a level 2 heading

Here is a short paragraph afterwards.

This is a level 3 heading

Here is another short paragraph.


Linking directly to a heading

To share a direct link to a heading within a post:

  1. Hover over the heading to reveal a link icon next to it: link icon.
  2. Click the link icon to update the URL in your browser’s address bar with the direct link to that heading.
  3. Copy the updated URL from the address bar to share it with others.

Best practices

  • Use descriptive headings to summarize content clearly.
  • Structure your post using hierarchical headings where appropriate.
  • Ensure that headings are consistent and logical for better readability.

FAQs

Can I link to any part of a post?
Yes, as long as it’s a heading properly formatted in Markdown.

Do links to headings work across different posts?
Yes! These links will direct to the header in the post when used in other topics or shared somewhere else.

Additional resources

Last edited by @JammyDodger 2024-07-09T11:12:13Z

Last checked by @hugh 2024-08-08T01:48:04Z

Check documentPerform check on document:
73 个赞

我在这个网站或我自己的网站上都看不到链接图标。无论是在火狐浏览器还是 Chrome 浏览器中都看不到。

是否发生了什么变化?

您需要将鼠标悬停在标题上才能显示它们:

当我鼠标悬停时,什么也看不到。

似乎确实存在一些东西,但没有显示任何图标。

一个后续问题…

我假设如果更改标题的文本,链接就会断开。

编辑补充:不仅当标题更改时锚点会更改,而且如果在现有标题之前插入另一个标题,则标题上的数字后缀也会更改。这似乎脆弱得毫无用处。

唯一安全的解决方法是将标题包装在 html a 标签中并为其指定一个 id,对吗?

再次编辑补充:我尝试了将标题文本包装在 a 标签中,以及仅在标题文本之前放置空标签。两种 id 都不起作用。我还尝试了 span 标签,结果同样失败。

是的,我可以在 DOM 中将其找到为 before

我使用的是 Windows 11。

我能让链接图标出现的方法是复制链接,然后在新标签页中打开该链接(带有 # 和 id)。这样链接图标就会一直显示(无需悬停),但其他链接图标仍然不会在悬停时出现。

我可以顶一下这个帖子吗?我想知道我是不是遗漏了关于在帖子中链接的某些显而易见的东西。