From 3e19ce9be07a637d8c492d6687a6371b16be939b Mon Sep 17 00:00:00 2001 From: shbernal Date: Fri, 30 Oct 2026 13:42:44 +0100 Subject: [PATCH] fix(output): print links once and show dates Comments printed every link twice, the second copy shortened with "...", which cost tokens and carried nothing. Tags are now stripped before entities are decoded. In the old order a comment discussing `<p>` lost it to a line break. Anything older than a day shows its date. "3mo ago" says little in a saved note, and an agent has to work out what it means. --- CHANGELOG.md | 9 +++++++++ README.md | 6 +++--- internal/api/client.go | 15 +++++++++------ internal/api/client_test.go | 6 ++++++ internal/output/format.go | 11 ++++------- internal/output/format_test.go | 3 ++- 6 files changed, 33 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e2b8d5..b156c0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,15 @@ 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 +- 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 ### Fixed diff --git a/README.md b/README.md index e07e4dc..d977eae 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 5dc1595..f2e1b35 100644 --- a/internal/api/client.go +++ b/internal/api/client.go @@ -6,6 +6,7 @@ import ( "html" "net/http" "net/url" + "regexp" "strconv" "strings" "time" @@ -206,9 +207,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, "", "") @@ -219,8 +225,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 9f3fc5d..cda5fed 100644 --- a/internal/api/client_test.go +++ b/internal/api/client_test.go @@ -147,6 +147,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"}, }