Skip to content

Commit

Permalink
feat!: When a post is deleted from Blogger, delete the Markdown version
Browse files Browse the repository at this point in the history
  • Loading branch information
slashtechno authored Jun 26, 2024
1 parent 0fc9e46 commit 96d8642
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 28 deletions.
27 changes: 11 additions & 16 deletions cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/slashtechno/cross-blogger/internal"
"github.com/slashtechno/cross-blogger/internal/platforms"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// watchCmd represents the watch command
Expand Down Expand Up @@ -83,24 +82,20 @@ var watchCmd = &cobra.Command{
// Assert that the source is Blogger
blogger, ok := source.(*platforms.Blogger)
if ok {
// Check if db is enabled
if viper.GetBool("db.enable") {
// For every destinationn, assert that it is Markdown
// If it is, pass it to Blogger.CleanMarkdownPosts
for _, dest := range destinationSlice {
if markdownDest, ok := dest.(*platforms.Markdown); ok {
// Check if overwriting is enabled
if markdownDest.Overwrite {
wg.Add(1)
go blogger.CleanMarkdownPosts(&wg, internal.ConfigViper.GetDuration("interval"), markdownDest, options, errChan)
} else {
log.Debug("Overwriting is disabled; not cleaning up posts", "destination", dest.GetName())
}
for _, dest := range destinationSlice {
if markdownDest, ok := dest.(*platforms.Markdown); ok {
// Check if overwriting is enabled
if markdownDest.Overwrite {
wg.Add(1)
go blogger.CleanMarkdownPosts(&wg, internal.ConfigViper.GetDuration("interval"), markdownDest, options, errChan)
} else {
log.Debug("Destination is not Markdown; not cleaning up posts", "destination", dest.GetName())
log.Debug("Overwriting is disabled; not cleaning up posts", "destination", dest.GetName())
}
} else {
log.Debug("Destination is not Markdown; not cleaning up posts", "destination", dest.GetName())
}
}

}
wg.Add(1)
go func() {
Expand All @@ -115,7 +110,7 @@ var watchCmd = &cobra.Command{
err := pushToDestinations(post, destinationSlice, false)

if err != nil {
log.Error("Error", "error", err)
log.Fatal("Error", "error", err)
}
// If an error occurs
case err := <-errChan:
Expand Down
29 changes: 24 additions & 5 deletions internal/platforms/blogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,9 @@ func (b Blogger) CleanMarkdownPosts(wg *sync.WaitGroup, interval time.Duration,
}
post := (*resp.Result().(*map[string]interface{}))
title := post["title"].(string)
slug := slug.Make(title) + ".md"
knownFiles = append(knownFiles, slug)
slug := slug.Make(title)
fileName := slug + ".md"
knownFiles = append(knownFiles, fileName)
}
// List all files in markdownDest.ContentDir
fs := afero.NewOsFs()
Expand All @@ -401,6 +402,7 @@ func (b Blogger) CleanMarkdownPosts(wg *sync.WaitGroup, interval time.Duration,
continue
}
if !utils.ContainsString(knownFiles, file.Name()) {
// With Hugo at least, `_index.md`
unkownFiles = append(unkownFiles, file.Name())
}
}
Expand All @@ -416,13 +418,30 @@ func (b Blogger) CleanMarkdownPosts(wg *sync.WaitGroup, interval time.Duration,
}
markdownString := string(fileBytes)
// Get the frontmatter for the file
_, _, frontmatter, err := markdownDest.ParseMarkdown(markdownString)
log.Debug("Got frontmatter", "frontmatter", frontmatter)
_, _, postFrontmatter, err := markdownDest.ParseMarkdown(markdownString)
if err != nil {
errChan <- err
return
}
// TODO: Check if the file is manage and delete it if it is
log.Debug("Got frontmatter", "frontmatter", postFrontmatter)
if postFrontmatter.Managed {
// Delete the file
err := fs.Remove(absPath)
if err != nil {
errChan <- err
return
}
// Comit and push the changes
if markdownDest.GitDir != "" {
slug := strings.TrimSuffix(file, filepath.Ext(file))
commitHash, err := markdownDest.Commit(slug, true)
log.Info("Committed and pushed changes", "hash", commitHash)
if err != nil {
errChan <- err
return
}
}
}
}
}

Expand Down
30 changes: 26 additions & 4 deletions internal/platforms/frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package platforms
import (
"errors"
"fmt"
"time"
)

// This is more just a set of defaults compatible with Hugo's frontmatter
Expand Down Expand Up @@ -89,17 +90,38 @@ func FrontmatterMappingFromInterface(m interface{}) (*FrontmatterMapping, error)
}

// Take a map and return a Frontmatter struct, taking FrontmatterMapping into account
func FrontmatterFromMap(m map[string]interface{}, frontmatterMapping FrontmatterMapping) *Frontmatter {
func FrontmatterFromMap(m map[string]interface{}, frontmatterMapping FrontmatterMapping) (*Frontmatter, error) {
frontmatterObject := &Frontmatter{}
if title, ok := m[frontmatterMapping.Title]; ok {
frontmatterObject.Title = title.(string)
}
if date, ok := m[frontmatterMapping.Date]; ok {
frontmatterObject.Date = date.(string)
// Convert the time.time to a string
if dateObject, ok := date.(time.Time); ok {
frontmatterObject.Date = dateObject.Format(time.RFC3339)
} else {
// Check if it's a string. If it's not a string or time.Time, return an error
if date, ok := date.(string); ok {
frontmatterObject.Date = date
} else {
return nil, errors.New("date is not a string or time.Time")
}
}
}
if lastUpdated, ok := m[frontmatterMapping.LastUpdated]; ok {
frontmatterObject.DateUpdated = lastUpdated.(string)
// Convert the time.time to a string
if lastUpdatedObject, ok := lastUpdated.(time.Time); ok {
frontmatterObject.DateUpdated = lastUpdatedObject.Format(time.RFC3339)
} else {
// Check if it's a string. If it's not a string or time.Time, return an error
if lastUpdated, ok := lastUpdated.(string); ok {
frontmatterObject.DateUpdated = lastUpdated
} else {
return nil, errors.New("date_updated is not a string or time.Time")
}
}
}

if description, ok := m[frontmatterMapping.Description]; ok {
frontmatterObject.Description = description.(string)
}
Expand All @@ -109,5 +131,5 @@ func FrontmatterFromMap(m map[string]interface{}, frontmatterMapping Frontmatter
if managed, ok := m[frontmatterMapping.Managed]; ok {
frontmatterObject.Managed = managed.(bool)
}
return frontmatterObject
return frontmatterObject, nil
}
10 changes: 7 additions & 3 deletions internal/platforms/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ func (m Markdown) Commit(slug string, push bool) (hash string, err error) {
return commitHash.String(), nil
}

func (m Markdown) ParseMarkdown(markdown string) (markdownWithoutFrontmatter string, html string, postFrontmatter *Frontmatter, err error) {
func (m Markdown) ParseMarkdown(markdown string) (markdownWithoutFrontmatter string, html string, frontmatterObject *Frontmatter, err error) {
err = nil
// Convert the markdown to HTML with Goldmark
// Use the Frontmatter extension to get the frontmatter
mdParser := goldmark.New(goldmark.WithExtensions(&goldmarkfrontmatter.Extender{
Expand All @@ -197,13 +198,16 @@ func (m Markdown) ParseMarkdown(markdown string) (markdownWithoutFrontmatter str
return "", "", nil, err
}
// Get the frontmatter
frontmatterObject := FrontmatterFromMap(parsedDoc.OwnerDocument().Meta(), m.FrontmatterMapping)
frontmatterObject, err = FrontmatterFromMap(parsedDoc.OwnerDocument().Meta(), m.FrontmatterMapping)
if err != nil {
return "", "", nil, err
}
// Check if title and canonical URL are set
if frontmatterObject.Title == "" {
return "", "", nil, err
}
if frontmatterObject.CanonicalUrl == "" {
log.Warn("canonical_url is not set in frontmatter")
log.Debug("canonical_url is not set in frontmatter")
}
// Convert the HTML to Markdown
html = buf.String()
Expand Down

0 comments on commit 96d8642

Please sign in to comment.