From c9a30a376720d044b35f319a443a3dedf6fb8772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=CC=81scar=20M=2E=20Lage?= Date: Fri, 11 Oct 2024 22:48:21 +0200 Subject: [PATCH] Fix: title cant be updated Doesn't make sense because the first thing it does is to search by title in disk The md won't be found --- internal/books/controller.go | 28 +++++++++++++++++++++++++++- utils/markdown.go | 2 -- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/internal/books/controller.go b/internal/books/controller.go index 951ea40..f87bff9 100644 --- a/internal/books/controller.go +++ b/internal/books/controller.go @@ -25,12 +25,29 @@ func LoadBooks() ([]Book, error) { return books, nil } -func ProcessBooks(books []Book) error { +func ProcessBooks(books []Book, update bool) error { fmt.Printf(" B O O K S\n") utils.Sep() for _, book := range books { fmt.Printf("Title: %s, Author: %s, ID: %s\n", book.Title, book.Author, book.ID) + // If we're updating, the process is a bit different + if update { + outputDir := os.Getenv("MARKDOWN_OUTPUT_BOOKS_DIR") + mdFilePath := filepath.Join(outputDir, fmt.Sprintf("%s.md", utils.Sluggify(book.Title))) + frontmatter, content, err := utils.LoadMarkdown(mdFilePath) + if err != nil { + fmt.Printf(" ! Error loading markdown frontmatter for book %s: %v\n", book.Title, err) + continue + } + updatedFrontmatter := updateFrontmatterWithYAML(frontmatter, book) + err = utils.SaveUpdatedMarkdown(mdFilePath, updatedFrontmatter, content) + if err != nil { + fmt.Printf(" ! Error saving updated markdown for book %s: %v\n", book.Title, err) + continue + } + } + // If we dont have ID, search movie by Title and get the ID if book.ID == "" { err := SearchBookByTitle(book.Title, &book) @@ -85,3 +102,12 @@ func generateBookMarkdown(book Book) error { return utils.GenerateMarkdown(templatePath, outputPath, data) } + +// Helper function to update only YAML fields that exist in both the frontmatter and YAML data +func updateFrontmatterWithYAML(frontmatter utils.FrontMatter, book Book) utils.FrontMatter { + // Update fields (adapt based on your actual Book struct) + frontmatter.Progress = book.Progress + frontmatter.Date = book.Date + frontmatter.Rate = book.Rate + return frontmatter +} diff --git a/utils/markdown.go b/utils/markdown.go index 2a63441..f59326a 100644 --- a/utils/markdown.go +++ b/utils/markdown.go @@ -97,14 +97,12 @@ func SaveUpdatedMarkdown(filepath string, frontmatter FrontMatter, content strin // Define regex patterns to match specific fields in the frontmatter fieldPatterns := map[string]string{ - "title": fmt.Sprintf(`(?m)^title: "(.*)"`), "progress": fmt.Sprintf(`(?m)^progress: "(.*)"`), "date": fmt.Sprintf(`(?m)^date: (\d+)$`), "rate": fmt.Sprintf(`(?m)^rate: (\d+\.?\d*)$`), } // Replace the relevant fields in the frontmatter using regex substitution - fileContent = updateFieldInFrontmatter(fileContent, "title", frontmatter.Title, fieldPatterns["title"]) fileContent = updateFieldInFrontmatter(fileContent, "progress", frontmatter.Progress, fieldPatterns["progress"]) fileContent = updateFieldInFrontmatter(fileContent, "date", fmt.Sprintf("%d", frontmatter.Date), fieldPatterns["date"]) fileContent = updateFieldInFrontmatterFloat(fileContent, "rate", frontmatter.Rate, fieldPatterns["rate"])