Skip to content

Commit

Permalink
Support for "data" meta field
Browse files Browse the repository at this point in the history
Ref #151
  • Loading branch information
srid committed Apr 30, 2020
1 parent 4e24724 commit 28df3d0
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 17 deletions.
21 changes: 20 additions & 1 deletion guide/2011505.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
title: Zettel metadata
---

You zettels can contain custom metadata. Neuron supports "tags" as builtin. For example:
Every zettel must contain a "title" field in its metadata. Option metadata include "tags" and "date".

## Tags

You can attach one or more tags to your zettels:

```markdown
---
Expand All @@ -13,3 +17,18 @@ tags:
```

Tags can also be nested using a "tag/subtag" syntax, to allow a more fine-grained organization of your Zettelkasten, especially when using advanced queries as shown in [2011506](zcf://linking-to-multiple-zettels).

## Date

If you are using custom ID, the creation date of the zettels can be specified in the "date" metadata field:

```markdown
---
title: Ate steak for birthday
date: 2020-04-07
tags:
- journal
---
```

The creation date can be made to display in a zquery result by using the `linkTheme=withDate` argument (see [2011506](zcf://linking-to-multiple-zettels)).
7 changes: 0 additions & 7 deletions src/Neuron/Zettelkasten/ID.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
module Neuron.Zettelkasten.ID
( ZettelID (..),
InvalidID (..),
zettelIDDay,
zettelIDText,
parseZettelID,
parseZettelID',
Expand Down Expand Up @@ -77,12 +76,6 @@ formatDay day =
zettelIDSourceFileName :: ZettelID -> FilePath
zettelIDSourceFileName zid = toString $ zettelIDText zid <> ".md"

zettelIDDay :: ZettelID -> Maybe Day
zettelIDDay = \case
ZettelCustomID _ -> Nothing
ZettelDateID day _ ->
Just day

zettelPath :: ZettelID -> Action FilePath
zettelPath zid = do
notesDir <- Rib.ribInputDir
Expand Down
2 changes: 1 addition & 1 deletion src/Neuron/Zettelkasten/Query/View.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ renderZettelLink LinkView {..} Zettel {..} = do
let zurl = Rib.routeUrlRel $ Route_Zettel zettelID
mextra =
if linkViewShowDate
then case zettelIDDay zettelID of
then case zettelDay of
Just day ->
Just $ toHtml $ show @Text day
Nothing ->
Expand Down
12 changes: 10 additions & 2 deletions src/Neuron/Zettelkasten/Zettel.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Neuron.Zettelkasten.Zettel where
import Data.Aeson
import Data.Graph.Labelled (Vertex (..))
import Data.TagTree (Tag)
import Data.Time.Calendar
import Development.Shake (Action)
import Neuron.Zettelkasten.ID
import qualified Neuron.Zettelkasten.Zettel.Meta as Meta
Expand All @@ -22,6 +23,7 @@ data Zettel = Zettel
{ zettelID :: ZettelID,
zettelTitle :: Text,
zettelTags :: [Tag],
zettelDay :: Maybe Day,
zettelContent :: MMark
}

Expand All @@ -46,7 +48,8 @@ zettelJson :: KeyValue a => Zettel -> [a]
zettelJson Zettel {..} =
[ "id" .= toJSON zettelID,
"title" .= zettelTitle,
"tags" .= zettelTags
"tags" .= zettelTags,
"day" .= zettelDay
]

-- | Load a zettel from a file.
Expand All @@ -59,4 +62,9 @@ mkZettelFromPath path = do
meta = Meta.getMeta doc
title = maybe (toText $ "No title for " <> path) Meta.title meta
tags = fromMaybe [] $ Meta.tags =<< meta
pure $ Zettel zid title tags doc
day = case zid of
-- We ignore the "data" meta field on legacy Date IDs, which encode the
-- creation date in the ID.
ZettelDateID v _ -> Just v
ZettelCustomID _ -> Meta.date =<< meta
pure $ Zettel zid title tags day doc
5 changes: 4 additions & 1 deletion src/Neuron/Zettelkasten/Zettel/Meta.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ where

import Data.Aeson
import Data.TagTree (Tag)
import Data.Time.Calendar
import Relude
import Text.MMark (MMark, projectYaml)

-- | YAML metadata in a zettel markdown file
data Meta = Meta
{ title :: Text,
tags :: Maybe [Tag]
tags :: Maybe [Tag],
-- | Creation day
date :: Maybe Day
}
deriving (Eq, Show, Generic, FromJSON)

Expand Down
12 changes: 7 additions & 5 deletions test/Neuron/Zettelkasten/ZettelSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ spec = do
describe "sortZettelsReverseChronological" $ do
let mkDay = fromGregorian 2020 3
dummyContent = either error id $ parsePure "<spec>" "Dummy"
mkZettel day idx = Zettel (ZettelDateID (mkDay day) idx) "Some title" [Tag "science", Tag "journal/class"] dummyContent
mkZettel day idx =
Zettel (ZettelDateID (mkDay day) idx) "Some title" [Tag "science", Tag "journal/class"] Nothing dummyContent
it "sorts correctly" $ do
let zs = [mkZettel 3 2, mkZettel 5 1]
sortZettelsReverseChronological zs
Expand All @@ -32,14 +33,15 @@ spec = do
`shouldBe` [mkZettel 7 4, mkZettel 7 2]
describe "Zettel JSON" $ do
let day = fromGregorian 2020 3 19
zid = ZettelDateID day 1
zid = ZettelCustomID "Foo-Bar"
dummyContent = either error id $ parsePure "<spec>" "Dummy"
zettel = Zettel zid "Some title" [Tag "science", Tag "journal/class"] dummyContent
zettel = Zettel zid "Some title" [Tag "science", Tag "journal/class"] (Just day) dummyContent
it "Produces expected json" $ do
-- "{\"id\":\"2011401\",\"title\":\"Some title\",\"tags\":[\"science\"]}"
object (zettelJson zettel)
`shouldBe` object
[ "id" .= ("2011401" :: Text),
[ "id" .= ("Foo-Bar" :: Text),
"title" .= ("Some title" :: Text),
"tags" .= (["science", "journal/class"] :: [Text])
"tags" .= (["science", "journal/class"] :: [Text]),
"day" .= day
]

0 comments on commit 28df3d0

Please sign in to comment.