This commit is contained in:
2026-05-28 21:34:46 +02:00
parent 51bf489449
commit 5844a870ce
3 changed files with 44 additions and 48 deletions
+20 -31
View File
@@ -71,14 +71,13 @@ func (d *diaryHandler) dateShortcutRedirect(root, fsPath, urlPath string) (strin
switch base {
case "today":
dayHeading := fmt.Sprintf("%s-%s-%s", year, month, day)
if dayHeadingExists(diaryRootFS, year, dayHeading) {
raw, _ := os.ReadFile(filepath.Join(diaryRootFS, year, "index.md"))
sections := splitSections(raw)
if _, found := findSectionIndex(sections, dayHeading); found {
return yearURL + "#" + dayHeading, true
}
// Missing day: route through the insert flow so today's section
// is spliced in at the right chronological position.
yearFS := filepath.Join(diaryRootFS, year)
raw, _ := os.ReadFile(filepath.Join(yearFS, "index.md"))
sections := splitSections(raw)
insertIdx := computeInsertIndex(sections, dayHeading)
return fmt.Sprintf("%s?edit&insert_before=%d&heading=%s",
yearURL, insertIdx, url.QueryEscape(dayHeading)), true
@@ -245,14 +244,14 @@ func findSectionIndex(sections [][]byte, target string) (int, bool) {
return 0, false
}
// computeInsertIndex returns the index at which a new day section with the
// given target date heading (YYYY-MM-DD) should be spliced in to keep date
// sections chronologically ordered. Only date-format headings — `YYYY`,
// `YYYY-MM`, or `YYYY-MM-DD` — participate in the comparison; non-date
// headings (e.g. `### Movies` in a year intro) are skipped so the new day
// is placed relative to the surrounding date sections, not the intro.
// Falls back to len(sections) when target is greater than every date
// heading. String comparison works for ISO dates.
// computeInsertIndex returns the section index at which a new date heading
// (target = `YYYY-MM` or `YYYY-MM-DD`) should be spliced in to keep date
// sections chronologically ordered. Only date-format headings participate in
// the comparison; non-date headings (e.g. `## Events` in a year intro) are
// skipped so the new section is placed relative to the surrounding date
// sections, not the intro. Falls back to len(sections) when target is
// greater than every date heading. ISO formatting means string comparison
// is equivalent to chronological order.
func computeInsertIndex(sections [][]byte, target string) int {
for i := 1; i < len(sections); i++ {
_, text := sectionHeading(sections[i])
@@ -297,18 +296,6 @@ func isDateHeading(text string) bool {
return true
}
// dayHeadingExists reads the year file and reports whether a `### date`
// section exists with the given heading text (e.g. "2026-05-28").
func dayHeadingExists(diaryRootFS, year, dateText string) bool {
raw, err := os.ReadFile(filepath.Join(diaryRootFS, year, "index.md"))
if err != nil {
return false
}
sections := splitSections(raw)
_, ok := findSectionIndex(sections, dateText)
return ok
}
// daysWithEntriesByMonth returns a `month → set[day]` map of `### YYYY-MM-DD`
// sections in the year's index.md. Used by the calendar widget to populate
// all 12 month grids in a single file read.
@@ -652,8 +639,9 @@ func sectionBody(section []byte) template.HTML {
return renderMarkdown(body)
}
// renderDiaryYear renders the full year file with photos attached to each
// `### YYYY-MM-DD` section.
// renderDiaryYear renders the year page: every section from the year file
// (with photos attached to `### YYYY-MM-DD` headings) plus virtual entries
// for every month/day slot the file doesn't yet contain.
func renderDiaryYear(yearFS, yearURL string) template.HTML {
year, err := strconv.Atoi(filepath.Base(yearFS))
if err != nil {
@@ -663,16 +651,17 @@ func renderDiaryYear(yearFS, yearURL string) template.HTML {
sections := splitSections(raw)
photos := yearPhotos(yearFS, yearURL)
out := buildSectionsForRange(sections, photos, 1, len(sections), yearURL)
out := buildFileSections(sections, photos, yearURL)
out = appendVirtualEntries(out, sections, photos, year, yearURL)
return renderDiaryContent(out)
}
// buildSectionsForRange converts raw splitSections entries in [start, end)
// into rendered diarySection entries, attaching photos to day sections.
func buildSectionsForRange(sections [][]byte, photos []diaryPhoto, start, end int, yearURL string) []diarySection {
// buildFileSections converts the year file's sections (skipping the
// pre-heading section 0) into rendered diarySection entries. Photos are
// attached to level-3 headings whose text parses as `YYYY-MM-DD`.
func buildFileSections(sections [][]byte, photos []diaryPhoto, yearURL string) []diarySection {
var out []diarySection
for i := start; i < end; i++ {
for i := 1; i < len(sections); i++ {
level, text := sectionHeading(sections[i])
if level == 0 {
continue