Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set the updated to nil when the entry is still draft #95

Merged
merged 1 commit into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading