Skip to content

Commit

Permalink
Set note date when the path begin with a timestamp (#552)
Browse files Browse the repository at this point in the history
This change simplifies timeline creation by setting the date metadata
from the filename when it begins with `YYYY-MM-DD-`.
  • Loading branch information
TristanCacqueray authored Sep 30, 2024
1 parent 40607a2 commit 013be93
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/guide/yaml-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Notice how this page's sidebar colorscheme has [changed to green]{.greenery}? Vi

- `page.image`: The image to use for the page. This is used for the [[ogp]] meta tag `og:image` meta tag. If not specified, the first image in the page is used. Relative URLs are automatically rewritten to absolute URLs if `page.siteUrl` is non-empty.

- `date`: The note timestamp. This is used to order note chronologically, such as for the timeline [[query|query]].
The value can be set from the filename if it begins with `YYYY-MM-DD`, which is useful for including the date in the note URL.
In case of conflict, the date from the YAML configuration takes priority.

## Examples

Expand Down
1 change: 1 addition & 0 deletions emanote/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Lua filters: filter paths will now be looked up in all layers now.
- Live server now uses Tailwind 3 ([\#503](https://github.com/srid/emanote/pull/503))
- Enable auto identifier for org files ([\#502](https://github.com/srid/emanote/pull/502))
- Support date metadata from the filename when it begins with YYYY-MM-DD ([\#552](https://github.com/srid/emanote/pull/552)).
- Bug fixes:
- Emanote no longer crashes when run on an empty directory ([\#487](https://github.com/srid/emanote/issues/487))
- Stork search fixes
Expand Down
2 changes: 1 addition & 1 deletion emanote/emanote.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.4
name: emanote
version: 1.3.17.1
version: 1.3.18.0
license: AGPL-3.0-only
copyright: 2022 Sridhar Ratnakumar
maintainer: [email protected]
Expand Down
18 changes: 16 additions & 2 deletions emanote/src/Emanote/Model/Note.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Control.Monad.Logger (MonadLogger)
import Control.Monad.Writer (MonadWriter (tell), WriterT, runWriterT)
import Data.Aeson qualified as Aeson
import Data.Aeson.Optics qualified as AO
import Data.Char (isDigit)
import Data.Default (Default (def))
import Data.IxSet.Typed (Indexable (..), IxSet, ixFun, ixList)
import Data.IxSet.Typed qualified as Ix
Expand All @@ -29,14 +30,15 @@ import Network.URI.Slug (Slug)
import Optics.Core ((%), (.~))
import Optics.TH (makeLenses)
import Relude
import System.FilePath ((</>))
import System.FilePath (takeFileName, (</>))
import Text.Pandoc (readerExtensions, runPure)
import Text.Pandoc.Builder qualified as B
import Text.Pandoc.Definition (Pandoc (..))
import Text.Pandoc.Extensions
import Text.Pandoc.Readers.Org (readOrg)
import Text.Pandoc.Scripting (ScriptingEngine)
import Text.Pandoc.Walk qualified as W
import Text.Parsec qualified as P
import UnliftIO.Directory (doesPathExist)

data Feed = Feed
Expand Down Expand Up @@ -316,7 +318,19 @@ parseNote scriptingEngine pluginBaseDir r src@(_, fp) s = do
parseNoteMarkdown scriptingEngine pluginBaseDir fp s
R.LMLRoute_Org _ -> do
parseNoteOrg s
pure $ mkNoteWith r (Just src) doc meta errs
let metaWithDateFromPath = case P.parse dateParser mempty (takeFileName fp) of
Left _ -> meta
Right date -> SData.modifyAeson (pure "date") (Just . fromMaybe (Aeson.String date)) meta
pure $ mkNoteWith r (Just src) doc metaWithDateFromPath errs
where
dateParser = do
year <- replicateM 4 P.digit
_ <- P.char '-'
month <- replicateM 2 P.digit
_ <- P.char '-'
day <- replicateM 2 P.digit
_ <- P.satisfy (not . isDigit)
pure $ toText $ mconcat [year, "-", month, "-", day]

parseNoteOrg :: (MonadWriter [Text] m) => Text -> m (Pandoc, Aeson.Value)
parseNoteOrg s =
Expand Down

0 comments on commit 013be93

Please sign in to comment.