openreader/src/lib/epub.ts
Richard Roberson 04def62ff5 refactor(core): standardize module structure and enhance TTS types
- Reorganized utility modules from `src/utils` to `src/lib` for clearer separation of concerns.
- Introduced new, dedicated type definitions in `src/types` for improved type safety in configuration and TTS API interactions.
  - Replaced `src/types/appConfig.ts` with `src/types/config.ts`.
  - Added `src/types/tts.ts` for TTS request payloads, error structures, and retry options.
- Updated module imports across several contexts (`Config`, `EPUB`, `HTML`, `PDF`, `TTS`) and components to reflect the new `lib` and `types` locations.
- Enhanced TTS API request and error handling in `src/app/api/tts/route.ts` and TTS-consuming contexts with explicit types.
- Simplified `ProgressCard`, `ProgressPopup`, and `AudiobookExportModal` components by removing the `isProcessing` prop, centralizing processing state management.
- Streamlined `HTMLContext` by removing `createFullAudioBook` and `isAudioCombining` properties, focusing its scope.
2025-11-16 15:12:35 -07:00

36 lines
1.1 KiB
TypeScript

export function findCommonBase(startCfi: string, endCfi: string): string {
// Remove 'epubcfi(' prefix and ')' suffix
const start = startCfi.replace(/^epubcfi\(|\)$/g, '');
const end = endCfi.replace(/^epubcfi\(|\)$/g, '');
const startParts = start.split('/');
const endParts = end.split('/');
const commonParts: string[] = [];
for (let i = 0; i < startParts.length && i < endParts.length; i++) {
if (startParts[i] === endParts[i]) {
commonParts.push(startParts[i]);
} else {
break;
}
}
return commonParts.join('/');
}
export function createRangeCfi(startCfi: string, endCfi: string): string {
// Clean the CFIs
const start = startCfi.replace(/^epubcfi\(|\)$/g, '');
const end = endCfi.replace(/^epubcfi\(|\)$/g, '');
// Find the common base path
const base = findCommonBase(startCfi, endCfi);
// Get the unique parts of start and end
const startUnique = start.substring(base.length);
const endUnique = end.substring(base.length);
// Construct the range CFI
return `epubcfi(${base},${startUnique},${endUnique})`;
}