mirror of
https://github.com/jgm/pandoc.git
synced 2026-09-24 15:44:59 +08:00
Org reader: fix treatment of links to images
Links with descriptions which are pointing to images are no longer read as inline images, but as proper links. Fixes: #5191
This commit is contained in:
@@ -432,21 +432,27 @@ explicitOrImageLink :: PandocMonad m => OrgParser m (F Inlines)
|
||||
explicitOrImageLink = try $ do
|
||||
char '['
|
||||
srcF <- applyCustomLinkFormat =<< possiblyEmptyLinkTarget
|
||||
title <- enclosedRaw (char '[') (char ']')
|
||||
title' <- parseFromString (mconcat <$> many inline) title
|
||||
descr <- enclosedRaw (char '[') (char ']')
|
||||
titleF <- parseFromString (mconcat <$> many inline) descr
|
||||
char ']'
|
||||
return $ do
|
||||
src <- srcF
|
||||
case cleanLinkString title of
|
||||
title <- titleF
|
||||
case cleanLinkString descr of
|
||||
Just imgSrc | isImageFilename imgSrc ->
|
||||
pure . B.link src "" $ B.image imgSrc mempty mempty
|
||||
return . B.link src "" $ B.image imgSrc mempty mempty
|
||||
_ ->
|
||||
linkToInlinesF src =<< title'
|
||||
linkToInlinesF src title
|
||||
|
||||
selflinkOrImage :: PandocMonad m => OrgParser m (F Inlines)
|
||||
selflinkOrImage = try $ do
|
||||
src <- char '[' *> linkTarget <* char ']'
|
||||
return $ linkToInlinesF src (B.str src)
|
||||
target <- char '[' *> linkTarget <* char ']'
|
||||
case cleanLinkString target of
|
||||
Nothing -> return $ internalLink target (B.str target)
|
||||
Just nonDocTgt -> returnF $
|
||||
if isImageFilename nonDocTgt
|
||||
then B.image nonDocTgt "" ""
|
||||
else B.link nonDocTgt "" (B.str target)
|
||||
|
||||
plainLink :: PandocMonad m => OrgParser m (F Inlines)
|
||||
plainLink = try $ do
|
||||
@@ -481,10 +487,8 @@ linkToInlinesF linkStr =
|
||||
"" -> pure . B.link mempty "" -- wiki link (empty by convention)
|
||||
('#':_) -> pure . B.link linkStr "" -- document-local fraction
|
||||
_ -> case cleanLinkString linkStr of
|
||||
(Just cleanedLink) -> if isImageFilename cleanedLink
|
||||
then const . pure $ B.image cleanedLink "" ""
|
||||
else pure . B.link cleanedLink ""
|
||||
Nothing -> internalLink linkStr -- other internal link
|
||||
Just extTgt -> return . B.link extTgt ""
|
||||
Nothing -> internalLink linkStr -- other internal link
|
||||
|
||||
internalLink :: String -> Inlines -> F Inlines
|
||||
internalLink link title = do
|
||||
|
||||
@@ -61,8 +61,7 @@ cleanLinkString s =
|
||||
'.':'.':'/':_ -> Just s -- relative path
|
||||
-- Relative path or URL (file schema)
|
||||
'f':'i':'l':'e':':':s' -> Just $ if "//" `isPrefixOf` s' then s else s'
|
||||
_ | isUrl s -> Just s -- URL
|
||||
_ -> Nothing
|
||||
_ -> if isUrl s then Just s else Nothing
|
||||
where
|
||||
isUrl :: String -> Bool
|
||||
isUrl cs =
|
||||
|
||||
@@ -184,26 +184,26 @@ tests =
|
||||
|
||||
, testGroup "Images"
|
||||
[ "Image" =:
|
||||
"[[./sunset.jpg]]" =?>
|
||||
para (image "./sunset.jpg" "" "")
|
||||
"[[./sunset.jpg]]" =?>
|
||||
para (image "./sunset.jpg" "" "")
|
||||
|
||||
, "Image with explicit file: prefix" =:
|
||||
"[[file:sunrise.jpg]]" =?>
|
||||
para (image "sunrise.jpg" "" "")
|
||||
"[[file:sunrise.jpg]]" =?>
|
||||
para (image "sunrise.jpg" "" "")
|
||||
|
||||
, "Multiple images within a paragraph" =:
|
||||
T.unlines [ "[[file:sunrise.jpg]]"
|
||||
, "[[file:sunset.jpg]]"
|
||||
] =?>
|
||||
para ((image "sunrise.jpg" "" "")
|
||||
T.unlines [ "[[file:sunrise.jpg]]"
|
||||
, "[[file:sunset.jpg]]"
|
||||
] =?>
|
||||
para (image "sunrise.jpg" "" ""
|
||||
<> softbreak
|
||||
<> (image "sunset.jpg" "" ""))
|
||||
<> image "sunset.jpg" "" "")
|
||||
|
||||
, "Image with html attributes" =:
|
||||
T.unlines [ "#+ATTR_HTML: :width 50%"
|
||||
, "[[file:guinea-pig.gif]]"
|
||||
] =?>
|
||||
para (imageWith ("", [], [("width", "50%")]) "guinea-pig.gif" "" "")
|
||||
T.unlines [ "#+ATTR_HTML: :width 50%"
|
||||
, "[[file:guinea-pig.gif]]"
|
||||
] =?>
|
||||
para (imageWith ("", [], [("width", "50%")]) "guinea-pig.gif" "" "")
|
||||
]
|
||||
|
||||
, "Explicit link" =:
|
||||
@@ -235,6 +235,10 @@ tests =
|
||||
"[[http://example.com][./logo.png]]" =?>
|
||||
para (link "http://example.com" "" (image "./logo.png" "" ""))
|
||||
|
||||
, "Link to image" =:
|
||||
"[[https://example.com/image.jpg][Look!]]" =?>
|
||||
para (link "https://example.com/image.jpg" "" (str "Look!"))
|
||||
|
||||
, "Plain link" =:
|
||||
"Posts on http://zeitlens.com/ can be funny at times." =?>
|
||||
para (spcSep [ "Posts", "on"
|
||||
|
||||
Reference in New Issue
Block a user