public
Created

Hierarchical tree of pages in Jekyll

  • Download Gist
gistfile1.rb
Ruby
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
require 'digest/md5'
 
module Jekyll
# Add accessor for directory
class Page
attr_reader :dir
end
class NavTree < Liquid::Tag
def render(context)
site = context.registers[:site]
@page_url = context.environments.first["page"]["url"]
@dirs = {}
tree = {}
 
site.pages.each do |page|
# exclude all pages that are hidden in front-matter
if page.data["navigation"]["show"] != false
relative = page.dir[1..-1] ||""
path = relative + page.url
path = path.index('/')==0 ? path[1..-1] : path
@dirs[path] = page.data
end
end
@dirs.each do |path, data|
current = tree
path.split("/").inject("") do |sub_path,dir|
sub_path = File.join(sub_path, dir)
current[sub_path] ||= {}
current = current[sub_path]
sub_path
end
end
 
puts "generating nav tree for #{@page_url}"
files_first_traverse "", tree
end
def files_first_traverse(prefix, node = {})
output = ""
output += "#{prefix}<ul id=\"nav-menu\" class=\"nav nav-list\">"
node_list = node.sort
node_list.each do |base, subtree|
name = base[1..-1]
if name.index('.') != nil
name = @dirs[name]["title"] || name
end
li_class = ""
if base == @page_url
li_class = "active"
end
output += "#{prefix} <li class=#{li_class}><a href=\"#{base}\">#{name}</a></li>" if subtree.empty?
end
node_list.each do |base, subtree|
next if subtree.empty?
show_link = true
href = base
name = base[1..-1]
if name.index('.') != nil
is_parent = false
name = @dirs[name]["title"] || name
else
is_parent = true
href = base + '/index.html'
if name.index('/')
name = name[name.rindex('/')+1..-1]
end
end
li_class = ""
if @page_url.index(base)
list_class = "collapsibleListClosed"
end
if href == @page_url
li_class = "active"
end
if is_parent
id = Digest::MD5.hexdigest(base);
 
li = "<li id=\"node-#{id}\" class=\"parent #{list_class}\"><div class=\"subtree-name\">#{name}</div>"
else
li = "<li class=\"#{li_class}\"><a href=\"#{href}\">#{name}</a></li>"
end
output += "#{prefix} #{li}"
output += files_first_traverse(prefix + ' ', subtree)
 
if is_parent
output+= "</li>"
end
end
output += "#{prefix} </ul>"
output
end
end
end
 
Liquid::Template.register_tag("navigation", Jekyll::NavTree)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.