Skip to content

Commit

Permalink
Merge pull request #22 from allejo/hotfix/nested-headers-break-html
Browse files Browse the repository at this point in the history
Fix losing content when headings are in nested elements
  • Loading branch information
allejo authored Aug 2, 2020
2 parents 9e92644 + f07b2b3 commit a112eb5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
16 changes: 13 additions & 3 deletions _includes/anchor_headings.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
OTHER DEALINGS IN THE SOFTWARE.
{% endcomment %}
{% comment %}
Version 1.0.6
Version 1.0.7
https://github.com/allejo/jekyll-anchor-headings

"Be the pull request you wish to see in the world." ~Ben Balter
Expand Down Expand Up @@ -82,7 +82,8 @@
{% continue %}
{% endif %}

{% assign _workspace = node | split: '</h' %}
{% 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] %}
Expand Down Expand Up @@ -127,8 +128,17 @@
{{ header }}{{ anchor }}
{% endif %}
{{ include.bodySuffix }}
</h{{ _workspace | last }}
</h{{ headerLevel }}>
{% endcapture %}

<!--
If we have content after the `</hX>` tag, then we'll want to append that here so we don't lost any content.
-->
{% assign chunkCount = _workspace | size %}
{% if chunkCount > 1 %}
{% capture new_heading %}{{ new_heading }}{{ _workspace | last }}{% endcapture %}
{% endif %}

{% capture edited_headings %}{{ edited_headings }}{{ new_heading }}{% endcapture %}
{% endfor %}
{% endcapture %}{% assign headingsWorkspace = '' %}{{ edited_headings | strip }}
24 changes: 24 additions & 0 deletions _tests/nestedHeadingsInHeader.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
# https://github.com/allejo/jekyll-anchor-headings/issues/21
---

{% capture markdown %}
<header>
<h1 id="goodbye-world">Goodbye World</h1>
<p>some header text</p>
</header>
{% endcapture %}
{% assign text = markdown | markdownify %}

<div>
{% include anchor_headings.html html=text %}
</div>

<!-- /// -->

<div>
<header>
<h1 id="goodbye-world">Goodbye World <a href="#goodbye-world"></a></h1>
<p>some header text</p>
</header>
</div>

0 comments on commit a112eb5

Please sign in to comment.