Skip to content
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
- Anything older than a day shows its date, not "3mo ago".

### Fixed
- Exit codes now match the docs. Network failures, rate limits and server
errors exit 8, missing items and users exit 5, and an empty `hn front` exits 3.
- `hn status --json` exits 8 when the API is down.
- `hn user` looks profiles up through the Firebase API, because Algolia answers
500 for unknown users.
- Links appear once, with their full URL. They printed twice, the second time
shortened.
- Escaped markup in comments stays literal: `&lt;p&gt;` prints as `<p>`, not a
line break.

## [0.2.1] — 2026-08-08

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ Agent-friendly CLI for [Hacker News](https://news.ycombinator.com) via the [Algo
```bash
$ hn search "local LLMs" -n 3
5632 results:
1. [2127pts 963c] Running Local LLMs Is a Waste of Time @dang 3mo ago
1. [2127pts 963c] Running Local LLMs Is a Waste of Time @dang 2026-01-14
https://example.com/local-llms
HN: https://news.ycombinator.com/item?id=43163011
2. [891pts 412c] Ollama: Run LLMs Locally @thunderbong 8mo ago
2. [891pts 412c] Ollama: Run LLMs Locally @thunderbong 2025-08-02
https://ollama.ai
HN: https://news.ycombinator.com/item?id=42591244
3. [654pts 287c] How I Run LLMs on a Raspberry Pi @tosh 1y ago
3. [654pts 287c] How I Run LLMs on a Raspberry Pi @tosh 2025-04-11
https://example.com/rpi-llms
HN: https://news.ycombinator.com/item?id=41023547

Expand Down
15 changes: 9 additions & 6 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"html"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -213,9 +214,14 @@ func (c *Client) HealthCheck() (time.Duration, error) {
return elapsed, nil
}

// CleanHTML strips HTML entities and basic tags from HN text.
// linkTag matches HN's auto-linked URLs, which repeat the URL as a shortened
// link text.
var linkTag = regexp.MustCompile(`<a href="([^"]*)"[^>]*>.*?</a>`)

// CleanHTML strips HTML entities and basic tags from HN text. Entities are
// decoded last so that escaped markup in a comment stays literal.
func CleanHTML(s string) string {
s = html.UnescapeString(s)
s = linkTag.ReplaceAllString(s, "$1")
s = strings.ReplaceAll(s, "<p>", "\n")
s = strings.ReplaceAll(s, "</p>", "")
s = strings.ReplaceAll(s, "<i>", "")
Expand All @@ -226,8 +232,5 @@ func CleanHTML(s string) string {
s = strings.ReplaceAll(s, "</code></pre>", "\n```\n")
s = strings.ReplaceAll(s, "<code>", "`")
s = strings.ReplaceAll(s, "</code>", "`")
s = strings.ReplaceAll(s, "<a href=\"", "")
s = strings.ReplaceAll(s, "\" rel=\"nofollow\">", " ")
s = strings.ReplaceAll(s, "</a>", "")
return strings.TrimSpace(s)
return strings.TrimSpace(html.UnescapeString(s))
}
6 changes: 6 additions & 0 deletions internal/api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ func TestCleanHTML(t *testing.T) {
{"inline code", "use <code>go run</code> here", "use `go run` here"},
{"html entities", "this &amp; that &lt; those", "this & that < those"},
{"empty string", "", ""},
{"escaped markup stays", "use &lt;p&gt; and &lt;code&gt;", "use <p> and <code>"},
{
"auto-linked url",
`see <a href="https:&#x2F;&#x2F;example.com&#x2F;a&#x2F;very&#x2F;long" rel="nofollow">https:&#x2F;&#x2F;example.com&#x2F;a&#x2F;ve...</a> now`,
"see https://example.com/a/very/long now",
},
}

for _, tt := range tests {
Expand Down
11 changes: 4 additions & 7 deletions internal/output/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ func countComments(children []api.Item) int {
return count
}

// timeAgo parses an HN timestamp and returns a human-readable relative time.
// The Algolia API returns RFC 3339 from search endpoints but uses a
// timeAgo parses an HN timestamp and returns a relative time within the last
// day and the date before that, which stays meaningful outside the current
// moment. The Algolia API returns RFC 3339 from search endpoints but uses a
// millisecond-precision variant (2006-01-02T15:04:05.000Z) from item endpoints,
// so both formats must be attempted.
func timeAgo(created string) string {
Expand All @@ -178,11 +179,7 @@ func timeAgo(created string) string {
return fmt.Sprintf("%dm ago", int(d.Minutes()))
case d < 24*time.Hour:
return fmt.Sprintf("%dh ago", int(d.Hours()))
case d < 30*24*time.Hour:
return fmt.Sprintf("%dd ago", int(d.Hours()/24))
case d < 365*24*time.Hour:
return fmt.Sprintf("%dmo ago", int(d.Hours()/(24*30)))
default:
return fmt.Sprintf("%dy ago", int(d.Hours()/(24*365)))
return t.UTC().Format("2006-01-02")
}
}
3 changes: 2 additions & 1 deletion internal/output/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func TestTimeAgo(t *testing.T) {
{"just now", time.Now().Add(-10 * time.Second).Format(time.RFC3339), "just now"},
{"minutes ago", time.Now().Add(-30 * time.Minute).Format(time.RFC3339), "30m ago"},
{"hours ago", time.Now().Add(-5 * time.Hour).Format(time.RFC3339), "5h ago"},
{"days ago", time.Now().Add(-3 * 24 * time.Hour).Format(time.RFC3339), "3d ago"},
{"older shows the date", "2024-03-05T12:00:00Z", "2024-03-05"},
{"older in millisecond format", "2024-03-05T12:00:00.000Z", "2024-03-05"},
{"invalid returns raw", "not-a-date", "not-a-date"},
{"millisecond format", time.Now().UTC().Add(-1 * time.Hour).Format("2006-01-02T15:04:05.000Z"), "1h ago"},
}
Expand Down
Loading