Skip to content

Commit 013be93

Browse files
Set note date when the path begin with a timestamp (#552)
This change simplifies timeline creation by setting the date metadata from the filename when it begins with `YYYY-MM-DD-`.
1 parent 40607a2 commit 013be93

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

docs/guide/yaml-config.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Notice how this page's sidebar colorscheme has [changed to green]{.greenery}? Vi
2121

2222
- `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.
2323

24+
- `date`: The note timestamp. This is used to order note chronologically, such as for the timeline [[query|query]].
25+
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.
26+
In case of conflict, the date from the YAML configuration takes priority.
2427

2528
## Examples
2629

emanote/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- Lua filters: filter paths will now be looked up in all layers now.
2525
- Live server now uses Tailwind 3 ([\#503](https://github.com/srid/emanote/pull/503))
2626
- Enable auto identifier for org files ([\#502](https://github.com/srid/emanote/pull/502))
27+
- Support date metadata from the filename when it begins with YYYY-MM-DD ([\#552](https://github.com/srid/emanote/pull/552)).
2728
- Bug fixes:
2829
- Emanote no longer crashes when run on an empty directory ([\#487](https://github.com/srid/emanote/issues/487))
2930
- Stork search fixes

emanote/emanote.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 2.4
22
name: emanote
3-
version: 1.3.17.1
3+
version: 1.3.18.0
44
license: AGPL-3.0-only
55
copyright: 2022 Sridhar Ratnakumar
66
maintainer: [email protected]

emanote/src/Emanote/Model/Note.hs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Control.Monad.Logger (MonadLogger)
1010
import Control.Monad.Writer (MonadWriter (tell), WriterT, runWriterT)
1111
import Data.Aeson qualified as Aeson
1212
import Data.Aeson.Optics qualified as AO
13+
import Data.Char (isDigit)
1314
import Data.Default (Default (def))
1415
import Data.IxSet.Typed (Indexable (..), IxSet, ixFun, ixList)
1516
import Data.IxSet.Typed qualified as Ix
@@ -29,14 +30,15 @@ import Network.URI.Slug (Slug)
2930
import Optics.Core ((%), (.~))
3031
import Optics.TH (makeLenses)
3132
import Relude
32-
import System.FilePath ((</>))
33+
import System.FilePath (takeFileName, (</>))
3334
import Text.Pandoc (readerExtensions, runPure)
3435
import Text.Pandoc.Builder qualified as B
3536
import Text.Pandoc.Definition (Pandoc (..))
3637
import Text.Pandoc.Extensions
3738
import Text.Pandoc.Readers.Org (readOrg)
3839
import Text.Pandoc.Scripting (ScriptingEngine)
3940
import Text.Pandoc.Walk qualified as W
41+
import Text.Parsec qualified as P
4042
import UnliftIO.Directory (doesPathExist)
4143

4244
data Feed = Feed
@@ -316,7 +318,19 @@ parseNote scriptingEngine pluginBaseDir r src@(_, fp) s = do
316318
parseNoteMarkdown scriptingEngine pluginBaseDir fp s
317319
R.LMLRoute_Org _ -> do
318320
parseNoteOrg s
319-
pure $ mkNoteWith r (Just src) doc meta errs
321+
let metaWithDateFromPath = case P.parse dateParser mempty (takeFileName fp) of
322+
Left _ -> meta
323+
Right date -> SData.modifyAeson (pure "date") (Just . fromMaybe (Aeson.String date)) meta
324+
pure $ mkNoteWith r (Just src) doc metaWithDateFromPath errs
325+
where
326+
dateParser = do
327+
year <- replicateM 4 P.digit
328+
_ <- P.char '-'
329+
month <- replicateM 2 P.digit
330+
_ <- P.char '-'
331+
day <- replicateM 2 P.digit
332+
_ <- P.satisfy (not . isDigit)
333+
pure $ toText $ mconcat [year, "-", month, "-", day]
320334

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

0 commit comments

Comments
 (0)