Skip to content

Commit

Permalink
set the updated to nil when the entry is still draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Oct 14, 2023
1 parent 2e2bbd7 commit 5e231eb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
32 changes: 22 additions & 10 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type entryURL struct {
type entryHeader struct {
Title string `yaml:"Title"`
Category []string `yaml:"Category,omitempty"`
Date *time.Time `yaml:"Date"`
Date *time.Time `yaml:"Date,omitempty"`
URL *entryURL `yaml:"URL"`
EditURL string `yaml:"EditURL"`
IsDraft bool `yaml:"Draft,omitempty"`
Expand Down Expand Up @@ -140,24 +140,31 @@ func entryFromAtom(e *atom.Entry) (*entry, error) {
categories = append(categories, c.Term)
}

entry := &entry{
var isDraft bool
if e.Control != nil && e.Control.Draft == "yes" {
isDraft = true
}

// Set the updated to nil when the entry is still draft.
// But if the date is in the future, don't set to nil because it may be a reserved post.
updated := e.Updated
if updated != nil && isDraft && !time.Now().Before(*updated) {
updated = nil
}

return &entry{
entryHeader: &entryHeader{
URL: &entryURL{u},
EditURL: editLink.Href,
Title: e.Title,
Category: categories,
Date: e.Updated,
Date: updated,
IsDraft: isDraft,
},
LastModified: e.Edited,
Content: e.Content.Content,
ContentType: e.Content.Type,
}

if e.Control != nil && e.Control.Draft == "yes" {
entry.IsDraft = true
}

return entry, nil
}, nil
}

var delimReg = regexp.MustCompile(`---\n+`)
Expand All @@ -180,6 +187,11 @@ func entryFromReader(source io.Reader) (*entry, error) {
if err != nil {
return nil, err
}
// Set the updated to nil when the entry is still draft.
// But if the date is in the future, don't set to nil because it may be a reserved post.
if eh.IsDraft && eh.Date != nil && !time.Now().Before(*eh.Date) {
eh.Date = nil
}
content = c[2]
}
entry := &entry{
Expand Down
4 changes: 2 additions & 2 deletions entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ func TestDraftFullContent(t *testing.T) {
}

func TestFrontmatterDraftEntryFromReader(t *testing.T) {
jst, _ := time.LoadLocation("Asia/Tokyo")
var ti *time.Time

e, err := entryFromReader(strings.NewReader(draftContent))
assert.NoError(t, err)

assert.Equal(t, "所内#4", e.Title)
assert.True(t, e.Date.Equal(time.Date(2012, 12, 20, 0, 0, 0, 0, jst)))
assert.Equal(t, e.Date, ti)
assert.Equal(t, "http://hatenablog.example.com/2", e.URL.String())
assert.Equal(t, "http://hatenablog.example.com/2/edit", e.EditURL)
assert.True(t, e.IsDraft)
Expand Down

0 comments on commit 5e231eb

Please sign in to comment.