From 2632c44d0e9d7b27ae3cc329db5d22d4b9f5ad68 Mon Sep 17 00:00:00 2001 From: me Date: Mon, 4 Nov 2024 11:20:05 +0200 Subject: [PATCH] nicer dates in html --- app/Html.hs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/app/Html.hs b/app/Html.hs index e6037db..b42d9ae 100644 --- a/app/Html.hs +++ b/app/Html.hs @@ -6,6 +6,7 @@ import Data.String (fromString) import Data.Text qualified as T import Fedi qualified as Fedi import Lucid qualified as H +import Text.RawString.QQ (r) -- * HTML @@ -52,10 +53,12 @@ userHtml details = do notesHtml :: [Fedi.Note] -> Html notesHtml notes = do H.div_ [H.class_ "notes"] $ mapM_ noteHtml notes + H.script_ $ T.pack localDateJs -- | A single post as HTML. noteHtml :: Fedi.Note -> Html noteHtml note = do + let noteid = T.pack (maybe "" (\i -> i.unwrap) note.id) H.div_ [H.class_ "note"] $ do H.div_ [H.class_ "note-header"] $ do case note.name of @@ -68,12 +71,13 @@ noteHtml note = do H.p_ $ H.a_ [H.href_ (T.pack url)] $ fromString url Nothing -> pure () - H.a_ - [ H.href_ (T.pack (maybe "" (\i -> i.unwrap) note.id)) - , H.class_ "note-time" - , H.title_ "See note page" - ] - (H.toHtml (T.pack (show note.published))) + Fedi.for_ note.published \published -> + H.a_ + [ H.href_ noteid + , H.class_ "note-time" + , H.title_ "See note page" + ] + (H.span_ [H.class_ $ "note-date-published"] $ H.toHtml (show published)) H.div_ [H.class_ $ "note-content " <> checkDirection (maybe "" id note.content)] $ do H.toHtmlRaw (maybe "" id note.content) @@ -127,3 +131,13 @@ newNoteHtml details = do , 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(); +} +|]