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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- The sidebar footer is now its own component, `BridgetownStoa::SidebarFooter`, rendered by `bridgetown-stoa/layout` in place of the inline markup. A host site can replace it by adding `src/_components/bridgetown_stoa/sidebar_footer.serb`, without copying the whole layout.
- `BridgetownStoa::Shadowable`, a mixin that makes a gem component look for its template in the host site's components directory before falling back to the gem's copy. Bridgetown normally resolves a component's template next to its Ruby class, which for a gem component is inside the gem; this mixin is what lets a site override just the `.serb`. Both `Sidebar` and `SidebarFooter` use it, so `src/_components/bridgetown_stoa/sidebar.serb` now shadows the navigation template too.

### Changed
- `script/release` accepts `--otp CODE` and forwards it to `gem push` via `GEM_HOST_OTP_CODE`, so a TOTP code can stand in for the WebAuthn browser handoff.

## [0.4.0] - 2026-09-24
Expand Down
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,40 @@ The layout is self-contained: it renders a full HTML document (doctype, `<head>`

The document title is `{page title} | {site metadata title}` when a page sets `title:` in front matter, and just the site title otherwise. The layout also injects `asset_path :css` / `:js` so your site's esbuild bundle loads automatically.

### Overriding the layout
### Overriding parts of the layout

The sidebar chrome is built from components that a host site can shadow one at a time, without copying the whole layout. Drop a template with the matching path into your site's `src/_components` directory and Stoa renders yours instead of its own:

| Component | Shadow at | Renders |
| --- | --- | --- |
| `BridgetownStoa::SidebarFooter` | `src/_components/bridgetown_stoa/sidebar_footer.serb` | The attribution footer at the bottom of the sidebar |
| `BridgetownStoa::Sidebar` | `src/_components/bridgetown_stoa/sidebar.serb` | The navigation tree (the template has access to `tree` and `section_open?`) |

For example, to add a line to the footer:

```html
<!-- src/_components/bridgetown_stoa/sidebar_footer.serb -->
<footer class="stoa-sidebar-footer">
<small>
A <a href="https://example.com">My Org</a> project.
Built with <a href="https://www.bridgetownrb.com">Bridgetown</a>
<span>+</span>
<a href="https://github.com/Guided-Rails/bridgetown-stoa">Stoa</a>.
</small>
</footer>
```

Only the template is replaced; the component's Ruby class still comes from the gem, so you keep any behavior it gains in later releases. Any template extension Bridgetown's components support (`.serb`, `.erb`, `.slim`, `.haml`) works.

### Overriding the whole layout

To customize the chrome, shadow the layout in your own site:
To replace the chrome entirely, shadow the layout itself:

```
src/_layouts/bridgetown-stoa/layout.serb
```

Bridgetown's layout resolution picks the host site's file over the gem's, so you can replace the entire layout or copy ours and edit it. (More granular override points — head, nav, footer partials — will land as the theme grows.)
Bridgetown's layout resolution picks the host site's file over the gem's, so you can replace the entire layout or copy ours and edit it. Prefer shadowing a component when you only need to change one region, since a copied layout won't pick up later changes to the gem's chrome.

## Development

Expand Down
33 changes: 33 additions & 0 deletions components/bridgetown_stoa/shadowable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

# Lets a host site override a Stoa component's template without redefining
# its Ruby class. Bridgetown normally looks for a component's template next
# to the file that defines the class, which for a gem-provided component is
# inside the gem. Extending a component with this module makes it look in the
# site's own components directory first (for example
# src/_components/bridgetown_stoa/sidebar_footer.serb) and only then fall back
# to the template shipped with the gem.
module BridgetownStoa::Shadowable
using Bridgetown::Refinements

def component_template_path
@_tmpl_path ||= site_template_path || super
end

private

def site_template_path
site = Bridgetown::Current.site
return unless site

relative_name = name.underscore
site.config.components_load_paths.reverse_each do |load_path|
supported_template_extensions.each do |ext|
candidate = File.join(load_path, "#{relative_name}.#{ext}")
return candidate if File.exist?(candidate)
end
end

nil
end
end
2 changes: 2 additions & 0 deletions components/bridgetown_stoa/sidebar.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class BridgetownStoa::Sidebar < Bridgetown::Component
extend BridgetownStoa::Shadowable

Node = Struct.new(:title, :url, :order, :parent_title, :current, :children, keyword_init: true)

def initialize(site:, current_resource:)
Expand Down
9 changes: 9 additions & 0 deletions components/bridgetown_stoa/sidebar_footer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

# The attribution footer at the bottom of the sidebar. Rendered by the
# bridgetown-stoa/layout template. Shadow it in a host site at
# src/_components/bridgetown_stoa/sidebar_footer.serb to change the footer
# without copying the whole layout.
class BridgetownStoa::SidebarFooter < Bridgetown::Component
extend BridgetownStoa::Shadowable
end
7 changes: 7 additions & 0 deletions components/bridgetown_stoa/sidebar_footer.serb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<footer class="stoa-sidebar-footer">
<small>
Built with <a href="https://www.bridgetownrb.com">Bridgetown</a>
<span>+</span>
<a href="https://github.com/Guided-Rails/bridgetown-stoa">Stoa</a>.
</small>
</footer>
8 changes: 1 addition & 7 deletions layouts/bridgetown-stoa/layout.serb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@
<a href="/" class="stoa-site-title">{{ site.metadata.title }}</a>
</header>
{%= render BridgetownStoa::Sidebar.new(site: site, current_resource: resource) %}
<footer class="stoa-sidebar-footer">
<small>
Built with <a href="https://www.bridgetownrb.com">Bridgetown</a>
<span>+</span>
<a href="https://github.com/Guided-Rails/bridgetown-stoa">Stoa</a>.
</small>
</footer>
{%= render BridgetownStoa::SidebarFooter.new %}
</aside>
<main class="stoa-main">
<div class="stoa-content">
Expand Down
53 changes: 53 additions & 0 deletions test/test_sidebar_footer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

require_relative "helper"

class TestSidebarFooter < Bridgetown::TestCase
SHADOW_DIR = File.join(SOURCE_DIR, "_components")
SHADOW_PATH = File.join(SHADOW_DIR, "bridgetown_stoa/sidebar_footer.serb")

describe "sidebar footer" do
it "renders the default attribution inside the sidebar" do
contents = File.read(dest_dir("index.html"))
pattern = %r{
<aside[^>]*>.*
<footer\sclass="stoa-sidebar-footer">.*Built\swith.*Bridgetown.*Stoa.*</footer>.*
</aside>
}mx
assert_match(pattern, contents)
end

it "can be shadowed by a template in the host site's components directory" do
with_shadowed_footer("<footer class=\"custom-footer\">A Guided Rails project.</footer>") do
contents = File.read(dest_dir("index.html"))
assert_match(%r{<footer class="custom-footer">A Guided Rails project\.</footer>}, contents)
refute_includes contents, "Built with"
end
end
end

private

def with_shadowed_footer(markup)
FileUtils.mkdir_p(File.dirname(SHADOW_PATH))
File.write(SHADOW_PATH, markup)
rebuild_site
yield
ensure
FileUtils.rm_rf(SHADOW_DIR)
rebuild_site
end

# Bridgetown memoizes a component's template on the class, so a template
# added after the first build is only picked up once that cache is cleared.
def rebuild_site
%i(@_tmpl_path @_tmpl_content @_tmpl).each do |ivar|
next unless BridgetownStoa::SidebarFooter.instance_variable_defined?(ivar)

BridgetownStoa::SidebarFooter.remove_instance_variable(ivar)
end
with_metadata title: "My Awesome Site" do
@site.process
end
end
end
Loading