Skip to content

Commit f3fff87

Browse files
committed
EPUB reader: fix links to other files in the epub...
making them internal links to a fragment derived from the filename. There was already code to handle links like `#foo`, but not to handle links like `ch0001.html#foo`. Closes #10207.
1 parent f4fe61a commit f3fff87

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/Text/Pandoc/Readers/EPUB.hs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import qualified Data.Map as M (Map, elems, fromList, lookup)
2929
import Data.Maybe (mapMaybe)
3030
import qualified Data.Text.Lazy as TL
3131
import qualified Data.Text.Lazy.Encoding as TL
32-
import Network.URI (unEscapeString)
32+
import Network.URI (unEscapeString, parseRelativeReference, URI(..))
3333
import System.FilePath (dropFileName, dropFileName, normalise, splitFileName,
3434
takeFileName, (</>))
3535
import qualified Text.Pandoc.Builder as B
@@ -211,10 +211,17 @@ fixInlineIRs s (Span as v) =
211211
Span (fixAttrs s as) v
212212
fixInlineIRs s (Code as code) =
213213
Code (fixAttrs s as) code
214-
fixInlineIRs s (Link as is (T.uncons -> Just ('#', url), tit)) =
215-
Link (fixAttrs s as) is (addHash s url, tit)
216-
fixInlineIRs s (Link as is t) =
217-
Link (fixAttrs s as) is t
214+
fixInlineIRs s (Link as is (url, tit)) =
215+
case parseRelativeReference (T.unpack url) of
216+
Just URI{ uriScheme = ""
217+
, uriAuthority = Nothing
218+
, uriPath = upath
219+
, uriQuery = ""
220+
, uriFragment = '#':ufrag } ->
221+
Link (fixAttrs s as) is (addHash (if null upath
222+
then s
223+
else upath) (T.pack ufrag), tit)
224+
_ -> Link (fixAttrs s as) is (url, tit)
218225
fixInlineIRs _ v = v
219226

220227
prependHash :: [Text] -> Inline -> Inline

0 commit comments

Comments
 (0)