forked from sendgrid/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanchors.rb
More file actions
38 lines (30 loc) · 1.13 KB
/
Copy pathanchors.rb
File metadata and controls
38 lines (30 loc) · 1.13 KB
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
#Generates a named anchor and wrapping tag from a string.
module Jekyll
class AnchorBlock < Liquid::Block
def initialize(tag_name, markup, tokens)
attributes = markup.split
@tag = attributes[0]
@name = attributes[1] || false
super
end
def render(context)
contents = super
# pipe param through liquid to make additional replacements possible
content = Liquid::Template.parse(contents).render context
# if the anchor has a defined name, use that, otherwise make one up
if @name
safeContent = @name
else
#strip out special chars and replace spaces with hyphens
safeContent = content.rstrip.gsub(/[^\w\s]/,'').gsub(/[\s]/,'-')
end
#should refactor this to allow wrapping tag to be passed in
output = "<#{@tag} class=\"anchor-wrap\"><a name=\"#{safeContent}\" class=\"anchor\" href=\"##{safeContent}\">"
output += '<span class="anchor-icon"><i class="icon-link"></i></span></a>'
output += content.strip
output += "</#{@tag}>"
output
end
end
end
Liquid::Template.register_tag("anchor", Jekyll::AnchorBlock)