mirror of
https://github.com/srid/ema.git
synced 2024-11-25 20:12:20 +03:00
Unicode normalize routeUrl
(via decodeSlug
)
This commit is contained in:
parent
ed782ef1a9
commit
df58a62139
@ -6,6 +6,7 @@
|
|||||||
- Add `Ord`, `Generic`, `Data` and Aeson instances to `Slug`
|
- Add `Ord`, `Generic`, `Data` and Aeson instances to `Slug`
|
||||||
- Unicode normalize slugs using NFC
|
- Unicode normalize slugs using NFC
|
||||||
- TODO(doc) Add `decodeSlug` and `encodeSlug`
|
- TODO(doc) Add `decodeSlug` and `encodeSlug`
|
||||||
|
- Unicode normalize `routeUrl` (via `decodeSlug`)
|
||||||
- Add default implementation based on Enum for `staticRoute`
|
- Add default implementation based on Enum for `staticRoute`
|
||||||
- Warn, without failing, on missing `staticAssets` during static generation
|
- Warn, without failing, on missing `staticAssets` during static generation
|
||||||
- Helpers
|
- Helpers
|
||||||
|
@ -4,8 +4,9 @@
|
|||||||
module Ema.Route.UrlStrategy where
|
module Ema.Route.UrlStrategy where
|
||||||
|
|
||||||
import Data.Default (Default, def)
|
import Data.Default (Default, def)
|
||||||
|
import qualified Data.List.NonEmpty as NE
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Ema.Route.Slug (Slug (unSlug))
|
import Ema.Route.Slug (Slug (unSlug), decodeSlug, encodeSlug)
|
||||||
import System.FilePath (joinPath)
|
import System.FilePath (joinPath)
|
||||||
|
|
||||||
data UrlStrategy
|
data UrlStrategy
|
||||||
@ -22,19 +23,19 @@ slugUrlWithStrategy :: UrlStrategy -> [Slug] -> Text
|
|||||||
slugUrlWithStrategy strat slugs =
|
slugUrlWithStrategy strat slugs =
|
||||||
case strat of
|
case strat of
|
||||||
UrlStrategy_FolderOnly ->
|
UrlStrategy_FolderOnly ->
|
||||||
"/" <> T.replace "index.html" "" (toText $ slugFileWithStrategy strat slugs)
|
"/" <> T.intercalate "/" (encodeSlug <$> slugs)
|
||||||
UrlStrategy_HtmlOnlySansExt ->
|
UrlStrategy_HtmlOnlySansExt ->
|
||||||
-- FIXME: This should replace only at the end, not middle
|
case nonEmpty slugs of
|
||||||
let fp = toText (slugFileWithStrategy strat slugs)
|
Nothing ->
|
||||||
in if
|
"/"
|
||||||
| "index.html" == fp ->
|
Just (removeLastIf (decodeSlug "index") -> slugsWithoutIndex) ->
|
||||||
"/"
|
"/" <> T.intercalate "/" (encodeSlug <$> slugsWithoutIndex)
|
||||||
| "/index.html" `T.isSuffixOf` fp ->
|
where
|
||||||
"/" <> T.take (T.length fp - T.length "/index.html") fp
|
removeLastIf :: Eq a => a -> NonEmpty a -> [a]
|
||||||
| ".html" `T.isSuffixOf` fp ->
|
removeLastIf x xs =
|
||||||
"/" <> T.take (T.length fp - T.length ".html") fp
|
if NE.last xs == x
|
||||||
| otherwise ->
|
then NE.init xs
|
||||||
"/" <> fp
|
else toList xs
|
||||||
|
|
||||||
slugFileWithStrategy :: UrlStrategy -> [Slug] -> FilePath
|
slugFileWithStrategy :: UrlStrategy -> [Slug] -> FilePath
|
||||||
slugFileWithStrategy strat slugs =
|
slugFileWithStrategy strat slugs =
|
||||||
@ -42,5 +43,5 @@ slugFileWithStrategy strat slugs =
|
|||||||
UrlStrategy_FolderOnly ->
|
UrlStrategy_FolderOnly ->
|
||||||
joinPath $ fmap (toString . unSlug) slugs <> ["index.html"]
|
joinPath $ fmap (toString . unSlug) slugs <> ["index.html"]
|
||||||
UrlStrategy_HtmlOnlySansExt ->
|
UrlStrategy_HtmlOnlySansExt ->
|
||||||
let (term :| (reverse -> parts)) = fromMaybe ("index" :| []) $ nonEmpty (reverse $ fmap unSlug slugs)
|
let (term :| (reverse -> parts)) = fromMaybe ("index" :| []) $ nonEmpty (reverse $ fmap (toString . unSlug) slugs)
|
||||||
in joinPath $ fmap toString parts <> [toString term <> ".html"]
|
in joinPath $ parts <> [term <> ".html"]
|
||||||
|
Loading…
Reference in New Issue
Block a user