mirror of
https://github.com/SphericalKat/medium.rip.git
synced 2026-10-21 01:34:22 +00:00
1 parent
665d33d9d3
commit
aacabdd54d
4 files changed
+63
-47
No files matched your search
@@ -0,0 +1,46 @@
|
||||
package converters
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
var r = regexp.MustCompile(`[\/\-]([0-9a-f]+)\/?$`)
|
||||
|
||||
func ConvertId(c *fiber.Ctx) string {
|
||||
path := c.Path()
|
||||
postId := idFromParams(c)
|
||||
if postId == "" {
|
||||
return idFromPath(path)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func idFromPath(path string) string {
|
||||
if strings.HasPrefix(path, "/tag/") {
|
||||
return ""
|
||||
}
|
||||
|
||||
matches := r.FindStringSubmatch(path)
|
||||
if len(matches) != 2 {
|
||||
return ""
|
||||
}
|
||||
|
||||
return matches[1]
|
||||
}
|
||||
|
||||
func idFromParams(c *fiber.Ctx) string {
|
||||
ru := c.Query("redirectUrl", "")
|
||||
if ru != "" {
|
||||
pu, err := url.Parse(ru)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return idFromPath(pu.Path)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package converters
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestConvertId(t *testing.T) {
|
||||
id := "@fareedkhandev/pandas-ai-the-future-of-data-analysis-8f0be9b5ab6f"
|
||||
convertedId := idFromPath(id)
|
||||
expected := "8f0be9b5ab6f"
|
||||
if convertedId != expected {
|
||||
t.Errorf("ConvertId(%s) = %s; want %s", id, convertedId, expected)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user