loadPosts :: FilePath -> IO (Vector Post)
loadPosts dir =
     fmap (sortBy (\x y -> postTime y `compare` postTime x))
   $ runConduitRes
   $ sourceDirectory dir
  .| concatMapC (stripSuffix ".md")
  .| mapMC loadPost
  .| sinkVector
  where
    loadPost :: FilePath -> ResourceT IO Post
    loadPost noExt = handleAny (\e -> throwString $ "Could not parse " ++ noExt ++ ".md: " ++ show e) $ do
      bs <- readFile $ noExt ++ ".md"
      let slug = pack $ takeFileName noExt
          text = filter (/= '\r') $ decodeUtf8 bs
      (frontmatter, body) <-
        case lines text of
          "---":rest ->
            case break (== "---") rest of
              (frontmatter, "---":body) -> return (unlines frontmatter, unlines body)
              _ -> error "Missing closing --- on frontmatter"
          _ -> error "Does not start with --- frontmatter"
      case Data.Yaml.decodeEither' $ encodeUtf8 frontmatter of
        Left e -> throwIO e
        Right mkPost -> return $ mkPost slug $ preEscapedToHtml $ commonmarkToHtml
          [optSmart]
          [extTable, extAutolink]
          body
loadPosts :: FilePath -> IO (Vector Post)
loadPosts dir =
     fmap (sortBy (\x y -> postTime y `compare` postTime x))
   $ runConduitRes
   $ sourceDirectory dir
  .| concatMapC (stripSuffix ".md")
  .| mapMC loadPost
  .| sinkVector
  where
    loadPost :: FilePath -> ResourceT IO Post
    loadPost noExt = handleAny (\e -> throwString $ "Could not parse " ++ noExt ++ ".md: " ++ show e) $ do
      bs <- readFile $ noExt ++ ".md"
      let slug = pack $ takeFileName noExt
          text = filter (/= '\r') $ decodeUtf8 bs
      (frontmatter, body) <-
        case lines text of
          "---":rest ->
            case break (== "---") rest of
              (frontmatter, "---":body) -> return (unlines frontmatter, unlines body)
              _ -> error "Missing closing --- on frontmatter"
          _ -> error "Does not start with --- frontmatter"
      case Data.Yaml.decodeEither' $ encodeUtf8 frontmatter of
        Left e -> throwIO e
        Right mkPost -> return $ mkPost slug $ preEscapedToHtml $ commonmarkToHtml
          [optSmart]
          [extTable, extAutolink]
          body
loadPosts dir =
     fmap (sortBy (\x y -> postTime y `compare` postTime x))
   $ runConduitRes
   $ sourceDirectory dir
  .| concatMapC (stripSuffix ".md")
  .| mapMC loadPost
  .| sinkVector
  where
    loadPost :: FilePath -> ResourceT IO Post
    loadPost noExt = handleAny (\e -> throwString $ "Could not parse " ++ noExt ++ ".md: " ++ show e) $ do
      bs <- readFile $ noExt ++ ".md"
      let slug = pack $ takeFileName noExt
          text = filter (/= '\r') $ decodeUtf8 bs
      (frontmatter, body) <-
        case lines text of
          "---":rest ->
            case break (== "---") rest of
              (frontmatter, "---":body) -> return (unlines frontmatter, unlines body)
              _ -> error "Missing closing --- on frontmatter"
          _ -> error "Does not start with --- frontmatter"
      case Data.Yaml.decodeEither' $ encodeUtf8 frontmatter of
        Left e -> throwIO e
        Right mkPost -> return $ mkPost slug $ preEscapedToHtml $ commonmarkToHtml
          [optSmart]
          [extTable, extAutolink]
          body