Thanks to visit codestin.com
Credit goes to github.com

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
12 changes: 11 additions & 1 deletion content/bcontent/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,26 @@ func NewPage(source fs.FS, filename string) (*Page, error) {
return pg, err
}

const unsetTitle = "$UNSET_TITLE"

// Defaults sets default values for the page based on its filename.
func (pg *Page) Defaults() {
pg.Name = strcase.ToSentence(strings.TrimSuffix(pg.Filename, filepath.Ext(pg.Filename)))
pg.URL = strcase.ToKebab(pg.Name)
pg.Title = pg.Name
pg.Title = unsetTitle
pg.Categories = []string{"Other"}
}

// ReadMetadata reads the page metadata from the front matter of the page file,
// if there is any.
func (pg *Page) ReadMetadata() error {
// Ensure the default title is set if it was not manually set (need defer).
defer func() {
if pg.Title == unsetTitle {
pg.Title = pg.Name
}
}()

f, err := pg.Source.Open(pg.Filename)
if err != nil {
return err
Expand Down
20 changes: 19 additions & 1 deletion content/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ type Content struct {
pagesByCategory map[string][]*bcontent.Page

// categories has all unique [bcontent.Page.Categories], sorted such that the categories
// with the most pages are listed first.
// with the most pages are listed first. "Other" is always last, and is used for pages that
// do not have a category, unless they are a category themselves.
categories []string

// history is the history of pages that have been visited.
Expand Down Expand Up @@ -297,12 +298,29 @@ func (ct *Content) SetSource(source fs.FS) *Content {
}))
ct.categories = maps.Keys(ct.pagesByCategory)
slices.SortFunc(ct.categories, func(a, b string) int {
if a == "Other" {
return 1
}
if b == "Other" {
return -1
}
v := cmp.Compare(len(ct.pagesByCategory[b]), len(ct.pagesByCategory[a]))
if v != 0 {
return v
}
return cmp.Compare(a, b)
})
// Pages that are a category are already represented in the categories tree,
// so they do not belong in the "Other" category.
ct.pagesByCategory["Other"] = slices.DeleteFunc(ct.pagesByCategory["Other"], func(pg *bcontent.Page) bool {
_, isCategory := ct.pagesByCategory[pg.Name]
if isCategory {
pg.Categories = slices.DeleteFunc(pg.Categories, func(c string) bool {
return c == "Other"
})
}
return isCategory
})

if url := ct.getWebURL(); url != "" {
ct.Open(url)
Expand Down
Loading