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 2 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
19 changes: 12 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,8 @@ func entryFromAtom(e *atom.Entry) (*entry, error) {
return nil, fmt.Errorf("could not find link[rel=edit]")
}

previewLink := e.Links.Find("preview")

categories := make([]string, 0)
for _, c := range e.Category {
categories = append(categories, c.Term)
Expand All @@ -154,12 +158,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.Href,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PreviewLinkはnilableだと思うので、nilチェックするようにして下さい。
(調べてはいませんが、公開後のエントリーや、はてなブログ管理画面から作られた下書きでpreviewを発行していないものは link rel="preview" がない気がしますし、その可能性はケアしておいたほうが良いと思います

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます。

実装時に PreviewLink が nil のケースは考えたのですが、コメントにあるように公開後や preview=no の場合 PreviewLink に nil が来るケースはあると思います。
しかし、alternateLink や EditLink と同様に nil が来た際エラーとして return するべきかというと、PreviewLink の場合 nil のケースは上記のように正常動作として許容されるべきかと思います。結果、nil チエックをしたとして、そのチェックの結果何を処理すべきかが思いつかずとりあえずひとまず nil チェックを飛ばした次第です。

PreviewLink が nil の場合の処理どうしましょうか・・・?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確認ありがとうございます。previewLinkがnilの場合、previewLink.Hrefにアクセスするとnil pointer exceptionが起きてしまうので、ハンドリングは必要です。
entryHeader.PreviewURL をomitemptyに設定してもらっているので、プレビューリンクがない場合に空文字列を指定するという方針で良いのではないでしょうか。空文字列を渡しておけばプFrontMatterに書かれなくなるので。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previewLink.Href のこと忘れてましたので nil チェック追加しました 🙇

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