Skip to content

Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.

Beefed up ToC generation to handle new ways of packing epub. (#840)

Joseph Milazzod2807253c31f
diff --git a/API/Controllers/BookController.cs b/API/Controllers/BookController.cs
index e05545ce2..2dc5001ad 100644
--- a/API/Controllers/BookController.cs
+++ b/API/Controllers/BookController.cs
@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using API.Data;
@@ -109,32 +110,12 @@ namespace API.Controllers
}
}
- if (navigationItem.Link == null)
- {
- var item = new BookChapterItem()
- {
- Title = navigationItem.Title,
- Children = nestedChapters
- };
- if (nestedChapters.Count > 0)
- {
- item.Page = nestedChapters[0].Page;
- }
- chaptersList.Add(item);
- }
- else
- {
- var groupKey = BookService.CleanContentKeys(navigationItem.Link.ContentFileName);
- if (mappings.ContainsKey(groupKey))
- {
- chaptersList.Add(new BookChapterItem()
- {
- Title = navigationItem.Title,
- Page = mappings[groupKey],
- Children = nestedChapters
- });
- }
- }
+ CreateToCChapter(navigationItem, nestedChapters, chaptersList, mappings);
+ }
+
+ if (navigationItem.NestedItems.Count == 0)
+ {
+ CreateToCChapter(navigationItem, Array.Empty<BookChapterItem>(), chaptersList, mappings);
}
}
@@ -187,6 +168,38 @@ namespace API.Controllers
return Ok(chaptersList);
}
+ private static void CreateToCChapter(EpubNavigationItemRef navigationItem, IList<BookChapterItem> nestedChapters, IList<BookChapterItem> chaptersList,
+ IReadOnlyDictionary<string, int> mappings)
+ {
+ if (navigationItem.Link == null)
+ {
+ var item = new BookChapterItem()
+ {
+ Title = navigationItem.Title,
+ Children = nestedChapters
+ };
+ if (nestedChapters.Count > 0)
+ {
+ item.Page = nestedChapters[0].Page;
+ }
+
+ chaptersList.Add(item);
+ }
+ else
+ {
+ var groupKey = BookService.CleanContentKeys(navigationItem.Link.ContentFileName);
+ if (mappings.ContainsKey(groupKey))
+ {
+ chaptersList.Add(new BookChapterItem()
+ {
+ Title = navigationItem.Title,
+ Page = mappings[groupKey],
+ Children = nestedChapters
+ });
+ }
+ }
+ }
+
[HttpGet("{chapterId}/book-page")]
public async Task<ActionResult<string>> GetBookPage(int chapterId, [FromQuery] int page)
{