finished link and image, only need to handle lists and HTML

This commit is contained in:
Pagwin 2025-12-10 15:04:11 -05:00
parent 9330e44b58
commit bc1ea7e84b
No known key found for this signature in database
GPG key ID: 81137023740CA260

View file

@ -57,36 +57,46 @@ blockEnding :: (Logger m, Characters s) => Parser s m ()
blockEnding = lineEnding *> lineEnding blockEnding = lineEnding *> lineEnding
-- TODO: check if inlineHTML needs to be handled in any markdown posts -- TODO: check if inlineHTML needs to be handled in any markdown posts
-- TODO: link impl
-- TODO: image impl
inlineText :: (Logger m, Characters s) => Parser s m InlineText inlineText :: (Logger m, Characters s) => Parser s m InlineText
inlineText = choice [try strikethrough, try bold, try italic, try underlined, try code, try link, try image, plain_text] inlineText = inlineText' (fail "notFollowedBy noop")
where where
inlineText' disallow = choice [try $ strikethrough disallow, try $ bold disallow, try $ italic disallow, try $ underlined disallow, try code, try $ link disallow, try $ image disallow, plain_text disallow]
between' start end middle_piece = between start end $ many ((notFollowedBy end) *> middle_piece) between' start end middle_piece = between start end $ many ((notFollowedBy end) *> middle_piece)
strikethrough = Crossed <$> (between' (string "~~") (string "~~") inlineText) strikethrough disallow = Crossed <$> (between' (string "~~") (string "~~") (inlineText' (disallow <|> (void $ string "~~"))))
bold = Bold <$> (between' (string "**") (string "**") inlineText) bold disallow = Bold <$> (between' (string "**") (string "**") (inlineText' (disallow <|> (void $ string "**"))))
italic = Italic <$> (between' (char '*') (char '*') inlineText) italic disallow = Italic <$> (between' (char '*') (char '*') (inlineText' (disallow <|> (void $ char '*'))))
underlined = Underlined <$> (between' (string "__") (string "__") inlineText) underlined disallow = Underlined <$> (between' (string "__") (string "__") (inlineText' (disallow <|> (void $ string "__"))))
code = InlineCode . T.pack <$> (between' (char '`') (char '`') (notFollowedBy lineEnding *> anySingle)) code = InlineCode . T.pack <$> (between' (char '`') (char '`') (notFollowedBy lineEnding *> anySingle))
link = do link disallow = do
linkText <- error "linkText parser" linkText <- between' (char '[') (char ']') (inlineText' (disallow <|> (void $ char ']')))
url <- error "url parser" (url, title) <- do
title <- error "title parser" char '('
-- might fail on newline char situation
url <- T.pack <$> (many (notFollowedBy (char ')' <|> spaceChar) *> anySingle))
hasTitle <- optional spaceChar
title <- case hasTitle of
Just _ -> Just . T.pack <$> (many (notFollowedBy ((void $ char ')') <|> lineEnding) *> anySingle))
Nothing -> pure Nothing
char ')'
pure (url, title)
pure Link {linkText, url, title} pure Link {linkText, url, title}
image = do image disallow = do
altText <- error "altText" char '!'
url <- error "url" -- Is this a hack? Yes. Bite me
title <- error "title" link_hack <- link disallow
(altText, url, title) <- case link_hack of
Link {linkText = [Text altText], url, title} -> pure (altText, url, title)
_ -> fail "Image alt text must be normal text, cannot be stylized in any way"
pure Image {altText, url, title} pure Image {altText, url, title}
plain_text = Text . T.pack <$> (many ((notFollowedBy blockEnding) *> anySingle)) plain_text disallow = Text . T.pack <$> (many ((notFollowedBy (blockEnding <|> disallow)) *> anySingle))
headingBlock :: (Logger m, Characters s) => Parser s m Element headingBlock :: (Logger m, Characters s) => Parser s m Element
headingBlock = do headingBlock = do