From 55b2c678a4fa71c2178b39c3846d4e9b2b957d3d Mon Sep 17 00:00:00 2001 From: Huang Xin Date: Sun, 7 Sep 2025 12:05:27 +0800 Subject: [PATCH] fix(kosync): handle non-standard XPath syntax from Koreader, closes #1968 (#1988) --- apps/readest-app/src/utils/xcfi.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/apps/readest-app/src/utils/xcfi.ts b/apps/readest-app/src/utils/xcfi.ts index 94c8a736..b3f8da8b 100644 --- a/apps/readest-app/src/utils/xcfi.ts +++ b/apps/readest-app/src/utils/xcfi.ts @@ -544,12 +544,17 @@ export const getXPointerFromCFI = async ( // Koreader sometimes cannot recognize totally valid XPointer. // Workaround this by cleaning up any trailing /text().N segments. +// Also remove any trailing .N suffixes after node steps. // This has neglectable effect on position accuracy as the XPointer still point to the correct element // while offset within the text node is usually ignored by pagination. export const normalizeProgressXPointer = (xpointer: string): string => { - const tailingTextRange = /\/text\(\).*$/; - if (xpointer.match(tailingTextRange)) { - xpointer = xpointer.replace(tailingTextRange, ''); + const tailingTextOffset = /\/text\(\).*$/; + if (xpointer.match(tailingTextOffset)) { + xpointer = xpointer.replace(tailingTextOffset, ''); + } + const suffixNodeOffset = /\.\d+$/; + if (xpointer.match(suffixNodeOffset)) { + xpointer = xpointer.replace(suffixNodeOffset, ''); } return xpointer; };