diff --git a/apps/readest-app/src/libs/document.ts b/apps/readest-app/src/libs/document.ts index 5515d678..58be5cd8 100644 --- a/apps/readest-app/src/libs/document.ts +++ b/apps/readest-app/src/libs/document.ts @@ -59,6 +59,7 @@ export interface BookDoc { }; toc: Array; sections: Array; + splitTOCHref(href: string): Array; getCover(): Promise; } diff --git a/apps/readest-app/src/store/readerStore.ts b/apps/readest-app/src/store/readerStore.ts index 314c88d6..811d8e46 100644 --- a/apps/readest-app/src/store/readerStore.ts +++ b/apps/readest-app/src/store/readerStore.ts @@ -114,13 +114,13 @@ export const useReaderStore = create((set, get) => ({ console.log('Loading book', key); const { book: loadedBookDoc } = await new DocumentLoader(file).open(); const bookDoc = loadedBookDoc as BookDoc; - if (bookDoc.toc && bookDoc.sections) { + if (bookDoc.toc?.length > 0 && bookDoc.sections?.length > 0) { updateTocID(bookDoc.toc); const sections = bookDoc.sections.reduce((map: Record, section) => { map[section.id] = section; return map; }, {}); - updateTocCFI(bookDoc.toc, sections); + updateTocCFI(bookDoc, bookDoc.toc, sections); } useBookDataStore.setState((state) => ({ booksData: { diff --git a/apps/readest-app/src/utils/toc.ts b/apps/readest-app/src/utils/toc.ts index e12b9229..87f6bd35 100644 --- a/apps/readest-app/src/utils/toc.ts +++ b/apps/readest-app/src/utils/toc.ts @@ -1,4 +1,4 @@ -import { SectionItem, TOCItem, CFI } from '@/libs/document'; +import { SectionItem, TOCItem, CFI, BookDoc } from '@/libs/document'; export const findParentPath = (toc: TOCItem[], href: string): TOCItem[] => { for (const item of toc) { @@ -47,17 +47,21 @@ export const updateTocID = (items: TOCItem[], index = 0): number => { return index; }; -export const updateTocCFI = (items: TOCItem[], sections: { [id: string]: SectionItem }): void => { +export const updateTocCFI = ( + bookDoc: BookDoc, + items: TOCItem[], + sections: { [id: string]: SectionItem }, +): void => { items.forEach((item) => { if (item.href) { - const id = item.href.split('#')[0]!; + const id = bookDoc.splitTOCHref(item.href)[0]!; const section = sections[id]; if (section) { item.cfi = section.cfi; } } if (item.subitems) { - updateTocCFI(item.subitems, sections); + updateTocCFI(bookDoc, item.subitems, sections); } }); }; diff --git a/packages/foliate-js b/packages/foliate-js index bb8d44cc..5bf4163a 160000 --- a/packages/foliate-js +++ b/packages/foliate-js @@ -1 +1 @@ -Subproject commit bb8d44cc040d351ebea47df9ffb2565310d7d7c6 +Subproject commit 5bf4163a3ae0e31dd4afb43eee75935fb1665d97