Skip to content

Commit abe8e42

Browse files
compscidrclaude
andcommitted
Hide plugin pages from nav when plugin is disabled
- Remove PageTypeResearch constant (now plugin-defined) - Mark ScholarID on Page as deprecated (kept for migration compat) - Add PageFilter to Blog struct, applied in GetNavPages to filter out pages owned by disabled plugins - Add IsPageTypeEnabled to registry for checking if the plugin that owns a page type is enabled - Wire up the filter in goblog.go after registry init - Update seed defaults and tests to use string literal "research" When the scholar plugin is disabled: - Research page disappears from navigation - Visiting /research shows "Page Not Available" 404 When re-enabled, everything comes back automatically. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 998ce79 commit abe8e42

6 files changed

Lines changed: 41 additions & 5 deletions

File tree

blog/blog.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@ import (
2424
"github.com/ikeikeikeike/go-sitemap-generator/v2/stm"
2525
)
2626

27+
// PageFilter is a function that decides whether a page should be shown.
28+
// Used to filter out pages owned by disabled plugins.
29+
type PageFilter func(page Page) bool
30+
2731
// Blog API handles non-admin functions of the blog like listing posts, tags
2832
// comments, etc.
2933
type Blog struct {
3034
db **gorm.DB // needs a double pointer to be able to update the db
3135
auth auth.IAuth
3236
Version string
37+
PageFilter PageFilter // optional filter set by plugin system
3338
commentLimiter map[string]time.Time
3439
limiterMu sync.Mutex
3540
}
@@ -354,9 +359,19 @@ func (b *Blog) TrackReferer(c *gin.Context, postID uint) {
354359
}
355360
}
356361
// GetNavPages returns enabled pages that should show in the navigation, ordered by nav_order.
362+
// Pages owned by disabled plugins are filtered out.
357363
func (b *Blog) GetNavPages() []Page {
358364
var pages []Page
359365
(*b.db).Where("enabled = ? AND show_in_nav = ?", true, true).Order("nav_order asc").Find(&pages)
366+
if b.PageFilter != nil {
367+
var filtered []Page
368+
for _, p := range pages {
369+
if b.PageFilter(p) {
370+
filtered = append(filtered, p)
371+
}
372+
}
373+
return filtered
374+
}
360375
return pages
361376
}
362377

blog/blog_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func TestBlogWorkflow(t *testing.T) {
197197

198198
// Create pages so dynamic page resolution works
199199
writingPage := blog.Page{Title: "Writing", Slug: "posts", PageType: blog.PageTypeWriting, ShowInNav: true, NavOrder: 1, Enabled: true}
200-
researchPage := blog.Page{Title: "Research", Slug: "research", PageType: blog.PageTypeResearch, ShowInNav: true, NavOrder: 2, Enabled: true, ScholarID: "SbUmSEAAAAAJ"}
200+
researchPage := blog.Page{Title: "Research", Slug: "research", PageType: "research", ShowInNav: true, NavOrder: 2, Enabled: true}
201201
aboutPage := blog.Page{Title: "About", Slug: "about", PageType: blog.PageTypeAbout, ShowInNav: true, NavOrder: 3, Enabled: true, Content: "About page content"}
202202
tagsPage := blog.Page{Title: "Tags", Slug: "tags", PageType: blog.PageTypeTags, ShowInNav: false, NavOrder: 4, Enabled: true}
203203
archivesPage := blog.Page{Title: "Archives", Slug: "archives", PageType: blog.PageTypeArchives, ShowInNav: false, NavOrder: 5, Enabled: true}

blog/page.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import "time"
55
// Page types
66
const (
77
PageTypeWriting = "writing"
8-
PageTypeResearch = "research"
98
PageTypeAbout = "about"
109
PageTypeCustom = "custom"
1110
PageTypeTags = "tags"
@@ -23,11 +22,11 @@ type Page struct {
2322
Content string `sql:"type:text;" json:"content"`
2423
HeroURL string `json:"hero_url"`
2524
HeroType string `json:"hero_type"` // "image" or "video"
26-
PageType string `json:"page_type"` // "writing", "research", "about", "custom"
25+
PageType string `json:"page_type"` // "writing", "research", "about", "custom", or plugin-defined
2726
ShowInNav bool `json:"show_in_nav"`
2827
NavOrder int `json:"nav_order"`
2928
Enabled bool `json:"enabled"`
30-
ScholarID string `json:"scholar_id,omitempty"`
29+
ScholarID string `json:"scholar_id,omitempty"` // deprecated: use scholar plugin settings instead
3130
PostTypeID *uint `json:"post_type_id,omitempty"`
3231
}
3332

goblog.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,14 @@ func main() {
319319
registry.StartScheduledJobs()
320320
}
321321

322+
// Filter nav pages: hide pages owned by disabled plugins
323+
_blog.PageFilter = func(page blog.Page) bool {
324+
if !registry.HasPageType(page.PageType) {
325+
return true // not a plugin page, always show
326+
}
327+
return registry.IsPageTypeEnabled(page.PageType)
328+
}
329+
322330
router.Use(CORS())
323331
router.Use(gplugin.Middleware(registry))
324332
store := cookie.NewStore([]byte(sessionKey))

plugin/registry.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,20 @@ func (r *Registry) GetNavItems() []PageDefinition {
237237
return items
238238
}
239239

240+
// IsPageTypeEnabled returns true if the plugin that owns the given page type is enabled.
241+
func (r *Registry) IsPageTypeEnabled(pageType string) bool {
242+
r.mu.RLock()
243+
defer r.mu.RUnlock()
244+
for _, p := range r.plugins {
245+
for _, page := range p.Pages() {
246+
if page.PageType == pageType {
247+
return r.IsPluginEnabled(p.Name())
248+
}
249+
}
250+
}
251+
return false
252+
}
253+
240254
// HasPageType returns true if any registered plugin (enabled or not) defines the given page type.
241255
func (r *Registry) HasPageType(pageType string) bool {
242256
r.mu.RLock()

tools/migrate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ I also enjoy driving, working on cars, video games, contributing to [open source
483483
Slug: "research",
484484
HeroURL: "/img/aidecentralized.jpg",
485485
HeroType: "image",
486-
PageType: blog.PageTypeResearch,
486+
PageType: "research", // owned by scholar plugin
487487
ShowInNav: true,
488488
NavOrder: 2,
489489
Enabled: true,

0 commit comments

Comments
 (0)