Something went wrong. Try again.
📰 A single-page application to manage standard.site publication on the AT Protocol cuducos.tngl.io/std-pub
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145module Route exposing ( Route(..) , basePathFromAppUrl , getQueryParam , parseRoute , routeToPath , selectedFromRoute )
import Url
-- Routes
type Route = RouteHome | RouteNewPub | RoutePub String
{-| Routes live in the URL fragment (`#/pub/<rkey>`) so the static host servesthe same `index.html` for every deep link.-}parseRoute : Url.Url -> RouteparseRoute url = let fragment : String fragment = Maybe.withDefault "" url.fragment in case String.split "/" (trimSlashes fragment) of [ "" ] -> RouteHome
[ "pub", "new" ] -> RouteNewPub
[ "pub", rkey ] -> if String.isEmpty rkey then RouteHome
else RoutePub rkey
_ -> RouteHome
routeToPath : String -> Route -> StringrouteToPath basePath route = let prefix : String prefix = if String.endsWith "/" basePath then basePath
else basePath ++ "/" in case route of RouteHome -> prefix
RouteNewPub -> prefix ++ "#/pub/new"
RoutePub rkey -> prefix ++ "#/pub/" ++ rkey
basePathFromAppUrl : String -> StringbasePathFromAppUrl appUrl = case Url.fromString appUrl of Just u -> if String.endsWith "/" u.path then u.path
else u.path ++ "/"
Nothing -> "/"
selectedFromRoute : Route -> Maybe StringselectedFromRoute route = case route of RoutePub rkey -> Just rkey
RouteNewPub -> Just ""
_ -> Nothing
getQueryParam : String -> Url.Url -> Maybe StringgetQueryParam name url = case url.query of Nothing -> Nothing
Just q -> q |> String.split "&" |> List.filterMap (\pair -> case String.split "=" pair of [ k, v ] -> if k == name then Url.percentDecode v
else Nothing
_ -> Nothing ) |> List.head
-- Internals
trimSlashes : String -> StringtrimSlashes path = let withoutLeading : String withoutLeading = if String.startsWith "/" path then String.dropLeft 1 path
else path in if String.endsWith "/" withoutLeading then String.dropRight 1 withoutLeading
else withoutLeading