book-helpers.ts 985 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { Book, BookEntryKind } from "@/types/book";
  2. export type FlattenedBookEntry = {
  3. id: string;
  4. title: string;
  5. kind: BookEntryKind;
  6. order: number;
  7. content: string[];
  8. sectionId: string;
  9. sectionTitle: string;
  10. sectionKind: BookEntryKind;
  11. flatIndex: number;
  12. };
  13. export function getBookEntries(book: Book): FlattenedBookEntry[] {
  14. return book.sections.flatMap((section) =>
  15. section.entries.map((entry) => ({
  16. ...entry,
  17. sectionId: section.id,
  18. sectionTitle: section.title,
  19. sectionKind: section.kind,
  20. flatIndex: 0
  21. }))
  22. ).map((entry, index) => ({
  23. ...entry,
  24. flatIndex: index
  25. }));
  26. }
  27. export function getBookContentCount(book: Book) {
  28. return getBookEntries(book).length;
  29. }
  30. export function getBookStructureStats(book: Book) {
  31. const entries = getBookEntries(book);
  32. return {
  33. loreCount: entries.filter((item) => item.kind === "lore").length,
  34. novelCount: entries.filter((item) => item.kind === "novel").length
  35. };
  36. }