summaryrefslogtreecommitdiffstats
path: root/src/MdBlocks/Resolver.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/MdBlocks/Resolver.hs')
-rw-r--r--src/MdBlocks/Resolver.hs55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/MdBlocks/Resolver.hs b/src/MdBlocks/Resolver.hs
new file mode 100644
index 0000000..c6e94d1
--- /dev/null
+++ b/src/MdBlocks/Resolver.hs
@@ -0,0 +1,55 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module MdBlocks.Resolver
+ ( resolveBlock
+ )
+where
+
+import Data.Text (Text)
+
+import MdBlocks.Address
+import MdBlocks.Types
+
+resolveBlock
+ :: Address
+ -> [Block]
+ -> Maybe Block
+
+resolveBlock (GlobalId ident) blocks =
+ findById ident blocks
+
+resolveBlock (FileAddress fp ident) blocks =
+ findByFileAndId fp ident blocks
+
+findById :: Text -> [Block] -> Maybe Block
+findById ident =
+ go
+ where
+ go [] =
+ Nothing
+
+ go (b:bs)
+ | mdId (blockMeta b) == Just ident =
+ Just b
+
+ | otherwise =
+ go bs
+
+findByFileAndId
+ :: FilePath
+ -> Text
+ -> [Block]
+ -> Maybe Block
+findByFileAndId fp ident =
+ go
+ where
+ go [] =
+ Nothing
+
+ go (b:bs)
+ | blockFile b == fp
+ , mdId (blockMeta b) == Just ident =
+ Just b
+
+ | otherwise =
+ go bs