diff options
| author | Paul Buetow <paul@buetow.org> | 2025-01-19 09:43:57 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-01-19 09:43:57 +0200 |
| commit | f520f130d266691301940f357967108d6a8cb7da (patch) | |
| tree | 26cca0c78b3884d867db836ec1607f0b909c2219 /internal/summary | |
| parent | 54e617e09027c8339a0f9096d61739ad353a5cf4 (diff) | |
internal gemini capsule links stay gemini://
Diffstat (limited to 'internal/summary')
| -rw-r--r-- | internal/summary/summary.go | 10 | ||||
| -rw-r--r-- | internal/summary/summary_test.go | 23 |
2 files changed, 31 insertions, 2 deletions
diff --git a/internal/summary/summary.go b/internal/summary/summary.go index 2cd88ac..50fbc22 100644 --- a/internal/summary/summary.go +++ b/internal/summary/summary.go @@ -72,7 +72,7 @@ func generateGemtext(args config.Args, entries []entry.Entry, title string) (str sb.WriteString("\n") for _, url := range urls { sb.WriteString("\n") - sb.WriteString(gemtextLink(url, 30)) + sb.WriteString(gemtextLink(args.GeminiCapsule, url, 30)) } } } @@ -149,10 +149,16 @@ func prepare(content string) string { return content } -func gemtextLink(url string, maxLen int) string { +func gemtextLink(geminiCapsule, url string, maxLen int) string { url = strings.TrimSpace(url) urlNoProto := regexp.MustCompile(`^[a-zA-Z]+://`).ReplaceAllString(url, "") + if strings.HasPrefix(urlNoProto, geminiCapsule) && + (strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")) { + // This is an internal link, so replace proto with gemini:// + url = "gemini://" + urlNoProto + } + if len(urlNoProto) <= maxLen { return "=> " + url + " " + urlNoProto } diff --git a/internal/summary/summary_test.go b/internal/summary/summary_test.go new file mode 100644 index 0000000..4b33609 --- /dev/null +++ b/internal/summary/summary_test.go @@ -0,0 +1,23 @@ +package summary + +import "testing" + +func TestGemtextLink(t *testing.T) { + const geminiCapsule = "foo.zone" + + table := map[string]string{ + "http://example.com": "=> http://example.com example.com", + "https://example.org": "=> https://example.org example.org", + "https://example.org/some/very/long/link/here?with=a&free=of¶meters=here": "=> https://example.org/some/very/long/link/here?with=a&free=of¶meters=here example.org/s...rameters=here", + "http://foo.zone": "=> gemini://foo.zone foo.zone", + "https://foo.zone": "=> gemini://foo.zone foo.zone", + "beer://foo.zone": "=> beer://foo.zone foo.zone", + "https://foo.zone/some/very/long/link/here?with=a&free=of¶meters=here": "=> gemini://foo.zone/some/very/long/link/here?with=a&free=of¶meters=here foo.zone/some...rameters=here", + } + + for url, expected := range table { + if result := gemtextLink(geminiCapsule, url, 30); result != expected { + t.Errorf("Expected '%s' but got '%s' with input '%s'", expected, result, url) + } + } +} |
