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

Add PreviewURL field #104

Merged
merged 3 commits into from
Oct 21, 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
3 changes: 2 additions & 1 deletion atom/atom.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ type Category struct {

// Control represents atom control
type Control struct {
Draft string `xml:"http://www.w3.org/2007/app draft"`
Draft string `xml:"http://www.w3.org/2007/app draft"`
Preview string `xml:"http://www.w3.org/2007/app preview"`
}

// Parse parses an atom xml from r and returns Feed
Expand Down
23 changes: 16 additions & 7 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type entryHeader struct {
Date *time.Time `yaml:"Date,omitempty"`
URL *entryURL `yaml:"URL"`
EditURL string `yaml:"EditURL"`
PreviewURL string `yaml:"PreviewURL,omitempty"`
IsDraft bool `yaml:"Draft,omitempty"`
CustomPath string `yaml:"CustomPath,omitempty"`
}
Expand Down Expand Up @@ -108,7 +109,8 @@ func (e *entry) atom() *atom.Entry {

if e.IsDraft {
atomEntry.Control = &atom.Control{
Draft: "yes",
Draft: "yes",
Preview: "yes",
}
}

Expand All @@ -135,6 +137,12 @@ func entryFromAtom(e *atom.Entry) (*entry, error) {
return nil, fmt.Errorf("could not find link[rel=edit]")
}

var previewLink string
p := e.Links.Find("preview")
if p != nil {
previewLink = p.Href
}

categories := make([]string, 0)
for _, c := range e.Category {
categories = append(categories, c.Term)
Expand All @@ -154,12 +162,13 @@ func entryFromAtom(e *atom.Entry) (*entry, error) {

return &entry{
entryHeader: &entryHeader{
URL: &entryURL{u},
EditURL: editLink.Href,
Title: e.Title,
Category: categories,
Date: updated,
IsDraft: isDraft,
URL: &entryURL{u},
EditURL: editLink.Href,
PreviewURL: previewLink,
Title: e.Title,
Category: categories,
Date: updated,
IsDraft: isDraft,
},
LastModified: e.Edited,
Content: e.Content.Content,
Expand Down
51 changes: 51 additions & 0 deletions entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ func TestDraftFullContent(t *testing.T) {
assert.Equal(t, draftContent, e.fullContent())
}

func TestNoDraftFullContent(t *testing.T) {
u, _ := url.Parse("http://hatenablog.example.com/1")
jst, _ := time.LoadLocation("Asia/Tokyo")
d := time.Date(2012, 12, 19, 0, 0, 0, 0, jst)

e := &entry{
entryHeader: &entryHeader{
URL: &entryURL{u},
EditURL: u.String() + "/edit",
Title: "所内#3",
Date: &d,
IsDraft: false,
},
LastModified: &d,
Content: "test\ntest2\n",
}
assert.Equal(t, content, e.fullContent())
}

func TestFrontmatterDraftEntryFromReader(t *testing.T) {
var ti *time.Time

Expand All @@ -113,6 +132,38 @@ func TestFrontmatterDraftEntryFromReader(t *testing.T) {
assert.Equal(t, "下書き\n", e.Content)
}

var draftWithPreviewContent = `---
Title: 所内#4
Date: 2012-12-20T00:00:00+09:00
URL: http://hatenablog.example.com/2
EditURL: http://hatenablog.example.com/2/edit
PreviewURL: http://hatenablog.example.com/2/preview
Draft: true
---

下書き
`

func TestDraftWithPreviewFullContent(t *testing.T) {
u, _ := url.Parse("http://hatenablog.example.com/2")
jst, _ := time.LoadLocation("Asia/Tokyo")
d := time.Date(2012, 12, 20, 0, 0, 0, 0, jst)

e := &entry{
entryHeader: &entryHeader{
URL: &entryURL{u},
EditURL: u.String() + "/edit",
PreviewURL: u.String() + "/preview",
Title: "所内#4",
Date: &d,
IsDraft: true,
},
LastModified: &d,
Content: "下書き\n",
}
assert.Equal(t, draftWithPreviewContent, e.fullContent())
}

var noCategory = `Title: 所内
Date: 2012-12-20T00:00:00+09:00
URL: http://hatenablog.example.com/2
Expand Down
Loading