Utopia::ProjectSourceUtopiaProjectSidebar

class Sidebar

Generates a sidebar navigation from markdown document headings.

Nested

Definitions

def self.build(document)

Build a sidebar from a markdown document by extracting headings.

Signature

parameter document Document

The document to extract headings from

returns Sidebar

Implementation

def self.build(document)
	entries = extract_headings_from_document(document)
	new(entries)
end

def initialize(entries)

Initialize with an array of entries.

Signature

parameter entries Array(Entry)

The navigation entries

Implementation

def initialize(entries)
	@entries = entries
end

attr :entries

The navigation entries.

Signature

attribute Array(Entry)

def any?

Check if there are any navigation entries.

Signature

returns Boolean

Implementation

def any?
	!entries.empty?
end

def to_html(sidebar: false, title: "Table of Contents")

Generate HTML markup for the sidebar navigation.

Signature

parameter sidebar Boolean

Whether this is rendered in a sidebar layout (unused, kept for compatibility)

parameter title String

The title for the navigation section

returns XRB::MarkupString

Implementation

def to_html(sidebar: false, title: "Table of Contents")
	return XRB::Markup.raw("") unless any?
	
	XRB::Builder.fragment do |builder|
		builder.tag :nav do
			builder.tag :heading do
				builder.text title
			end
			builder.tag :ul do
				entries.each do |entry|
					if entry.level > 2
						builder.tag :li, {class: "level-#{entry.level}"} do
							builder.tag :a, {href: "##{entry.anchor}"} do
								builder << entry.title_html
							end
						end
					else
						builder.tag :li do
							builder.tag :a, {href: "##{entry.anchor}"} do
								builder << entry.title_html
							end
						end
					end
				end
			end
		end
	end
end