diff --git a/cmd/web.go b/cmd/web.go
index 3d4b046..f3e93e5 100644
--- a/cmd/web.go
+++ b/cmd/web.go
@@ -211,17 +211,26 @@ func getFilenameFromContent(content, urlStr string) (string, error) {
titleStart := strings.Index(content, "
")
titleEnd := strings.Index(content, "")
if titleStart != -1 && titleEnd != -1 && titleEnd > titleStart {
- title := content[titleStart+7 : titleEnd]
- return sanitizeFilename(title) + ".rollup.md", nil
+ title := strings.TrimSpace(content[titleStart+7 : titleEnd])
+ if title != "" {
+ return sanitizeFilename(title) + ".rollup.md", nil
+ }
}
- // If no title found, use the URL without the protocol
+ // If no title found or title is empty, use the URL
parsedURL, err := url.Parse(urlStr)
if err != nil {
return "", fmt.Errorf("invalid URL: %v", err)
}
- filename := parsedURL.Host + parsedURL.Path
+ if parsedURL.Host == "" {
+ return "", fmt.Errorf("invalid URL: missing host")
+ }
+
+ filename := parsedURL.Host
+ if parsedURL.Path != "" && parsedURL.Path != "/" {
+ filename += strings.TrimSuffix(parsedURL.Path, "/")
+ }
return sanitizeFilename(filename) + ".rollup.md", nil
}
diff --git a/cmd/web_test.go b/cmd/web_test.go
index 739f320..8e470be 100644
--- a/cmd/web_test.go
+++ b/cmd/web_test.go
@@ -76,7 +76,9 @@ func TestGetFilenameFromContent(t *testing.T) {
{"No title here", "http://example.com/page", "example_com_page.rollup.md", false},
{" Trim Me ", "http://example.com", "Trim_Me.rollup.md", false},
{"", "http://example.com", "example_com.rollup.md", false},
+ {" ", "http://example.com", "example_com.rollup.md", false},
{"Invalid URL", "not a valid url", "", true},
+ {"No host", "http://", "", true},
}
for _, test := range tests {