Skip to content

Commit

Permalink
Fix support for <hr> tags getting butchered (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
allejo authored Feb 16, 2019
2 parents 4d414be + 0511557 commit 1d09aac
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
17 changes: 12 additions & 5 deletions _includes/anchor_headings.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% capture headingsWorkspace %}
{% comment %}
Version 1.0.2
Version 1.0.3
https://github.com/allejo/jekyll-anchor-headings

"Be the pull request you wish to see in the world." ~Ben Balter
Expand Down Expand Up @@ -32,15 +32,22 @@

{% capture edited_headings %}{% endcapture %}

{% for node in nodes %}
{% for _node in nodes %}
{% capture node %}{{ _node | strip }}{% endcapture %}

{% if node == "" %}
{% continue %}
{% endif %}

{% assign headerLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %}
{% assign nextChar = node | replace: '"', '' | strip | slice: 0, 1 %}
{% assign headerLevel = nextChar | times: 1 %}

<!-- If the level is cast to 0, it means it's not a h1-h6 tag, so let's try to fix it -->
{% if headerLevel == 0 %}
{% if nextChar != '<' and nextChar != '' %}
{% capture node %}<h{{ node }}{% endcapture %}
{% endif %}

<!-- If the node doesn't have a header, then it's content before the first heading; don't discard it -->
{% if headerLevel < 1 or headerLevel > 6 %}
{% capture edited_headings %}{{ edited_headings }}{{ node }}{% endcapture %}
{% continue %}
{% endif %}
Expand Down
30 changes: 30 additions & 0 deletions _tests/horizontalRule.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
# See https://github.com/allejo/jekyll-anchor-headings/issues/9
---

{% capture markdown %}
Starting content

# Heading 1

I can show you the world

---

...or maybe not.
{% endcapture %}
{% assign text = markdown | markdownify %}

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

<!-- /// -->

<div>
<p>Starting content</p>
<h1 id="heading-1">Heading 1 <a href="#heading-1"></a></h1>
<p>I can show you the world</p>
<hr />
<p>…or maybe not.</p>
</div>
34 changes: 34 additions & 0 deletions _tests/tagsStartingWithH.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
# See https://github.com/allejo/jekyll-anchor-headings/issues/9
---

{% capture markdown %}
Some introduction paragraph

<header>
some header
</header>

# Hello World

<head>
this shouldn't ever been in HTML, but it shouldn't break either
</head>

<hgroup>I can see the future</hgroup>
{% endcapture %}
{% assign text = markdown | markdownify %}

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

<!-- /// -->

<div>
<p>Some introduction paragraph</p>
<header>some header</header>
<h1 id="hello-world">Hello World <a href="#hello-world"></a></h1>
<head>this shouldn't ever been in HTML, but it shouldn't break either</head>
<hgroup>I can see the future</hgroup>
</div>

0 comments on commit 1d09aac

Please sign in to comment.