Skip to content

Commit

Permalink
fix: correct link path in navigation link on epub3 (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonardo R. da Silva authored and vers-one committed Sep 23, 2019
1 parent 3866e68 commit 6ca3987
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Source/VersOne.Epub/Readers/Epub3NavDocumentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,35 @@ public static async Task<Epub3NavDocument> ReadEpub3NavDocumentAsync(ZipArchive
{
throw new Exception("EPUB parsing error: navigation file does not contain body element.");
}

result.Navs = new List<Epub3Nav>();
string folder = ZipPathUtils.GetDirectoryPath(navManifestItem.Href);

foreach (XElement navNode in bodyNode.Elements(xhtmlNamespace + "nav"))
{
Epub3Nav epub3Nav = ReadEpub3Nav(navNode);
AdjustRelativePath(epub3Nav.Ol, folder);
result.Navs.Add(epub3Nav);
}
return result;
}

private static void AdjustRelativePath(Epub3NavOl nav, string rootFolder)
{
if (nav == null) return;

foreach (var li in nav.Lis)
{
if (li.Anchor?.Href != null && li.Anchor.Href.Length > 0 && li.Anchor.Href[0] != '/')
{
var relativeHref = $"{rootFolder}/{li.Anchor.Href}";
li.Anchor.Href = relativeHref;
}

AdjustRelativePath(li.ChildOl, rootFolder);
}
}

private static Epub3Nav ReadEpub3Nav(XElement navNode)
{
Epub3Nav epub3Nav = new Epub3Nav();
Expand Down

0 comments on commit 6ca3987

Please sign in to comment.