maxHeader %}
@@ -79,9 +87,17 @@
{% capture listItemClass %}{:.{{ include.item_class | replace: '%level%', headerLevel }}}{% endcapture %}
{% endif %}
- {% capture heading_body %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %}
+ {% capture anchor_body %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %}
+ {% capture anchor_body %}{{ anchor_body | replace: "|", "\|" }}{% endcapture %}
+
+ {% if html_id %}
+ {% capture list_item %}[{{ anchor_body }}]({% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ html_id }}){% endcapture %}
+ {% else %}
+ {% capture list_item %}{{ anchor_body }}{% endcapture %}
+ {% endif %}
+
{% capture my_toc %}{{ my_toc }}
-{{ space }}{{ listModifier }} {{ listItemClass }} [{{ heading_body | replace: "|", "\|" }}]({% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ html_id }}){% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %}
+{{ space }}{{ listModifier }} {{ listItemClass }} {{ list_item }}{% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %}
{% endfor %}
{% if include.class and include.class != blank %}
diff --git a/_tests/noAnchorOnHeadingsWithNoIDs.md b/_tests/noAnchorOnHeadingsWithNoIDs.md
new file mode 100644
index 0000000..913f97f
--- /dev/null
+++ b/_tests/noAnchorOnHeadingsWithNoIDs.md
@@ -0,0 +1,44 @@
+---
+# https://github.com/allejo/jekyll-toc/issues/32
+---
+
+{% capture markdown %}
+## Heading 2.1
+
+Heading 2.2 (no link)
+
+### Heading 2.2.1
+
+Heading 2.3
+
+## Heading 2.4
+
+Heading 2.4.1
+{% endcapture %}
+{% assign text = markdown | markdownify %}
+
+{% include toc.html html=text %}
+
+
+
+
diff --git a/_tests/skipHeadingsWithNoIDs.md b/_tests/skipHeadingsWithNoIDs.md
new file mode 100644
index 0000000..7084c80
--- /dev/null
+++ b/_tests/skipHeadingsWithNoIDs.md
@@ -0,0 +1,49 @@
+---
+# https://github.com/allejo/jekyll-toc/issues/32
+---
+
+{% capture markdown %}
+## Sample Usage
+
+
+
My Awesome Example Page
+ With an awesome subtitle
+
+
+### Known Problems
+
+Lots!
+
+### Resources
+
+#### Paid
+
+#### Free
+{% endcapture %}
+{% assign text = markdown | markdownify %}
+
+{% include toc.html html=text skipNoIDs=true %}
+
+
+
+