Skip to content

Commit

Permalink
Merge pull request #104 from Mahito/add_preview_option
Browse files Browse the repository at this point in the history
Add PreviewURL field
  • Loading branch information
Songmu committed Oct 21, 2023
2 parents 8d225d4 + 030c74f commit fea1afb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
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

0 comments on commit fea1afb

Please sign in to comment.