# Getting heading links and anchor ids more easily

**URL:** <https://forum.xwiki.org/t/getting-heading-links-and-anchor-ids-more-easily/14185>\
**Category:** Development\
**Tags:** proposal, usability\
**Created:** [March 1, 2024, 2:17pm UTC](https://forum.xwiki.org/t/getting-heading-links-and-anchor-ids-more-easily/14185 "2024-03-01T14:17:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vmassol](https://forum.xwiki.org/user_avatar/forum.xwiki.org/vmassol/32/6_2.png) [@vmassol](https://forum.xwiki.org/u/vmassol)\
**Post date:** [March 1, 2024, 2:17pm UTC](https://forum.xwiki.org/t/getting-heading-links-and-anchor-ids-more-easily/14185/1 "2024-03-01T14:17:17Z")

</div>

Hi devs,

Context:

> [@Content-independent generated ids for Headings and Images](https://forum.xwiki.org/t/content-independent-generated-ids-for-headings-and-images/14161/11):
>
> That would be a nice feature for todays strategy too. I always open up the dev console to get the right id or if there is a toc inserted click and jump to the heading and copy it from the adress bar. In principle the ids for headings are guessable more or less. Headings in a language with Umlauts are a hard guess sometimes.

Here’s one usability feature I’d like to discuss: the ability, in view mode, to hover over a heading with the mouse and display an anchor icon, in order to get a link with an anchor or copy the anchor id in the clipboard (so that it can be pasted when editing a page and linking to some heading). Something similar to:

- [Creating a Vue Application | Vue.js](https://vuejs.org/guide/essentials/application.html)
- [Build Scan® | Develocity](https://ge.xwiki.org/s/fooar7yu3hqs4)

It’s also similar to the Lightbox app which does this for images: [https://extensions.xwiki.org/xwiki/bin/view/Extension/Image%20Lightbox%20Application/](https://extensions.xwiki.org/xwiki/bin/view/Extension/Image%20Lightbox%20Application/)

Note: one harder part is to support is that we would need to support also this construct:

```auto
{{id name="myid"/}}
== some heading ==

```

which would generate in HTML something like:

```auto
<div .../>
<h2>...</h2>

```

WDYT?

Thanks

---

<div class="post-metadata">

**Author:** ![tmortagne](https://forum.xwiki.org/user_avatar/forum.xwiki.org/tmortagne/32/4930_2.png) [@tmortagne](https://forum.xwiki.org/u/tmortagne)\
**Post date:** [March 1, 2024, 2:24pm UTC](https://forum.xwiki.org/t/getting-heading-links-and-anchor-ids-more-easily/14185/2 "2024-03-01T14:24:21Z")

</div>

+1 for the general concept, it seems to be a pretty common feature, and it’s indeed very useful (I can think of quite a few others examples with the same behavior as the 2 you indicated)

---

<div class="post-metadata">

**Author:** ![tmortagne](https://forum.xwiki.org/user_avatar/forum.xwiki.org/tmortagne/32/4930_2.png) [@tmortagne](https://forum.xwiki.org/u/tmortagne)\
**Post date:** [March 1, 2024, 2:27pm UTC](https://forum.xwiki.org/t/getting-heading-links-and-anchor-ids-more-easily/14185/3 "2024-03-01T14:27:06Z")

</div>

> [@vmassol](#):
>
> Note: one harder part is to support is that we would need to support also this construct:
> 
> ```auto
> {{id name="myid"/}}
> == some heading ==
> 
> ```

I’m not sure we need to. Or to me more accurate we could decide to introduce this feature both for the headings and the id macro (since the goal of the id macro is to insert an anchor).

---

<div class="post-metadata">

**Author:** ![MichaelHamann](https://forum.xwiki.org/user_avatar/forum.xwiki.org/michaelhamann/32/3813_2.png) [@MichaelHamann](https://forum.xwiki.org/u/MichaelHamann)\
**Post date:** [March 1, 2024, 2:30pm UTC](https://forum.xwiki.org/t/getting-heading-links-and-anchor-ids-more-easily/14185/4 "2024-03-01T14:30:38Z")

</div>

I don’t think we ever advised to put an id macro before a heading. I think our advice was to put it inside the heading. See, e.g., the examples in the documentation for the [numbered reference macro](https://extensions.xwiki.org/xwiki/bin/view/Extension/Numbered%20Reference%20Macro/#HNumberedHeadings).

How this seems to work is that they put an HTML link inside or before the element to be linked to and display that link when hovering the heading. The Vue documentation seems to be the better example here in terms of accessibility as it manages to support keyboard navigation by just hiding the link visually using an opacity of 0 and showing it with an opacity of 1 when the link is focused.

For this to work in XWiki, I think we would need a way to mark generated content to prevent converting this link back into the XWiki syntax when parsing the HTML from the WYSIWYG editor. Now such a marker is something we could use in other places, too, so this would be good to introduce, anyway.

So +1 from my side for the concept.

---

<div class="post-metadata">

**Author:** ![vmassol](https://forum.xwiki.org/user_avatar/forum.xwiki.org/vmassol/32/6_2.png) [@vmassol](https://forum.xwiki.org/u/vmassol)\
**Post date:** [March 1, 2024, 2:55pm UTC](https://forum.xwiki.org/t/getting-heading-links-and-anchor-ids-more-easily/14185/5 "2024-03-01T14:55:01Z")

</div>

> [@MichaelHamann](#):
>
> I don’t think we ever advised to put an id macro before a heading. I think our advice was to put it inside the heading. See, e.g., the examples in the documentation for the [numbered reference macro](https://extensions.xwiki.org/xwiki/bin/view/Extension/Numbered%20Reference%20Macro/#HNumberedHeadings).

I can clearly remember several times where we’ve shown this pattern of the id macro before a heading (and even used it ourselves in platform and contrib - I’ve searched and it’s not used anymore in platform, good). Now, fortunately, I don’t think it’s ever been written on [xwiki.org](http://xwiki.org) anywhere so that makes it easier to deprecate the (bad) practice 🙂

One reason you see [https://extensions.xwiki.org/xwiki/bin/view/Extension/Numbered%20Reference%20Macro/#HNumberedHeadings](https://extensions.xwiki.org/xwiki/bin/view/Extension/Numbered%20Reference%20Macro/#HNumberedHeadings) is because 1) it’s recent and 2) we cannot have the id macro used outside the figure caption. It just wouldn’t work. It’s also less correct, I agree.

You can see some examples used at [Code search results · GitHub](https://github.com/search?q=org%3Axwiki-contrib%20%22%7B%7Bid%20name%3D%22&type=code)

I agree that we don’t need to support the id macro outside of the headings. We might need to support the id macro used inside headings though, as the first element.

> [@MichaelHamann](#):
>
> Now such a marker is something we could use in other places, too, so this would be good to introduce, anyway.

An example of such a need is Rendering Transformations. I don’t recall but that’s probably already supported somehow (to protect some portion of HTML). Maybe that could be reused.

Thx

---

<div class="post-metadata">

**Author:** ![tmortagne](https://forum.xwiki.org/user_avatar/forum.xwiki.org/tmortagne/32/4930_2.png) [@tmortagne](https://forum.xwiki.org/u/tmortagne)\
**Post date:** [March 1, 2024, 3:14pm UTC](https://forum.xwiki.org/t/getting-heading-links-and-anchor-ids-more-easily/14185/6 "2024-03-01T15:14:30Z")

</div>

> [@MichaelHamann](#):
>
> Now such a marker is something we could use in other places, too, so this would be good to introduce, anyway.

Yes. See [Loading...](https://jira.xwiki.org/browse/XRENDERING-78).

> [@vmassol](#):
>
> that’s probably already supported somehow

Nop.

---

<div class="post-metadata">

**Author:** ![vmassol](https://forum.xwiki.org/user_avatar/forum.xwiki.org/vmassol/32/6_2.png) [@vmassol](https://forum.xwiki.org/u/vmassol)\
**Post date:** [June 17, 2024, 2:10pm UTC](https://forum.xwiki.org/t/getting-heading-links-and-anchor-ids-more-easily/14185/7 "2024-06-17T14:10:00Z")

</div>

So we agree about the idea of adding this feature. I’ve now created [Loading...](https://jira.xwiki.org/browse/XWIKI-22270)
