@prose-reader/core 1.14.0 → 1.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2762,28 +2762,34 @@
2762
2762
  const buildChapterInfoFromSpineItem = (manifest, item) => {
2763
2763
  var _a;
2764
2764
  const { href } = item;
2765
- return getChapterInfo(href, ((_a = manifest.nav) == null ? void 0 : _a.toc) ?? []);
2765
+ return getChapterInfo(href, ((_a = manifest.nav) == null ? void 0 : _a.toc) ?? [], manifest);
2766
2766
  };
2767
- const getChapterInfo = (href, tocItems) => {
2768
- return tocItems.reduce((acc, tocItem) => {
2769
- const indexOfHash = tocItem.href.indexOf(`#`);
2770
- const tocItemPathWithoutAnchor = indexOfHash > 0 ? tocItem.href.substr(0, indexOfHash) : tocItem.href;
2767
+ const getChapterInfo = (href, tocItem, manifest) => {
2768
+ const spineItemIndex = manifest.spineItems.findIndex((item) => item.href === href);
2769
+ return tocItem.reduce((acc, tocItem2) => {
2770
+ const indexOfHash = tocItem2.href.indexOf(`#`);
2771
+ const tocItemPathWithoutAnchor = indexOfHash > 0 ? tocItem2.href.substr(0, indexOfHash) : tocItem2.href;
2771
2772
  const tocItemHrefWithoutFilename = tocItemPathWithoutAnchor.substring(0, tocItemPathWithoutAnchor.lastIndexOf("/"));
2772
2773
  const hrefWithoutFilename = href.substring(0, href.lastIndexOf("/"));
2773
2774
  const hrefIsChapterHref = href.endsWith(tocItemPathWithoutAnchor);
2774
2775
  const hrefIsWithinChapter = hrefWithoutFilename !== "" && hrefWithoutFilename.endsWith(tocItemHrefWithoutFilename);
2775
- if (hrefIsChapterHref || hrefIsWithinChapter) {
2776
+ const isPossibleTocItemCandidate = hrefIsChapterHref || hrefIsWithinChapter;
2777
+ if (isPossibleTocItemCandidate) {
2778
+ const spineItemIndexOfPossibleCandidate = manifest.spineItems.findIndex((item) => item.href === tocItem2.href);
2779
+ const spineItemIsBeforeThisTocItem = spineItemIndex < spineItemIndexOfPossibleCandidate;
2780
+ if (spineItemIsBeforeThisTocItem)
2781
+ return acc;
2776
2782
  return {
2777
- title: tocItem.title,
2778
- path: tocItem.path
2783
+ title: tocItem2.title,
2784
+ path: tocItem2.path
2779
2785
  };
2780
2786
  }
2781
- const subInfo = getChapterInfo(href, tocItem.contents);
2787
+ const subInfo = getChapterInfo(href, tocItem2.contents, manifest);
2782
2788
  if (subInfo) {
2783
2789
  return {
2784
2790
  subChapter: subInfo,
2785
- title: tocItem.title,
2786
- path: tocItem.path
2791
+ title: tocItem2.title,
2792
+ path: tocItem2.path
2787
2793
  };
2788
2794
  }
2789
2795
  return acc;