diff --git a/cmd/web.go b/cmd/web.go index b4c1d26..f2e21a9 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -203,7 +203,7 @@ func extractAndConvertContent(urlStr string) (string, error) { return header + markdown + "\n\n", nil } -func getFilenameFromContent(content, url string) string { +func getFilenameFromContent(content, urlStr string) string { // Try to extract title from content titleStart := strings.Index(content, "") titleEnd := strings.Index(content, "") @@ -212,8 +212,14 @@ func getFilenameFromContent(content, url string) string { return sanitizeFilename(title) + ".md" } - // If no title found, use the URL - return sanitizeFilename(url) + ".md" + // If no title found, use the URL without the protocol + parsedURL, err := url.Parse(urlStr) + if err != nil { + return "untitled.md" + } + + filename := parsedURL.Host + parsedURL.Path + return sanitizeFilename(filename) + ".md" } func sanitizeFilename(name string) string { diff --git a/cmd/web_test.go b/cmd/web_test.go index 0823049..a452525 100644 --- a/cmd/web_test.go +++ b/cmd/web_test.go @@ -72,9 +72,10 @@ func TestGetFilenameFromContent(t *testing.T) { expected string }{ {"Test Page", "http://example.com", "Test_Page.md"}, - {"No title here", "http://example.com/page", "http___example_com_page.md"}, + {"No title here", "http://example.com/page", "example_com_page.md"}, {" Trim Me ", "http://example.com", "Trim_Me.md"}, - {"", "http://example.com", "http___example_com.md"}, + {"", "http://example.com", "example_com.md"}, + {"Invalid URL", "not a valid url", "untitled.md"}, } for _, test := range tests {