Skip to content

Commit

Permalink
EPUB: Recover when KOReader annotation is missing field
Browse files Browse the repository at this point in the history
  • Loading branch information
AbeJellinek committed Dec 3, 2024
1 parent d49df68 commit 0faca99
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/dom/epub/lib/koreader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function parseAnnotationsFromKOReaderMetadata(metadata: BufferSource): KO
}

let annotations: KOReaderAnnotation[] = [];
for (let annotationTableField of annotationsTable.fields) {
fieldLoop: for (let annotationTableField of annotationsTable.fields) {
let annotationTable = annotationTableField.value;
if (annotationTable.type !== 'TableConstructorExpression') {
throw new Error('Invalid KOReader metadata: "annotations" entry is not a table');
Expand All @@ -113,12 +113,15 @@ export function parseAnnotationsFromKOReaderMetadata(metadata: BufferSource): KO
text: findField(annotationTable, 'text'),
datetime: findField(annotationTable, 'datetime'),
};
for (let [key, value] of Object.entries(annotationFields)) {
if (['pos0', 'pos1', 'text', 'datetime'].includes(key) && !value) {
throw new Error(`Invalid KOReader metadata: annotation is missing required field "${key}"`);
for (let key of ['pos0', 'pos1', 'text', 'datetime'] as const) {
let value = annotationFields[key];
if (!value) {
console.error(`Invalid KOReader metadata: annotation is missing required field "${key}"`);
continue fieldLoop;
}
if (value && value.type !== 'StringLiteral') {
throw new Error(`Invalid KOReader metadata: annotation field "${key}" is not a string`);
console.error(`Invalid KOReader metadata: annotation field "${key}" is not a string`);
continue fieldLoop;
}
}
annotations.push({
Expand Down

0 comments on commit 0faca99

Please sign in to comment.