Try two date formats as block name

This commit is contained in:
Peter Stuifzand 2021-01-17 15:38:48 +01:00
parent 36dab44a24
commit 8f65d459ff
2 changed files with 33 additions and 8 deletions

35
file.go
View File

@ -41,7 +41,6 @@ type Block struct {
Parent string
}
// ListItemV2 is way to convert from old structure to new structure
type ListItemV2 struct {
ID ID
@ -127,24 +126,38 @@ func convertBlocksToListItems(current string, blocks BlockResponse, indent int)
return listItems
}
type titleOption struct {
date bool
timeObj time.Time
}
// 1_januari_2021
// 2021-01-01
func (fp *FilePages) Get(name string) Page {
var sw stopwatch
sw.Start("Get " + name)
defer sw.Stop()
names := []string{name}
var names []string
date := false
t, err := time.Parse("2006-01-02", name)
if err == nil {
date = true
names = append(names, formatDateTitle(t), name)
}
for _, name := range names {
blocks, err := loadBlocks(fp.dirname, name)
if err != nil && errors.Is(err, BlockNotFound) {
continue
}
return fp.blocksBackendGet(name, blocks)
return fp.blocksBackendGet(name, blocks, titleOption{date, t})
}
return fp.oldPagesBackend(name)
}
func (fp *FilePages) blocksBackendGet(name string, blocks BlockResponse) Page {
func (fp *FilePages) blocksBackendGet(name string, blocks BlockResponse, option titleOption) Page {
buf := bytes.Buffer{}
current := blocks.PageID
listItems := convertBlocksToListItems(current, blocks, 0)
@ -162,9 +175,11 @@ func (fp *FilePages) blocksBackendGet(name string, blocks BlockResponse) Page {
refs = nil
}
title := formatTitle(blocks.Texts[name], option)
return Page{
Name: name,
Title: blocks.Texts[name],
Title: title,
Content: buf.String(),
Refs: refs,
Blocks: blocks,
@ -172,6 +187,13 @@ func (fp *FilePages) blocksBackendGet(name string, blocks BlockResponse) Page {
}
}
func formatTitle(title string, option titleOption) string {
if option.date {
return formatDateTitle(option.timeObj)
}
return title
}
func (fp *FilePages) oldPagesBackend(title string) Page {
name := strings.Replace(title, " ", "_", -1)
title = strings.Replace(title, "_", " ", -1)
@ -418,7 +440,6 @@ func saveBlocksFromPage(dirname string, page Page) error {
return err
}
func loadBlocks(dirname, rootBlockID string) (BlockResponse, error) {
resp := BlockResponse{
rootBlockID,

View File

@ -99,6 +99,10 @@ func (sw *stopwatch) Stop() {
func todayPage() string {
now := time.Now()
return formatDateTitle(now)
}
func formatDateTitle(date time.Time) string {
months := []string{
"",
"januari",
@ -114,7 +118,7 @@ func todayPage() string {
"november",
"december",
}
return fmt.Sprintf("%d_%s_%d", now.Day(), months[now.Month()], now.Year())
return fmt.Sprintf("%d_%s_%d", date.Day(), months[date.Month()], date.Year())
}
func PageTitle(pageText string) (string, error) {