Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ AllCops:
- script/**/*
- test/fixtures/**/*
- vendor/**/*

Metrics/BlockLength:
Exclude:
- "test/**/*"
66 changes: 66 additions & 0 deletions components/bridgetown_stoa/sidebar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

class BridgetownStoa::Sidebar < Bridgetown::Component
Node = Struct.new(:title, :url, :order, :parent_title, :current, :children, keyword_init: true)

def initialize(site:, current_resource:)
super()
@site = site
@current_resource = current_resource
end

def tree
@tree ||= build_tree
end

def section_open?(node)
return true if node.current

node.children.any? { |child| section_open?(child) }
end

private

def navigable_resources
@site.resources.select do |r|
next false if r.data[:nav_exclude]
next false unless r.data[:title]
next false unless r.output_ext == ".html"

true
end
end

def build_tree
nodes = navigable_resources.map { |r| build_node(r) }
by_title = nodes.to_h { |n| [n.title, n] }

roots = nodes.each_with_object([]) do |node, acc|
parent = node.parent_title && by_title[node.parent_title]
if parent && parent != node
parent.children << node
else
acc << node
end
end

sort_nodes!(roots)
roots
end

def build_node(resource)
Node.new(
title: resource.data[:title],
url: resource.relative_url,
order: resource.data[:nav_order],
parent_title: resource.data[:parent],
current: resource == @current_resource,
children: []
)
end

def sort_nodes!(nodes)
nodes.sort_by! { |n| [n.order || Float::INFINITY, n.title.to_s.downcase] }
nodes.each { |n| sort_nodes!(n.children) }
end
end
25 changes: 25 additions & 0 deletions components/bridgetown_stoa/sidebar.serb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<nav class="stoa-sidebar" aria-label="Documentation navigation">
<ul>
{% tree.each do |node| %}
{% if node.children.any? %}
<li class="stoa-nav-parent">
<a href="{{ node.url }}"{% if node.current %} aria-current="page"{% end %}>{{ node.title }}</a>
<details{% if section_open?(node) %} open{% end %}>
<summary aria-label="Toggle {{ node.title }} section"></summary>
<ul>
{% node.children.each do |child| %}
<li>
<a href="{{ child.url }}"{% if child.current %} aria-current="page"{% end %}>{{ child.title }}</a>
</li>
{% end %}
</ul>
</details>
</li>
{% else %}
<li>
<a href="{{ node.url }}"{% if node.current %} aria-current="page"{% end %}>{{ node.title }}</a>
</li>
{% end %}
{% end %}
</ul>
</nav>
Loading
Loading