nicer dates in html

This commit is contained in:
me 2024-11-04 11:20:05 +02:00
parent 1fde45736d
commit 2632c44d0e

View file

@ -6,6 +6,7 @@ import Data.String (fromString)
import Data.Text qualified as T import Data.Text qualified as T
import Fedi qualified as Fedi import Fedi qualified as Fedi
import Lucid qualified as H import Lucid qualified as H
import Text.RawString.QQ (r)
-- * HTML -- * HTML
@ -52,10 +53,12 @@ userHtml details = do
notesHtml :: [Fedi.Note] -> Html notesHtml :: [Fedi.Note] -> Html
notesHtml notes = do notesHtml notes = do
H.div_ [H.class_ "notes"] $ mapM_ noteHtml notes H.div_ [H.class_ "notes"] $ mapM_ noteHtml notes
H.script_ $ T.pack localDateJs
-- | A single post as HTML. -- | A single post as HTML.
noteHtml :: Fedi.Note -> Html noteHtml :: Fedi.Note -> Html
noteHtml note = do noteHtml note = do
let noteid = T.pack (maybe "" (\i -> i.unwrap) note.id)
H.div_ [H.class_ "note"] $ do H.div_ [H.class_ "note"] $ do
H.div_ [H.class_ "note-header"] $ do H.div_ [H.class_ "note-header"] $ do
case note.name of case note.name of
@ -68,12 +71,13 @@ noteHtml note = do
H.p_ $ H.a_ [H.href_ (T.pack url)] $ fromString url H.p_ $ H.a_ [H.href_ (T.pack url)] $ fromString url
Nothing -> pure () Nothing -> pure ()
H.a_ Fedi.for_ note.published \published ->
[ H.href_ (T.pack (maybe "" (\i -> i.unwrap) note.id)) H.a_
, H.class_ "note-time" [ H.href_ noteid
, H.title_ "See note page" , H.class_ "note-time"
] , H.title_ "See note page"
(H.toHtml (T.pack (show note.published))) ]
(H.span_ [H.class_ $ "note-date-published"] $ H.toHtml (show published))
H.div_ [H.class_ $ "note-content " <> checkDirection (maybe "" id note.content)] $ do H.div_ [H.class_ $ "note-content " <> checkDirection (maybe "" id note.content)] $ do
H.toHtmlRaw (maybe "" id note.content) H.toHtmlRaw (maybe "" id note.content)
@ -127,3 +131,13 @@ newNoteHtml details = do
, H.value_ "Post" , H.value_ "Post"
] ]
) )
localDateJs :: String
localDateJs = [r|
let collection = document.querySelectorAll(".note-date-published");
for (let i = 0; i < collection.length; i++) {
let date = new Date(collection[i].innerHTML);
collection[i].innerHTML = date.toLocaleString();
}
|]