Skip to content

Commit

Permalink
Merge pull request #28 from allejo/hotfix/escape-heading-placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
allejo authored Dec 19, 2020
2 parents 376bee9 + 3a15990 commit c7e11e3
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 13 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ jobs:
with:
ruby-version: '2.5'

- uses: actions/setup-python@v2
with:
python-version: '3.8'

- name: Install bundler
run: gem install bundler

Expand All @@ -51,3 +55,19 @@ jobs:
bundle exec jekyll --version
bundle exec jekyll build
python tests.py
on_complete:
name: Notify IRC on Completion
runs-on: ubuntu-latest
needs: tests
if: always()

steps:
- name: Send IRC notification
uses: allejo/supybot-notification-action@v1
with:
status: ${{ needs.tests.result }}
hostname: ${{ secrets.SUPYBOT_HOSTNAME }}
credentials: ${{ secrets.SUPYBOT_PASSWORD }}
channel: '#sujevo-dev'
default_message: true
12 changes: 7 additions & 5 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.8
Version 1.0.9
https://github.com/allejo/jekyll-anchor-headings

"Be the pull request you wish to see in the world." ~Ben Balter
Expand Down Expand Up @@ -97,8 +97,10 @@
{% 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%', header | replace: '%html_id%', html_id }}>{% endcapture %}
{% capture _hAttrToStrip %}{{ _hAttrToStrip | split: '>' | first }} {{ include.headerAttrs | replace: '%heading%', escaped_header | replace: '%html_id%', html_id }}>{% endcapture %}
{% endif %}

{% capture anchor %}href="#{{ html_id }}"{% endcapture %}
Expand All @@ -108,14 +110,14 @@
{% endif %}

{% if include.anchorTitle %}
{% capture anchor %}{{ anchor }} title="{{ include.anchorTitle | replace: '%heading%', header }}"{% endcapture %}
{% capture anchor %}{{ anchor }} title="{{ include.anchorTitle | replace: '%heading%', escaped_header }}"{% endcapture %}
{% endif %}

{% if include.anchorAttrs %}
{% capture anchor %}{{ anchor }} {{ include.anchorAttrs | replace: '%heading%', header | replace: '%html_id%', html_id }}{% endcapture %}
{% capture anchor %}{{ anchor }} {{ include.anchorAttrs | replace: '%heading%', escaped_header | replace: '%html_id%', html_id }}{% endcapture %}
{% endif %}

{% capture anchor %}<a {{ anchor }}>{{ include.anchorBody | replace: '%heading%', header | default: '' }}</a>{% endcapture %}
{% capture anchor %}<a {{ anchor }}>{{ include.anchorBody | replace: '%heading%', escaped_header | default: '' }}</a>{% endcapture %}

<!-- In order to prevent adding extra space after a heading, we'll let the 'anchor' value contain it -->
{% if beforeHeading %}
Expand Down
25 changes: 25 additions & 0 deletions _tests/escapeHeadingPlaceholder.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
# https://github.com/allejo/jekyll-anchor-headings/issues/27
---

{% capture markdown %}
### The [**match**](#match) Expression
{% endcapture %}
{% assign text = markdown | markdownify %}

{% include anchor_headings.html
beforeHeading=false
h_min=2
html=text
anchorClass="permalink icon-link"
anchorBody=""
anchorTitle="Permalink for '%heading%'"
anchorAttrs='data-heading="%heading%"'
headerAttrs='data-heading="%heading%"'
%}

<!-- /// -->

<h3 id="the-match-expression" data-heading="The match Expression">
The <a href="#match"><strong>match</strong></a> Expression <a href="#the-match-expression" class="permalink icon-link" title="Permalink for 'The match Expression'" data-heading="The match Expression"></a>
</h3>
22 changes: 14 additions & 8 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
import unittest
import xml.etree.ElementTree as ET

class TestSequense(unittest.TestCase):
class TestSequence(unittest.TestCase):
pass

def test_generator(a, b):
def test(self):
self.maxDiff = None
self.assertEqual(a, b)
return test

def normalize_xml(xml):
tree = ET.fromstring(xml)
return re.sub('\\n\s+|\\n', '', ET.tostring(tree))
def normalize_xml(xml, test_file):
try:
tree = ET.fromstring(xml)
except:
print(f">> Invalid XML in {test_file}")
raise

return re.sub(r'(\\n|\n)\s*', '', str(ET.tostring(tree)), 0, re.MULTILINE)

if __name__ == '__main__':
test_path = os.path.join(os.getcwd(), '_site', 'tests')
Expand All @@ -25,12 +31,12 @@ def normalize_xml(xml):
path = os.path.join(test_path, test_file)
with open(path, 'r') as file:
actual, expected = file.read().split('<!-- /// -->')
actual = normalize_xml(actual)
expected = normalize_xml(expected)
actual = normalize_xml(actual, test_file)
expected = normalize_xml(expected, test_file)

# Add the unit test to our TestSequense
# Add the unit test to our TestSequence
test_name = 'test_{}'.format(test_file)
test = test_generator(actual, expected)
setattr(TestSequense, test_name, test)
setattr(TestSequence, test_name, test)

unittest.main()

0 comments on commit c7e11e3

Please sign in to comment.