Org reader: recognize absolute paths on Windows

Fixes: #8201
This commit is contained in:
Albert Krewinkel 2022-08-02 23:16:47 +02:00
parent 4633fc3ca0
commit 516c827d61
No known key found for this signature in database
GPG key ID: 388DC0B21F631124
2 changed files with 19 additions and 3 deletions

View file

@ -20,6 +20,8 @@ import Data.Char (isAlphaNum)
import Data.Text (Text)
import qualified Data.Text as T
import System.FilePath (isValid, takeExtension)
import qualified System.FilePath.Posix as Posix
import qualified System.FilePath.Windows as Windows
import Text.Pandoc.Shared (elemText)
-- | Check whether the given string looks like the path to of URL of an image.
@ -37,13 +39,21 @@ isImageFilename fp = hasImageExtension && (isValid (T.unpack fp) || isKnownProto
-- the string does not appear to be a link.
cleanLinkText :: Text -> Maybe Text
cleanLinkText s
| Just _ <- T.stripPrefix "/" s = Just $ "file://" <> s -- absolute path
| isAbsolute s = Just $ "file://" <> s -- absolute path
| Just _ <- T.stripPrefix "./" s = Just s -- relative path
| Just _ <- T.stripPrefix "../" s = Just s -- relative path
-- Relative path or URL (file schema)
| Just s' <- T.stripPrefix "file:" s = Just $ if "//" `T.isPrefixOf` s' then s else s'
| otherwise = if isUrl s then Just s else Nothing
| Just s' <- T.stripPrefix "file:" s = Just $
if "//" `T.isPrefixOf` s'
then s
else if isAbsolute s'
then "file://" <> s'
else s'
| isUrl s = Just s
| otherwise = Nothing
where
isAbsolute :: Text -> Bool
isAbsolute = ((||) <$> Posix.isAbsolute <*> Windows.isAbsolute) . T.unpack
isUrl :: Text -> Bool
isUrl cs =
let (scheme, path) = T.break (== ':') cs

6
test/command/8201.md Normal file
View file

@ -0,0 +1,6 @@
```
% pandoc -f org -t html
[[file:d:/Home/Documents/test.png][Link Test]]
^D
<p><a href="file://d:/Home/Documents/test.png">Link Test</a></p>
```