Skip to content

Commit

Permalink
Add support to generate ids for headers that do not have them
Browse files Browse the repository at this point in the history
As commonmark does not generate ids for its headers see,

github/cmark-gfm#186

we need the possibility to generate them when creating anchor headings.
  • Loading branch information
garazdawi committed Sep 10, 2021
1 parent 73e8160 commit a4b9c3d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ This snippet is highly customizable. Here are the available parameters to change
| `h_max` | int | 6 | The maximum header level to build an anchor for; any header greater than this value will be ignored |
| `bodyPrefix` | string | '' | Anything that should be inserted inside of the heading tag _before_ its anchor and content |
| `bodySuffix` | string | '' | Anything that should be inserted inside of the heading tag _after_ its anchor and content |
| `generateId` | bool | false | Set to true if a header without id should generate an id to use.

<sup>*</sup> This is a required parameter

Expand Down
23 changes: 17 additions & 6 deletions _includes/anchor_headings.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* h_max (int) : 6 - The maximum header level to build an anchor for; any header greater than this value will be ignored
* bodyPrefix (string) : '' - Anything that should be inserted inside of the heading tag _before_ its anchor and content
* bodySuffix (string) : '' - Anything that should be inserted inside of the heading tag _after_ its anchor and content
* generateId (true) : false - Set to true if a header without id should generate an id to use.

Output:
The original HTML with the addition of anchors inside of all of the h1-h6 headings.
Expand Down Expand Up @@ -87,20 +88,30 @@
{% capture _closingTag %}</h{{ headerLevel }}>{% endcapture %}
{% assign _workspace = node | split: _closingTag %}
{% assign _idWorkspace = _workspace[0] | split: 'id="' %}
{% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
{% assign html_id = _idWorkspace[0] %}

{% assign headerAttrs = include.headerAttrs %}
{% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %}
{% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
{% assign escaped_header = header | strip_html | strip %}

{% if _idWorkspace[1] %}
{% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
{% assign html_id = _idWorkspace[0] %}
{% elsif include.generateId %}
<!-- If the header did not have an id we create one. -->
{% assign html_id = escaped_header | slugify %}
{% if html_id == "" %}
{% assign html_id = false %}
{% endif %}
{% capture headerAttrs %}{{headerAttrs}} id="%html_id%"{% endcapture %}
{% endif %}

<!-- Build the anchor to inject for our heading -->
{% capture anchor %}{% endcapture %}

{% if html_id and headerLevel >= minHeader and headerLevel <= maxHeader %}
{% assign escaped_header = header | strip_html %}

{% if include.headerAttrs %}
{% capture _hAttrToStrip %}{{ _hAttrToStrip | split: '>' | first }} {{ include.headerAttrs | replace: '%heading%', escaped_header | replace: '%html_id%', html_id }}>{% endcapture %}
{% if headerAttrs %}
{% capture _hAttrToStrip %}{{ _hAttrToStrip | split: '>' | first }} {{ headerAttrs | replace: '%heading%', escaped_header | replace: '%html_id%', html_id }}>{% endcapture %}
{% endif %}

{% capture anchor %}href="#{{ html_id }}"{% endcapture %}
Expand Down
13 changes: 13 additions & 0 deletions _tests/anchorsWithGeneratedIDs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
# See: https://github.com/allejo/jekyll-anchor-headings/issues/1
---

{% capture text %}
<h2>Hello world</h2>
{% endcapture %}

{% include anchor_headings.html html=text generateId=true %}

<!-- /// -->

<h2 id="hello-world">Hello world<a href="#heading-1"></a></h2>

0 comments on commit a4b9c3d

Please sign in to comment.