-
Notifications
You must be signed in to change notification settings - Fork 126
/
auto-anchor.rb
51 lines (42 loc) · 1.4 KB
/
auto-anchor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true
## Automatically add id tags to list items that are formatted like one
## of the following:
# - **<Summary:** Details
# - *<Summary:* Details
# - [Summary][]: Details
# - [Summary](URL): Details
def auto_anchor(content)
content.gsub!(/^ *- .*/) do |string|
## Find shortest match for **bold**, *italics*, or [markdown][links]
title = string.match(/\*\*.*?\*\*|\*.*?\*|\[.*?\][(\[]/).to_s
if title.empty?
## No match, pass item through unchanged
string
else
slug = generate_slug(title)
id_prefix = "- {:#{slug} .anchor-list} <a href=\"#{slug}\" class=\"anchor-list-link\">●</a>"
string.sub!(/-/, id_prefix)
end
end
end
## Run automatically on all documents
Jekyll::Hooks.register :documents, :pre_render do |post|
## Don't process documents if YAML headers say: "auto_id: false" or
## we're formatting for email
unless post.data["auto_id"] == false || ENV['JEKYLL_ENV'] == 'email'
auto_anchor(post.content)
end
end
## Block filter that provides {% auto_anchor %}{% endauto_anchor %} for
## use on {% include %} files
module Jekyll
class RenderAutoAnchor < Liquid::Block
def render(context)
text = super
text = auto_anchor(text)
text = Liquid::Template.parse(text)
text.render(@context)
end
end
end
Liquid::Template.register_tag('auto_anchor', Jekyll::RenderAutoAnchor)