Skip to content

Commit 3568089

Browse files
authored
Generate header ids and anchors without Javascript
1 parent 46af259 commit 3568089

16 files changed

Lines changed: 170 additions & 32 deletions

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
`img/link-icon.svg` is provided by Emojitwo, originally released as Emojione 2.2 by Ranks.com with contributions from the Emojitwo community and is licensed under CC-BY 4.0.

_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ url: https://ircv3.net
55
repository: ircv3/ircv3.github.io
66
kramdown:
77
input: GFM
8-
auto_ids: false
8+
auto_ids: true
99
include:
1010
- _redirects
1111
exclude:

_includes/anchor_headings.html

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
{% capture headingsWorkspace %}
2+
{% comment %}
3+
Copyright (c) 2018 Vladimir "allejo" Jimenez
4+
5+
Permission is hereby granted, free of charge, to any person
6+
obtaining a copy of this software and associated documentation
7+
files (the "Software"), to deal in the Software without
8+
restriction, including without limitation the rights to use,
9+
copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the
11+
Software is furnished to do so, subject to the following
12+
conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24+
OTHER DEALINGS IN THE SOFTWARE.
25+
{% endcomment %}
26+
{% comment %}
27+
Version 1.0.9
28+
https://github.com/allejo/jekyll-anchor-headings
29+
30+
"Be the pull request you wish to see in the world." ~Ben Balter
31+
32+
Usage:
33+
{% include anchor_headings.html html=content anchorBody="#" %}
34+
35+
Parameters:
36+
* html (string) - the HTML of compiled markdown generated by kramdown in Jekyll
37+
38+
Optional Parameters:
39+
* beforeHeading (bool) : false - Set to true if the anchor should be placed _before_ the heading's content
40+
* headerAttrs (string) : '' - Any custom HTML attributes that will be added to the heading tag; you may NOT use `id`;
41+
the `%heading%` and `%html_id%` placeholders are available
42+
* anchorAttrs (string) : '' - Any custom HTML attributes that will be added to the `<a>` tag; you may NOT use `href`, `class` or `title`;
43+
the `%heading%` and `%html_id%` placeholders are available
44+
* anchorBody (string) : '' - The content that will be placed inside the anchor; the `%heading%` placeholder is available
45+
* anchorClass (string) : '' - The class(es) that will be used for each anchor. Separate multiple classes with a space
46+
* anchorTitle (string) : '' - The `title` attribute that will be used for anchors
47+
* h_min (int) : 1 - The minimum header level to build an anchor for; any header lower than this value will be ignored
48+
* h_max (int) : 6 - The maximum header level to build an anchor for; any header greater than this value will be ignored
49+
* bodyPrefix (string) : '' - Anything that should be inserted inside of the heading tag _before_ its anchor and content
50+
* bodySuffix (string) : '' - Anything that should be inserted inside of the heading tag _after_ its anchor and content
51+
52+
Output:
53+
The original HTML with the addition of anchors inside of all of the h1-h6 headings.
54+
{% endcomment %}
55+
56+
{% assign minHeader = include.h_min | default: 1 %}
57+
{% assign maxHeader = include.h_max | default: 6 %}
58+
{% assign beforeHeading = include.beforeHeading %}
59+
{% assign nodes = include.html | split: '<h' %}
60+
61+
{% capture edited_headings %}{% endcapture %}
62+
63+
{% for _node in nodes %}
64+
{% capture node %}{{ _node | strip }}{% endcapture %}
65+
66+
{% if node == "" %}
67+
{% continue %}
68+
{% endif %}
69+
70+
{% assign nextChar = node | replace: '"', '' | strip | slice: 0, 1 %}
71+
{% assign headerLevel = nextChar | times: 1 %}
72+
73+
<!-- If the level is cast to 0, it means it's not a h1-h6 tag, so let's see if we need to fix it -->
74+
{% if headerLevel == 0 %}
75+
<!-- Split up the node based on closing angle brackets and get the first one. -->
76+
{% assign firstChunk = node | split: '>' | first %}
77+
78+
<!-- If the first chunk does NOT contain a '<', that means we've broken another HTML tag that starts with 'h' -->
79+
{% unless firstChunk contains '<' %}
80+
{% capture node %}<h{{ node }}{% endcapture %}
81+
{% endunless %}
82+
83+
{% capture edited_headings %}{{ edited_headings }}{{ node }}{% endcapture %}
84+
{% continue %}
85+
{% endif %}
86+
87+
{% capture _closingTag %}</h{{ headerLevel }}>{% endcapture %}
88+
{% assign _workspace = node | split: _closingTag %}
89+
{% assign _idWorkspace = _workspace[0] | split: 'id="' %}
90+
{% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
91+
{% assign html_id = _idWorkspace[0] %}
92+
93+
{% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %}
94+
{% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
95+
96+
<!-- Build the anchor to inject for our heading -->
97+
{% capture anchor %}{% endcapture %}
98+
99+
{% if html_id and headerLevel >= minHeader and headerLevel <= maxHeader %}
100+
{% assign escaped_header = header | strip_html %}
101+
102+
{% if include.headerAttrs %}
103+
{% capture _hAttrToStrip %}{{ _hAttrToStrip | split: '>' | first }} {{ include.headerAttrs | replace: '%heading%', escaped_header | replace: '%html_id%', html_id }}>{% endcapture %}
104+
{% endif %}
105+
106+
{% capture anchor %}href="#{{ html_id }}"{% endcapture %}
107+
108+
{% if include.anchorClass %}
109+
{% capture anchor %}{{ anchor }} class="{{ include.anchorClass }}"{% endcapture %}
110+
{% endif %}
111+
112+
{% if include.anchorTitle %}
113+
{% capture anchor %}{{ anchor }} title="{{ include.anchorTitle | replace: '%heading%', escaped_header }}"{% endcapture %}
114+
{% endif %}
115+
116+
{% if include.anchorAttrs %}
117+
{% capture anchor %}{{ anchor }} {{ include.anchorAttrs | replace: '%heading%', escaped_header | replace: '%html_id%', html_id }}{% endcapture %}
118+
{% endif %}
119+
120+
{% capture anchor %}<a {{ anchor }}>{{ include.anchorBody | replace: '%heading%', escaped_header | default: '' }}</a>{% endcapture %}
121+
122+
<!-- In order to prevent adding extra space after a heading, we'll let the 'anchor' value contain it -->
123+
{% if beforeHeading %}
124+
{% capture anchor %}{{ anchor }} {% endcapture %}
125+
{% else %}
126+
{% capture anchor %} {{ anchor }}{% endcapture %}
127+
{% endif %}
128+
{% endif %}
129+
130+
{% capture new_heading %}
131+
<h{{ _hAttrToStrip }}
132+
{{ include.bodyPrefix }}
133+
{% if beforeHeading %}
134+
{{ anchor }}{{ header }}
135+
{% else %}
136+
{{ header }}{{ anchor }}
137+
{% endif %}
138+
{{ include.bodySuffix }}
139+
</h{{ headerLevel }}>
140+
{% endcapture %}
141+
142+
<!--
143+
If we have content after the `</hX>` tag, then we'll want to append that here so we don't lost any content.
144+
-->
145+
{% assign chunkCount = _workspace | size %}
146+
{% if chunkCount > 1 %}
147+
{% capture new_heading %}{{ new_heading }}{{ _workspace | last }}{% endcapture %}
148+
{% endif %}
149+
150+
{% capture edited_headings %}{{ edited_headings }}{{ new_heading }}{% endcapture %}
151+
{% endfor %}
152+
{% endcapture %}{% assign headingsWorkspace = '' %}{{ edited_headings | strip }}

_includes/anchors.html

Lines changed: 0 additions & 8 deletions
This file was deleted.

_includes/software_list.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
### {{ type.name }} <a id="{{ type.name | slugify }}" ></a>
1+
### {{ type.name }}
22

33
{% if type.note %}{{ type.note | markdownify }}{% endif %}
44

_irc/index.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,3 @@ The [`tls` spec]({{site.baseurl}}/specs/extensions/tls-3.1.html) is still
396396
available for reference. It describes how the `STARTTLS` command works,
397397
as well as how connection registration is changed by the introduction of
398398
this capability.
399-
400-
401-
{% include anchors.html %}

_layouts/default.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
{% capture mdph %}{{ page.page-header | markdownify }}{% endcapture %}
4141
{% include pageheader.html content=mdph %}
4242
{% endif %}
43-
{{ content }}
43+
{% include anchor_headings.html html=content anchorBody="🔗" anchorClass="anchor" %}
4444
</div>
4545
</div>
4646
<footer>

_layouts/page.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@
22
layout: default
33
---
44
{{ content }}
5-
6-
{% include anchors.html %}

_layouts/spec.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,3 @@ <h1>{{ page.title | markdownify | replace:'<p>','' | replace:'</p>','' }}</h1>
147147
{% endif %}
148148
{% endif %}
149149
{% endfor %}
150-
151-
{% include anchors.html %}

charter.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,3 @@ The main project resources are:
7272
Contributions are welcome from anyone in the IRC community; including users, developers, operators, administrators. Feel free to start a discussion on IRC or on the issue tracker if you'd like to contribute.
7373

7474
Failure to follow our [code of conduct]({{site.baseurl}}/conduct.html) when participating may result in immediate removal from the project resources.
75-
76-
{% include anchors.html %}

0 commit comments

Comments
 (0)