Skip to content

Commit 29de49d

Browse files
N0K0allejo
authored andcommitted
Add support for alternative URL in TOC (#10)
1 parent 50cbec8 commit 29de49d

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ This snippet is highly customizable. Here are the available parameters to change
4444
| `h_max` | int | 6 | the maximum TOC header level to use; any heading greater than this value will be ignored |
4545
| `ordered` | bool | false | when set to true, an ordered list will be outputted instead of an unordered list |
4646
| `item_class` | string | '' | add custom class for each list item; has support for `%level%` placeholder, which is the current heading level |
47+
| `base_url` | string | '' | add an base url to the TOC links for when your TOC is on an other page than the actual content |
4748

4849
<sup>*</sup> This is a required parameter
4950

_includes/toc.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* h_max (int) : 6 - the maximum TOC header level to use; any header greater than this value will be ignored
2020
* ordered (bool) : false - when set to true, an ordered list will be outputted instead of an unordered list
2121
* item_class (string) : '' - add custom class for each list item; has support for '%level%' placeholder, which is the current heading level
22+
* base_url (string) : '' - add an base url to the TOC links for when your TOC is on an other page than the actual content
2223

2324
Output:
2425
An ordered or unordered list representing the table of contents of a markdown block. This snippet will only generate the table of contents and will NOT output the markdown given to it
@@ -69,8 +70,7 @@
6970
{% endunless %}
7071

7172
{% capture my_toc %}{{ my_toc }}
72-
{{ space }}{{ listModifier }} {{ listItemClass }} [{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}](#{{ html_id }}){% endcapture %}
73-
73+
{{ space }}{{ listModifier }} {{ listItemClass }} [{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}]({% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ html_id }}){% endcapture %}
7474
{% endfor %}
7575

7676
{% if include.class %}

_tests/base_url.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
---
3+
4+
{% capture markdown %}
5+
# Heading 1
6+
{% endcapture %}
7+
{% assign text = markdown | markdownify %}
8+
9+
{% include toc.html html=text baseurl="example.org" %}
10+
11+
<!-- /// -->
12+
13+
<ul>
14+
<li><a href="example.org#heading-1">Heading 1</a></li>
15+
</ul>

0 commit comments

Comments
 (0)