From 27e1dea57dde45bcc9e42e80d1f178075815e03d Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Mon, 14 Sep 2020 17:27:14 -0400 Subject: [PATCH] Allow dot in Zettel ID; resolves #369 --- CHANGELOG.md | 1 + guide/2011403.md | 1 + neuron/neuron.cabal | 2 +- neuron/src/lib/Neuron/Zettelkasten/ID.hs | 2 +- neuron/src/lib/Neuron/Zettelkasten/Query/Parser.hs | 10 +++++----- neuron/test/Neuron/Zettelkasten/IDSpec.hs | 6 +++++- 6 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7fa3cc00..31b2f267d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ - Inline tags (#189) - support for [fancy lists](https://github.com/jgm/commonmark-hs/blob/master/commonmark-extensions/test/fancy_lists.md) (#335) - Fix hard line breaks to actually work (#354) + - Allow dot in Zettel ID (#369) - CLI - Faster querying: add `--cached` option to `neuron query`, to run faster using the cache. To keep the cache up to date, make sure that `neuron rib` is running. - Add `--id` and `--search` options to `open` command to open given zettel ID or search page respectively (#317) diff --git a/guide/2011403.md b/guide/2011403.md index bef10fe86..7779b2728 100644 --- a/guide/2011403.md +++ b/guide/2011403.md @@ -8,6 +8,7 @@ specify your own as well, as long as it contains only the following characters: * digits * hyphen (`-`) * underscore (`_`) +* dot (`.`) ## Why prefer random IDs? diff --git a/neuron/neuron.cabal b/neuron/neuron.cabal index b947ffedf..f59703660 100644 --- a/neuron/neuron.cabal +++ b/neuron/neuron.cabal @@ -1,7 +1,7 @@ cabal-version: 2.4 name: neuron -- This version must be in sync with what's in Default.dhall -version: 0.6.9.0 +version: 0.6.9.1 license: AGPL-3.0-only copyright: 2020 Sridhar Ratnakumar maintainer: srid@srid.ca diff --git a/neuron/src/lib/Neuron/Zettelkasten/ID.hs b/neuron/src/lib/Neuron/Zettelkasten/ID.hs index 470ecd7f6..86e6b323b 100644 --- a/neuron/src/lib/Neuron/Zettelkasten/ID.hs +++ b/neuron/src/lib/Neuron/Zettelkasten/ID.hs @@ -120,7 +120,7 @@ dayParser = do customIDParser :: Parser Text customIDParser = do - fmap toText $ M.some $ M.alphaNumChar <|> M.char '_' <|> M.char '-' + fmap toText $ M.some $ M.alphaNumChar <|> M.char '_' <|> M.char '-' <|> M.char '.' -- | Parse the ZettelID if the given filepath is a zettel. getZettelID :: ZettelFormat -> FilePath -> Maybe ZettelID diff --git a/neuron/src/lib/Neuron/Zettelkasten/Query/Parser.hs b/neuron/src/lib/Neuron/Zettelkasten/Query/Parser.hs index 867f6b5cd..19d45fe58 100644 --- a/neuron/src/lib/Neuron/Zettelkasten/Query/Parser.hs +++ b/neuron/src/lib/Neuron/Zettelkasten/Query/Parser.hs @@ -67,11 +67,11 @@ queryFromURI defConn uri = do (URI.unRText -> path) :| [] <- hoistMaybe shortLinkPath zid <- hoistMaybe $ - -- Allow direct use of ID - rightToMaybe (parseZettelID path) - -- Also, allow raw filename (ending with ".md"). HACK: hardcoding - -- format, but we shouldn't. - <|> getZettelID ZettelFormat_Markdown (toString path) + -- Allow raw filename (ending with ".md"). HACK: hardcoding + -- format, but we shouldn't. + getZettelID ZettelFormat_Markdown (toString path) + -- Before checking for direct use of ID + <|> rightToMaybe (parseZettelID path) pure $ Some $ ZettelQuery_ZettelByID zid conn Just (URI.unRText -> proto) -> do guard $ proto == "z" diff --git a/neuron/test/Neuron/Zettelkasten/IDSpec.hs b/neuron/test/Neuron/Zettelkasten/IDSpec.hs index b4fab2d8f..05670334c 100644 --- a/neuron/test/Neuron/Zettelkasten/IDSpec.hs +++ b/neuron/test/Neuron/Zettelkasten/IDSpec.hs @@ -34,10 +34,14 @@ spec = do let deceptiveZid = Z.ZettelCustomID "2136537e" it "parses a custom zettel ID that looks like date ID" $ do Z.parseZettelID "2136537e" `shouldBe` Right deceptiveZid + it "parses a custom zettel ID with dot" $ do + Z.parseZettelID "foo.bar" `shouldBe` Right (Z.ZettelCustomID "foo.bar") + -- Even if there is a ".md" (not a file extension) + Z.parseZettelID "foo.md" `shouldBe` Right (Z.ZettelCustomID "foo.md") context "failures" $ do it "fails to parse ID with disallowed characters" $ do Z.parseZettelID "/foo" `shouldSatisfy` isLeft - Z.parseZettelID "foo." `shouldSatisfy` isLeft + Z.parseZettelID "foo$" `shouldSatisfy` isLeft Z.parseZettelID "foo bar" `shouldSatisfy` isLeft describe "ID converstion" $ do context "JSON encoding" $ do