Handling anchors in posts created with markdown

Hey community, I am using a discourse site to publish some specs that are primarily written in markdown, but I came to the problem that the anchors used for the headers are being rendered, while they should be hidden.

Does anyone know if there is a way to hide them?

2 Likes

Hey, welcome!

Is this syntax from kramdown or PHP Markdown Extra?

Discourse relies on MarkdownIt, which is based on CommonMark spec. It doesn’t support specifying a header ID.

It seems that they discussed it at some point: Anchors in markdown - Spec - CommonMark Discussion, but I don’t see any definitive conclusion.

However, Discourse generates anchor automatically.
You can see it here:

Essentially, you can expect those IDs:

  • h-{headingId} (no post ID, no slug)
  • h-{slug}-{headingId} (no post ID)
  • p-{postId}-h-{headingId} (post ID, no slug)
  • p-{postId}-h-{slug}-{headingId} (post ID, slug)

E.g.:

If you prefer, you can still create an anchor manually with HTML, e.g., <a name="anchor_name"></a>


Additional resources –
If you need to generate a Table of Contents, there is this component for you:

5 Likes

Thanks a lot @Arkshine!

The issue is more because we are publishing the markdowns used for other reasons in discourse as well, so the people who keep the documents use these anchors for cross-reference when merging multiple markdowns in a single document.

I think I will make some sort of script to clean up before posting the content.

3 Likes

I see!

The solution is also to make a plugin for Discourse with a markdown plugin to support this syntax. If you have some budget, you could ask Marketplace.

@Nihokam

If you’re still interested, I made a tiny Discourse plugin that handles specifying an anchor ID next to the heading.

It works alongside Discourse’s automatic heading anchors.
Note that it’s basic support. There are probably a lot of variants, but this plugin supports only the following:

## My Heading {#custom_section}

will generate:

<a name="custom_section" href="#custom_section" class="custom-anchor-id"></a>

You can give it a try! Let me know if you have any issues with it.

2 Likes