Skip to content

Commit

Permalink
Merge pull request #78 from x-motemen/page
Browse files Browse the repository at this point in the history
support fixed pages
  • Loading branch information
Songmu authored Oct 8, 2023
2 parents bb8cf09 + 184524f commit 15a9f3e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
46 changes: 37 additions & 9 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,18 @@ func newBroker(bc *blogConfig) *broker {

func (b *broker) FetchRemoteEntries() ([]*entry, error) {
entries := []*entry{}
url := entryEndPointUrl(b.blogConfig)
urls := []string{
entryEndPointUrl(b.blogConfig),
fixedPageEndpointURL(b.blogConfig),
}
for url := ""; true; {
if url == "" {
if len(urls) == 0 {
break
}
url, urls = urls[0], urls[1:]
}

for {
feed, err := b.Client.GetFeed(url)
if err != nil {
return nil, err
Expand All @@ -45,16 +54,15 @@ func (b *broker) FetchRemoteEntries() ([]*entry, error) {
if err != nil {
return nil, err
}

entries = append(entries, e)
}

nextLink := feed.Links.Find("next")
if nextLink == nil {
break
if nextLink != nil {
url = nextLink.Href
} else {
url = ""
}

url = nextLink.Href
}

return entries, nil
Expand All @@ -66,6 +74,13 @@ func (b *broker) LocalPath(e *entry) string {
if b.OmitDomain == nil || !*b.OmitDomain {
paths = append(paths, b.BlogID)
}
// If possible, for fixed pages, we would like to dig a directory such as page/ to place md files,
// but it is difficult to solve by a simple method such as prepending a "page/" string
// if the path does not begin with an entry string. That is because if you are operating
// a subdirectory in Hatena Blog Media, you do not know where the root of the blog is.
// e.g.
// - https://example.com/subblog/entry/blog-entry
// - https://example.com/subblog/fixed-page
paths = append(paths, e.URL.Path+extension)
return filepath.Join(paths...)
}
Expand Down Expand Up @@ -141,8 +156,13 @@ func (b *broker) PutEntry(e *entry) error {
return b.Store(newEntry, path)
}

func (b *broker) PostEntry(e *entry) error {
endPoint := entryEndPointUrl(b.blogConfig)
func (b *broker) PostEntry(e *entry, isPage bool) error {
var endPoint string
if !isPage {
endPoint = entryEndPointUrl(b.blogConfig)
} else {
endPoint = fixedPageEndpointURL(b.blogConfig)
}
newEntry, err := asEntry(b.Client.PostEntry(endPoint, e.atom()))
if err != nil {
return err
Expand All @@ -162,3 +182,11 @@ func entryEndPointUrl(bc *blogConfig) string {
}
return fmt.Sprintf("https://blog.hatena.ne.jp/%s/%s/atom/entry", owner, bc.BlogID)
}

func fixedPageEndpointURL(bc *blogConfig) string {
owner := bc.Owner
if owner == "" {
owner = bc.Username
}
return fmt.Sprintf("https://blog.hatena.ne.jp/%s/%s/atom/page", owner, bc.BlogID)
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ var commandPost = &cli.Command{
&cli.BoolFlag{Name: "draft"},
&cli.StringFlag{Name: "title"},
&cli.StringFlag{Name: "custom-path"},
&cli.BoolFlag{Name: "page"},
},
Action: func(c *cli.Context) error {
blog := c.Args().First()
Expand Down Expand Up @@ -219,7 +220,7 @@ var commandPost = &cli.Command{
}

b := newBroker(blogConfig)
err = b.PostEntry(entry)
err = b.PostEntry(entry, c.Bool("page"))
if err != nil {
return err
}
Expand Down

0 comments on commit 15a9f3e

Please sign in to comment.