diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a5158f..dcd97b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: `<p>` prints as `
`, not a + line break. ## [0.2.1] — 2026-08-08 diff --git a/README.md b/README.md index cf122f9..0a3a23c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/internal/api/client.go b/internal/api/client.go index 4418851..a23309c 100644 --- a/internal/api/client.go +++ b/internal/api/client.go @@ -7,6 +7,7 @@ import ( "html" "net/http" "net/url" + "regexp" "strconv" "strings" "time" @@ -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(`]*>.*?`) + +// 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, "
", "\n") s = strings.ReplaceAll(s, "
", "") s = strings.ReplaceAll(s, "", "") @@ -226,8 +232,5 @@ func CleanHTML(s string) string { s = strings.ReplaceAll(s, "", "\n```\n") s = strings.ReplaceAll(s, "", "`")
s = strings.ReplaceAll(s, "", "`")
- s = strings.ReplaceAll(s, "", " ")
- s = strings.ReplaceAll(s, "", "")
- return strings.TrimSpace(s)
+ return strings.TrimSpace(html.UnescapeString(s))
}
diff --git a/internal/api/client_test.go b/internal/api/client_test.go
index f6f0e75..d294133 100644
--- a/internal/api/client_test.go
+++ b/internal/api/client_test.go
@@ -208,6 +208,12 @@ func TestCleanHTML(t *testing.T) {
{"inline code", "use go run here", "use `go run` here"},
{"html entities", "this & that < those", "this & that < those"},
{"empty string", "", ""},
+ {"escaped markup stays", "use <p> and <code>", "use and "},
+ {
+ "auto-linked url",
+ `see https://example.com/a/ve... now`,
+ "see https://example.com/a/very/long now",
+ },
}
for _, tt := range tests {
diff --git a/internal/output/format.go b/internal/output/format.go
index 3d624ff..499a2ce 100644
--- a/internal/output/format.go
+++ b/internal/output/format.go
@@ -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 {
@@ -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")
}
}
diff --git a/internal/output/format_test.go b/internal/output/format_test.go
index 4998f28..4b1a8e2 100644
--- a/internal/output/format_test.go
+++ b/internal/output/format_test.go
@@ -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"},
}