mirror of
https://github.com/tnypxl/rollup.git
synced 2025-12-15 15:03:17 +00:00
refactor: update getFilenameFromContent to remove http from filenames
This commit is contained in:
12
cmd/web.go
12
cmd/web.go
@@ -203,7 +203,7 @@ func extractAndConvertContent(urlStr string) (string, error) {
|
|||||||
return header + markdown + "\n\n", nil
|
return header + markdown + "\n\n", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFilenameFromContent(content, url string) string {
|
func getFilenameFromContent(content, urlStr string) string {
|
||||||
// Try to extract title from content
|
// Try to extract title from content
|
||||||
titleStart := strings.Index(content, "<title>")
|
titleStart := strings.Index(content, "<title>")
|
||||||
titleEnd := strings.Index(content, "</title>")
|
titleEnd := strings.Index(content, "</title>")
|
||||||
@@ -212,8 +212,14 @@ func getFilenameFromContent(content, url string) string {
|
|||||||
return sanitizeFilename(title) + ".md"
|
return sanitizeFilename(title) + ".md"
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no title found, use the URL
|
// If no title found, use the URL without the protocol
|
||||||
return sanitizeFilename(url) + ".md"
|
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 {
|
func sanitizeFilename(name string) string {
|
||||||
|
|||||||
@@ -72,9 +72,10 @@ func TestGetFilenameFromContent(t *testing.T) {
|
|||||||
expected string
|
expected string
|
||||||
}{
|
}{
|
||||||
{"<title>Test Page</title>", "http://example.com", "Test_Page.md"},
|
{"<title>Test Page</title>", "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"},
|
||||||
{"<title> Trim Me </title>", "http://example.com", "Trim_Me.md"},
|
{"<title> Trim Me </title>", "http://example.com", "Trim_Me.md"},
|
||||||
{"<title></title>", "http://example.com", "http___example_com.md"},
|
{"<title></title>", "http://example.com", "example_com.md"},
|
||||||
|
{"Invalid URL", "not a valid url", "untitled.md"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
|||||||
Reference in New Issue
Block a user