Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add anchors for headings in handbook pages #115

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

richardswain
Copy link

When deep-linking handbook content it's not easy to get a URL directly to the relevant heading ie:

  1. Page https://handbook.hmn.md/working-here/continuous-improvement/personal-development-plan/
  2. Relevant heading of interest HOW AND WHEN SHOULD I UPDATE MY PDP?
  3. There is no way for readers to quickly get a link to sub-content or headings.

Why this is useful? When sharing specific handbook information (policies, or specific policies etc) we will share the page link, then tell the person to scroll to x heading. So much mental weight can be lifted by matching the reader experience with current day expectations.

Related: https://github.com/humanmade/H2/issues/418 however - the code there may not be instantly portable.

@richardswain
Copy link

@tareiking Does this work for you?

* @return string $content The filtered post content.
*/
function anchor_content_headings( $content ) {
$content = preg_replace_callback( "/\<h([1|2|3|4|5])\>(.*?)\<\/h([1|2|3|4|5])\>/", function ( $matches ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no guard to prevent this matching a heading which already contains linked text, and a link cannot appear inside a link so one of them would break.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants