Proposal

This ticket is a proposal about automatically adding clickable anchor links and table of contents links for documentation headings on Drupal.org.

User Story

As non-developers & newcomers to Drupal we need clickable anchor icons in the documentation pages, so that:

  • we can easily and quickly copy a link/url to a specific documentation page section
  • we have a visual reminder about available clickable anchor icons

Assumptions

  • Assumes the clickable anchor icons is clear and meaningful to all audience. Not just some audience(s).

Options

The following shows options to choose from about clickable anchor icons. To test simply click on any anchor below.


Option 1: Clickable Anchor Character

Example

Section Header

Examples in context

Code

<h4><a id="option-1---anchor" name="option-1---anchor"></a>Section Header <a href="#option-1---anchor">⚓</a></h4>


Option 2: Clickable Prefix Hastag

Example

# Section Header

Examples in context

Code

<h4><a id="option-2---anchor" name="option-2---anchor"></a><a href="#option-2---anchor">#</a> Section Header</h4>


Option 3: Clickable Hastag Suffix

Example

Section Header #

Examples in context

  • TBD

Code

<h4><a id="option-3---anchor" name="option-3---anchor"></a>Section Header <a href="#option-3---anchor">#</a></h4>


Option 4: Full Text Anchor

Example

Writing and contributing documentation

Examples in context

Code

<h4><a id="option-4---anchor" name="option-4---anchor">Writing and contributing documentation</a></h4>


Option 5: Use the TOC

  1. Don't add visible anchor links to short pages without a TOC
  2. If a page has a TOC, the items in the TOC are visible anchor links.

Deployed solution

Option 2 with Clickable Prefix Hastag is used in the implementation that has gone live on drupal.org.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Francewhoa created an issue. See original summary.

Francewhoa’s picture

Issue summary: View changes

Added "Issue summary"

Francewhoa’s picture

Issue summary: View changes

@Francewhoa :) Note to myself. To facilitate cross-ticket communications, added invisible anchors.

Francewhoa’s picture

Issue summary: View changes

Hi drumm :) Thanks for all your Drupal contributions. This is my reply to your comment #9

Do you have a link to this discussion? Drupal.org has only supported emoji for about 8 months, #2463607: Drupal.org chokes on emojis

I added a summary of options here. Those were some of the end results of some general consensus about clickable anchors. Each option include link to real example(s). Including those contributed by hansfn.

BTW those past discussions were spread over multiple channels. Including but not limited to IRC, documentation comments, forum comments, and so on. And spreading years. I would love to go dig each one of those, but that's not realistic with my present workload. Speaking for myself I don't need those past discussions. I propose we focus our effort on the present audiences needs. But if you really need all of those past discussion you're welcome to dig in, search, compile, and add them to this ticket :)


I haven't seen a literal ⚓️ emoji used like this anywhere else on the internet. The convention I know about is a #, which is on a few pages throughout Drupal.org, but not standardized.

That's also my understanding. I also recall that initially the clickable anchor icons were hashtags #, those were used years back, the ⚓️ emoji came in later, and replaced the hashtags in a specific context. Which include and is limited to the documentation book and their sub-pages for newcomers and non-developers, which are listed in the "Scope" section.

I was not proposing to standardized the clickable anchor icons. I'm assuming hansfn was proposing this. I will let him clarify his proposal. Speaking for myself I proposed this User Story and its Assumptions.

hansfn’s picture

Issue summary: View changes

BTW those past discussions were spread over multiple channels. Including but not limited to IRC, documentation comments, forum comments, and so on. And spreading years. I would love to go dig each one of those, but that's not realistic with my present workload.

I think drumm's and my reaction comes from the fact that we expected "past discussions" to be a discussion in the drupal.org issue queue - with a conclusion. Anyway, let's focus on the task at :-)

I'll add a 5 option to the summary:

  1. Don't add visible anchor links to short pages without a TOC
  2. If a page has a TOC, the items in the TOC are visible anchor links.

I wonder if the first point, "Don't add visible anchor links to short pages without a TOC" could be a general rule.

Have to run!

hansfn’s picture

Issue summary: View changes
bertboerland’s picture

if you want to have visual anchors, I like the way http://guide.bash.academy/inception/ is doing it. otherwise, the making jist the title clickable is the norm.

grasmash’s picture

I'm adding the "Documentation Initiative" tag as this is a related UX improvement related.

I'd like to cast a vote for 2 things:

  • Use a clickable # prefix. Laravel documentation uses it, and it works well. Making a prefix allows the hash tag to be consistently placed. If it were a suffix, its position would change based on title length.
  • Create a table of contents listing links to anchors
ressa’s picture

To make maintenance easier, how about adding anchors as well as TOC automatically, based on h2, h3 and h4 headers on a page?

@bertboerland's example of visualization in #7 at http://guide.bash.academy/inception/ is interesting, not only because it uses automatic anchors and TOC, generated from h2, h3 or h4 headers, from this script: https://guide.bash.academy/js/main.js

Scripts disabled

<h2>
What's going on here?  Text, terminals, bash, programs, input, output!
</h2>

Scripts enabled

<h2 id="h2.2">
<a class="bookmark" href="?=What's_going_on_here?_Text,_terminals,_bash,_programs,_input,_output!#h2.2">#</a>
What's going on here?  Text, terminals, bash, programs, input, output!
</h2>

From: http://guide.bash.academy/inception/

hansfn’s picture

Just a comment about automatic TOC and clickable anchors: Great, really great, but numeric anchor IDs aren't stable. We should use anchors IDs based on heading text which are slightly more stable since they only change if someone changes the text. Numeric anchors change every time someone adds or remove a heading (at the same level).

Maybe the golden middle way could be to insert anchors manually for the headings that we know will be linked to from Drupal core itself or other documentation, and use autmatic numeric anchors for the rest? (The script should use any existing anchors, only generate when missing.)

ressa’s picture

numeric anchor IDs aren't stable. We should use anchors IDs based on heading text which are slightly more stable since they only change if someone changes the text.

I agree, numerical anchors are inferior to the more stable header, as seen at Wikipedia:
https://en.wikipedia.org/wiki/Protein#Cell_signaling_and_ligand_binding

grasmash’s picture

Trying to consolidate this conversation into a list of requirements:

The requirements appear to be:

  • Drupal.org documentation should automatically generate anchors for h2, h3, and h4 headings.
    • Anchors should use non-numerical names that are generated based on heading text.
    • The anchors styled appear as clickable hash tag prefixes for headings.
  • A table of contents should be automatically generated displaying all heading anchor links.

Agree or disagree?

ressa’s picture

Thanks for summarizing @grasmash, that looks very good to me. Perhaps it could be added, that the table of content should be indented, helping visualize the hierarchy?

grasmash’s picture

  • Drupal.org documentation should automatically generate anchors for h2, h3, and h4 headings.
    • Anchors should use non-numerical names that are generated based on heading text.
    • The anchors styled appear as clickable hash tag prefixes for headings.
  • A table of contents should be automatically generated displaying all heading anchor links.
    • The TOC should be indented to properly represent the hierarchy of headings. E.g., h5 would appear indented under h4.
ressa’s picture

Nice, thanks!

hansfn’s picture

The summary in comment 14 looks good. (A detail regarding the TOC: I think we should ignore h5 and below.)

When we "all" agree on this, we should move the issue to the drupal.org queue. Hopefully the needed JavaScript can be added quite quickly.

grasmash’s picture

Project: Documentation »
Component: Placement/Navigation/Structure » Code
Category: Plan » Feature request

Updated reqs as per #16:

  • Drupal.org documentation pages should automatically generate anchors for h2, h3, and h4 headings.
    • Anchors should use non-numerical names that are generated based on heading text.
    • The anchors will be styled to appear as clickable hash tag prefixes for headings. See Laravel documentation for example.
  • A table of contents should be automatically generated displaying all h2, h3, and h4 anchor links.
    • The TOC should be indented to properly represent the hierarchy of headings. E.g., h4 would appear indented under h2. See Laravel documentation for example (top TOC).

All respondents in the last month seem to agree on this list, that would seem to constitute a quorum? I'm moving this issue to Drupal.org Enhancement queue for further work.

Note that the implementation method is not part of the requirements. I'd like someone from the Drupal.org team, with knowledge of its architecture, to provide guidance for how we should implement these requirements. E.g., using javascript vs. text formatters, etc. I'd personally lean toward using a text formatter so that the markup is modified server-side, cached, and delivered to the client in its final form.

grasmash’s picture

Issue summary: View changes

It looks like this issue started as a proposal to manually and optionally add anchor links as a matter of docs standards. It has since evolved into a feature request to automatically generate anchors. Changing title and description to match new state.

grasmash’s picture

Title: Add Optional Clickable Anchors to Doc Guidelines? » Add Clickable Anchors and Table of Contents to Documentation Pages
ressa’s picture

I had a look at the available Drupal 7 TOC modules, and if using a text formatter is preferred, as opposed to using javascript, then the TOC Node module might serve as a starting point.

It does not use the title for anchor, but generates a numerical anchor, and also doesn't insert visible anchors at the destination, so these two features have to be added.

drumm’s picture

Project: » Drupal.org customizations
Version: » 7.x-3.x-dev
dqd’s picture

I'd personally lean toward using a text formatter so that the markup is modified server-side, cached, and delivered to the client in its final form.

While not completely disagreeing, I would like to point to the aspect that this TOCs are actually only small footprints on the clients side but a huge footprint in summary on the servers side while pages are in edit wars. Since Drupal.org has already a huge amount of server side processing in its pockets.

grasmash’s picture

@drumm Do you have any pointers for implementation? I'd like to make this issue actionable.

drumm’s picture

I had a look at the available Drupal 7 TOC modules, and if using a text formatter is preferred, as opposed to using javascript, then the TOC Node module might serve as a starting point.

It does not use the title for anchor, but generates a numerical anchor, and also doesn't insert visible anchors at the destination, so these two features have to be added.

This probably won’t use the text/filter format API, that’s not meant to be context-aware, it doesn’t know what field or content type it is processing.

toc_node looks like it acts on every page load, toc_node_node_post_render(), which isn’t ideal, since that does HTML parsing and manipulation.

I think a field formatter might be best, especially if the results land in the field cache.

An advantage of server-side processing is that it can attempt to keep anchors going to the same place as revisions are made and headings are edited. toc_node looks like it does this; I haven't dug into the algorithm to see how effective it might be.

grasmash’s picture

I've created a fork of the Table of Contents module and posted it in #2983144: Refactor module to provide and use field formatter.

The fork provides all of the TOC functionality required.

Provides a field formatter for text fields which will:

  • Add anchors to configured selectors (h2, h3, h4). Anchors are named using text contents of selector.
  • Create a block with a table of contents based on those selectors.

The rest of the functionality can be achieved with custom styling.

@drumm can you review the implementation in the fork and assert that it is acceptable for Drupal.org? Also, what is the preferred way to use a patched or forked module in the Drupal.org codebase?

I'll follow up with a patch to the drupalorg project once the implementation is approved.

grasmash’s picture

Status: Active » Needs review

Need a quick review to determine next steps.

If implementation is approved, this issue will require patches that:

  • Add fork of table_of_contents to Drupal.org
  • Add simplehtmldom library to Drupal.org sites/all/libraries
  • Update the default content for new documentation nodes:
    • The field formatter must use the new Table of Contents format
    • The table of contents block must be added to the right hand sidebar
drumm’s picture

Instead of the new library, can we use filter.module’s filter_dom_load() and related functions?

grasmash’s picture

I was not aware of filter_dom_load(), I'll look into it.

grasmash’s picture

I think that using DOMDocument, which used by filter_dom_load(), would limit the functionality of the module.

From what I can tell, it's not possible to use CSS selectors like "h2.toc" with DOMDocument. We'd need to limit selector specification to _only_ class or _only_ tag, and the parsing would be much clunkier.

In short, we _could_ use it, but it would require a degradation in functionality and would add more complexity to the module itself.

grasmash’s picture

This issue has been in "needs review" status for 27 days. How can this be moved forward?

grasmash’s picture

Adding patches. Steps to merge:

grasmash’s picture

@drumm asked for a performance review before merging this.

drumm’s picture

I confirmed that the DOM was processed twice per page load and not cached.

Looking at table_of_contents’s 8.x branch, I also found a way to use selectors with PHP’s DOM library - symfony/css-selector.

Both of these are addressed by #3072354: Add caching, use PHP’s DOMDocument and symfony/css-selector.

drumm’s picture

We’ll also need #3072743: Allow configuring block when field is only displayed via Panelizer so the table of contents block pane doesn’t disappear.

  • drumm committed 4881048 on 7.x-3.x authored by grasmash
    Issue #2943370 by grasmash, drumm: Add Clickable Anchors and Table of...
drumm’s picture

Status: Needs review » Fixed

This is now deployed!

I’ve updated the 3 example pages linked from the issue summary to remove the bespoke anchors and let the automated table of contents take over:

IDs are automatically assigned based on the heading content, if there is not already an id attribute on the heading tag. When updating pages, be sure to move over any existing IDs.

drumm’s picture

hansfn’s picture

Issue summary: View changes

Thank you, drumm. I'm so happy to see this live!

ressa’s picture

Thank you @grasmash @drumm and everybody else involved with getting this done! It looks really great, and will be super useful for getting an overview of documentation pages, as well as allow linking to a certain documentation page section.

grasmash’s picture

@neil Thanks so much for the merge!

At present, it looks like the new capability is not present on the Official Documentation pages, like:
https://www.drupal.org/docs/official_docs/en/_local_development_guide.html

Can it be added there?

drumm’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Francewhoa’s picture

Thanks all for your contributions :) I look forward to trying it

Francewhoa’s picture

Issue summary: View changes

@bertboerland, drumm, grasmash, and all contributors :) Done I tried it using those examples above.

Wow. It is fantastic. I love it. Easy to use, looks good, and filled with automation-magic. I like that the # sign is faded a little. It keeps the focus on the page content on the right side. While at the same time the anchor is visible. Plus the # signs are always display at the same location. So easy to recall & find when users need one. I also noticed the automated "On this page" table of content display around the top right side of the doc page. Pure-joy :)