1
+ {% capture headingsWorkspace %}
2
+ {% comment %}
3
+ Version 1.0.3
4
+ https://github.com/allejo/jekyll-anchor-headings
5
+
6
+ "Be the pull request you wish to see in the world." ~Ben Balter
7
+
8
+ Usage:
9
+ {% include anchor_headings.html html=content %}
10
+
11
+ Parameters:
12
+ * html (string) - the HTML of compiled markdown generated by kramdown in Jekyll
13
+
14
+ Optional Parameters:
15
+ * beforeHeading (bool) : false - Set to true if the anchor should be placed _before_ the heading's content
16
+ * anchorBody (string) : '' - The content that will be placed inside the anchor; the `%heading%` placeholder is available
17
+ * anchorClass (string) : '' - The class(es) that will be used for each anchor. Separate multiple classes with a space
18
+ * anchorTitle (string) : '' - The `title` attribute that will be used for anchors
19
+ * h_min (int) : 1 - The minimum header level to build an anchor for; any header lower than this value will be ignored
20
+ * h_max (int) : 6 - The maximum header level to build an anchor for; any header greater than this value will be ignored
21
+ * bodyPrefix (string) : '' - Anything that should be inserted inside of the heading tag _before_ its anchor and content
22
+ * bodySuffix (string) : '' - Anything that should be inserted inside of the heading tag _after_ its anchor and content
23
+
24
+ Output:
25
+ The original HTML with the addition of anchors inside of all of the h1-h6 headings.
26
+ {% endcomment %}
27
+
28
+ {% assign minHeader = include.h_min | default: 1 %}
29
+ {% assign maxHeader = include.h_max | default: 6 %}
30
+ {% assign beforeHeading = include.beforeHeading %}
31
+ {% assign nodes = include.html | split: '< h ' %}
32
+
33
+ {% capture edited_headings %}{% endcapture %}
34
+
35
+ {% for _node in nodes %}
36
+ {% capture node %}{{ _node | strip }}{% endcapture %}
37
+
38
+ {% if node == "" %}
39
+ {% continue %}
40
+ {% endif %}
41
+
42
+ {% assign nextChar = node | replace: '"', '' | strip | slice: 0, 1 %}
43
+ {% assign headerLevel = nextChar | times: 1 %}
44
+
45
+ <!-- If the level is cast to 0, it means it's not a h1-h6 tag, so let's try to fix it -->
46
+ {% if headerLevel == 0 %}
47
+ {% if nextChar != '< ' and nextChar != '' %}
48
+ {% capture node %}< h {{ node }}{% endcapture %}
49
+ {% endif %}
50
+
51
+ {% capture edited_headings %}{{ edited_headings }}{{ node }}{% endcapture %}
52
+ {% continue %}
53
+ {% endif %}
54
+
55
+ {% assign _workspace = node | split: '</ h' %}
56
+ {% assign _idWorkspace = _workspace[0] | split: 'id="' %}
57
+ {% assign _idWorkspace = _idWorkspace[1] | split: ' "' %}
58
+ {% assign html_id = _idWorkspace[0] %}
59
+
60
+ {% capture _hAttrToStrip %}{{ _workspace[0] | split: '> ' | first }}> {% endcapture %}
61
+ {% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
62
+
63
+ <!-- Build the anchor to inject for our heading -->
64
+ {% capture anchor %}{% endcapture %}
65
+
66
+ {% if html_id and headerLevel > = minHeader and headerLevel < = maxHeader %}
67
+ {% capture anchor %}href="#{{ html_id}}"{% endcapture %}
68
+
69
+ {% if include.anchorClass %}
70
+ {% capture anchor %}{{ anchor }} class="{{ include.anchorClass }}"{% endcapture %}
71
+ {% endif %}
72
+
73
+ {% if include.anchorTitle %}
74
+ {% capture anchor %}{{ anchor }} title="{{ include.anchorTitle | replace: '%heading%', header }}"{% endcapture %}
75
+ {% endif %}
76
+
77
+ {% capture anchor %}< a {{ anchor }} > {{ include.anchorBody | replace: '%heading%', header | default: '' }}</ a > {% endcapture %}
78
+
79
+ <!-- In order to prevent adding extra space after a heading, we'll let the 'anchor' value contain it -->
80
+ {% if beforeHeading %}
81
+ {% capture anchor %}{{ anchor }} {% endcapture %}
82
+ {% else %}
83
+ {% capture anchor %} {{ anchor }}{% endcapture %}
84
+ {% endif %}
85
+ {% endif %}
86
+
87
+ {% capture new_heading %}
88
+ < h {{ _hAttrToStrip }}
89
+ {{ include.bodyPrefix }}
90
+ {% if beforeHeading %}
91
+ {{ anchor }}{{ header }}
92
+ {% else %}
93
+ {{ header }}{{ anchor }}
94
+ {% endif %}
95
+ {{ include.bodySuffix }}
96
+ </ h{{ _workspace | last }}
97
+ {% endcapture %}
98
+ {% capture edited_headings %}{{ edited_headings }}{{ new_heading }}{% endcapture %}
99
+ {% endfor %}
100
+ {% endcapture %}{% assign headingsWorkspace = '' %}{{ edited_headings | strip }}
0 commit comments