Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/Data/Yaml/Frontmatter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module Data.Yaml.Frontmatter where

import Data.Attoparsec.ByteString
import Data.Frontmatter.Internal
import Data.Yaml (FromJSON, Value, decodeEither)
import Data.Yaml (FromJSON, Value, decodeEither', ParseException (InvalidYaml), YamlException (YamlParseException, yamlProblem, yamlProblemMark, yamlContext), YamlMark (yamlLine, yamlColumn))
import Data.Yaml.Aeson (prettyPrintParseException)

-- |
-- Parses a YAML frontmatter or JSON frontmatter from a 'ByteString' as a
Expand All @@ -14,6 +15,18 @@ frontmatterYaml = frontmatterYaml' <?> "frontmatterYaml"
where
frontmatterYaml' = do
f <- frontmatter
case decodeEither f of
Left e -> fail e
case decodeEither' f of
Left (InvalidYaml (Just (e0@YamlParseException { yamlProblem = msg, yamlContext = ctx, yamlProblemMark = pos }))) ->
let
fixedMark =
-- Column and line are both incremented, so they count from 1, as is usual...
pos {
-- ... and an extra increment for the --- marker
yamlLine = yamlLine pos + 2,
yamlColumn = yamlColumn pos + 1
}
e = InvalidYaml (Just e0 { yamlProblemMark = fixedMark })
in failYaml e
Left e -> failYaml e
Right v -> return v
failYaml = fail . prettyPrintParseException
5 changes: 4 additions & 1 deletion test/Data/FrontmatterSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@ spec = do

it "handles input with invalid frontmatters" $ do
input <- ByteString.readFile "./invalid-yaml-frontmatter.md"
let Fail input' _ _ = parse frontmatterYaml input :: Result Value
let Fail input' _ctx msg = parse frontmatterYaml input :: Result Value
input' `shouldBe` "etc etc\n"
-- ignoring _ctx. Does not appear useful.
-- A somewhat different message may be ok, but the line and column must be the same
msg `shouldBe` "Failed reading: YAML parse exception at line 2, column 11:\nmapping values are not allowed in this context"