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

Support Markdown Render Hooks for Headers #6713

Closed
frerich opened this issue Jan 6, 2020 · 9 comments · Fixed by #7133
Closed

Support Markdown Render Hooks for Headers #6713

frerich opened this issue Jan 6, 2020 · 9 comments · Fixed by #7133

Comments

@frerich
Copy link

frerich commented Jan 6, 2020

A useful feature provided by many web applications is the ability to conveniently get a link referencing a specific header in the page. People can then bookmark or share the link in order to reference an individual section. Consider e.g. the Effective Go page and try hovering the mouse above any of the headers with a blue background: the shown paragraph symbol is a link to the respective header. Copying the address of the link gets you the URL referencing the header.

Right now, automatically adding such anchor links is possible in Hugo by filtering the entire HTML markup of a page, e.g. given a partial add-header-anchors.html with

{{ . | replaceRE "(<h[1-9] id=\"([^\"]+)\".+)(</h[1-9]+>)" `${1}<a href="#${2}" class="header-anchor" ariaLabel="Anchor">&para;</a>${3}` | safeHTML }}

One can use

{{ .Content | partial "add-header-anchors" }}

With the advent of Markdown render hooks, this seems like a hack. Instead, it would be great if the HTML generated for headers could be customised using a hook such as layouts/_default/_markup/render-header.html

@frerich
Copy link
Author

frerich commented Jan 6, 2020

The proposed render-header template could be passed a context with the following fields:

  • Page: The Page being rendered
  • Level: The level of the header (1 for top level headers, 2 for sub-headings etc.)
  • Anchor: The generated or assigned anchor for the header
  • Text: The rendered (HTML) header text
  • PlainText: The plain variant of the above.

With this at hand, a header such as

## Getting Started

Could be converted to a header with the paragraph icon link using something along the lines of

<h{{ .Level }}>{{ .Text }}</h{{ .Level }}><a href="#{{ .Anchor }}">&para;</a>

@kaushalmodi
Copy link
Contributor

I like this proposal. Just a minor nitpick .. to call it render-heading instead of render-header as they are technically called headings in the HTML spec.

@frerich
Copy link
Author

frerich commented Jan 8, 2020

I concur - the CommonMark spec also talks about headings, so render-heading seems more appropriate.

kowalcj0 added a commit to kowalcj0/kowalcj0.github.io that referenced this issue Apr 28, 2020
@stale
Copy link

stale bot commented May 7, 2020

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

@stale stale bot added the Stale label May 7, 2020
@apexskier
Copy link
Contributor

(keeping this from stale-ing)

This still would be very useful for me. It's a common feature on blogs, it lets me cross-link within my article, and it allows me to share more helpful links to other people.

@stale stale bot removed the Stale label May 7, 2020
apexskier added a commit to apexskier/hugo that referenced this issue May 8, 2020
Adds a custom heading renderer like the current custom link and image
renderers.

TODO

- [ ] run locally and add tests
- [ ] cleanup - should this be called "render_hooks" instead of "render_link"?
- [ ] [now that there are three use cases, should I try to deduplicate](https://camlittle.com/posts/when-to-add-abstractions/)
?

Resolves gohugoio#6713
@jemus42
Copy link

jemus42 commented May 14, 2020

I am just about to fiddle this into the theme I'm using – before I do, I'd like to confirm: Does it look like this will make it into an upcoming release?

@bep
Copy link
Member

bep commented May 15, 2020

I am just about to fiddle this into the theme I'm using – before I do, I'd like to confirm: Does it look like this will make it into an upcoming release?

I have initially been a little hesitant about this feature; not the things It tries to solve (I recently found one more thing that this feature would help with), but I have been hoping to get to a state this could be solved in a ... more elegant way (I have talked about this elsewhere).

Image and link hooks was kind of obvious -- we really needed those right away, and they have proved it's usefulness.

But since that "elegant way" seem to take some time, I'll say that now is the time to also add render hooks for titles.

So yes, just let me know when this is ready and I'll have a look and eventually merge it.

@apexskier
Copy link
Contributor

apexskier commented May 15, 2020

I think this is close to ready:

@bep bep added Enhancement and removed Proposal labels May 15, 2020
@bep bep closed this as completed in #7133 May 15, 2020
bep pushed a commit that referenced this issue May 15, 2020
This commit also

* Renames previous types to be non-specific. (e.g. hookedRenderer rather
  than linkRenderer)

Resolves #6713
muenchhausen pushed a commit to muenchhausen/hugo that referenced this issue Jun 24, 2020
This commit also

* Renames previous types to be non-specific. (e.g. hookedRenderer rather
  than linkRenderer)

Resolves gohugoio#6713
@github-actions
Copy link

github-actions bot commented Feb 3, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 3, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants