Skip to content

Commit a112eb5

Browse files
authored
Merge pull request allejo#22 from allejo/hotfix/nested-headers-break-html
Fix losing content when headings are in nested elements
2 parents 9e92644 + f07b2b3 commit a112eb5

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

_includes/anchor_headings.html

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
OTHER DEALINGS IN THE SOFTWARE.
2525
{% endcomment %}
2626
{% comment %}
27-
Version 1.0.6
27+
Version 1.0.7
2828
https://github.com/allejo/jekyll-anchor-headings
2929

3030
"Be the pull request you wish to see in the world." ~Ben Balter
@@ -82,7 +82,8 @@
8282
{% continue %}
8383
{% endif %}
8484

85-
{% assign _workspace = node | split: '</h' %}
85+
{% capture _closingTag %}</h{{ headerLevel }}>{% endcapture %}
86+
{% assign _workspace = node | split: _closingTag %}
8687
{% assign _idWorkspace = _workspace[0] | split: 'id="' %}
8788
{% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
8889
{% assign html_id = _idWorkspace[0] %}
@@ -127,8 +128,17 @@
127128
{{ header }}{{ anchor }}
128129
{% endif %}
129130
{{ include.bodySuffix }}
130-
</h{{ _workspace | last }}
131+
</h{{ headerLevel }}>
131132
{% endcapture %}
133+
134+
<!--
135+
If we have content after the `</hX>` tag, then we'll want to append that here so we don't lost any content.
136+
-->
137+
{% assign chunkCount = _workspace | size %}
138+
{% if chunkCount > 1 %}
139+
{% capture new_heading %}{{ new_heading }}{{ _workspace | last }}{% endcapture %}
140+
{% endif %}
141+
132142
{% capture edited_headings %}{{ edited_headings }}{{ new_heading }}{% endcapture %}
133143
{% endfor %}
134144
{% endcapture %}{% assign headingsWorkspace = '' %}{{ edited_headings | strip }}

_tests/nestedHeadingsInHeader.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
# https://github.com/allejo/jekyll-anchor-headings/issues/21
3+
---
4+
5+
{% capture markdown %}
6+
<header>
7+
<h1 id="goodbye-world">Goodbye World</h1>
8+
<p>some header text</p>
9+
</header>
10+
{% endcapture %}
11+
{% assign text = markdown | markdownify %}
12+
13+
<div>
14+
{% include anchor_headings.html html=text %}
15+
</div>
16+
17+
<!-- /// -->
18+
19+
<div>
20+
<header>
21+
<h1 id="goodbye-world">Goodbye World <a href="#goodbye-world"></a></h1>
22+
<p>some header text</p>
23+
</header>
24+
</div>

0 commit comments

Comments
 (0)