feat: add linkable heading anchors to PSF pages#2966
feat: add linkable heading anchors to PSF pages#2966nagasrisai wants to merge 5 commits intopython:mainfrom
Conversation
Adds a custom template filter that post-processes rendered page HTML to inject id attributes and pilcrow self-link anchors into h2-h4 headings. Duplicate slugs get a -N suffix to prevent id collisions. Headings that already carry an id are left untouched. Part of python#2349
Loads the new page_tags library and pipes page content through the add_heading_anchors filter so that every h2-h4 in a PSF page (including the board resolutions listing) gets a stable id attribute and a pilcrow anchor link for direct linking. Part of python#2349
10 test cases covering: h2/h3/h4 processing, h1/h5 exclusion, duplicate-slug deduplication, existing-id passthrough, nested HTML stripping, non-heading passthrough, empty string, empty text, and anchor placement inside the heading element.
|
@JacobCoffee @ewdurbin @sethmlarson — could one of you review when you get a chance? This adds anchor IDs to headings on PSF pages so sections like the board resolutions can be linked to directly. 10 tests included. Thanks! |
|
The Actions workflows need maintainer approval to run since this is coming from a fork. Could @JacobCoffee or @ewdurbin approve the CI, Lint, and Check collectstatic runs from the Checks tab when you get a chance? Thanks! |
|
@nagasrisai hi please dont @ specific people, CODEOWNERS will handle assignment for review |
|
|
||
| register = template.Library() | ||
|
|
||
| # Match h2–h4 elements; capture tag name, existing attributes, and inner HTML. |
There was a problem hiding this comment.
@hugovk h1 is the page title ,there's only ever one per page and the page URL already points to it, so an anchor on it wouldn't be useful. The sections people actually want to link into are h2 and below (e.g. individual board resolutions or meeting minutes). This is also the convention most docs sites follow, including GitHub's own markdown renderer.
Adds linkable anchor IDs to headings on PSF pages so that individual sections can be shared as direct URLs, which is what #2349 is asking for.
Added a
add_heading_anchorstemplate filter inapps/pages/templatetags/page_tags.py. It processes the rendered page HTML and injects anidattribute into each h2, h3, and h4 element based on the slugified heading text, along with a small pilcrow (¶) anchor link. Applied the filter intemplates/psf/default.htmlso it covers the board resolutions page and all other PSF pages automatically.Duplicate heading texts get a
-2,-3suffix to keep IDs unique, and headings that already carry anidare left untouched. Ten tests included inapps/pages/tests/test_templatetags.py.Closes #2349