Skip to content

fix(cbz): compare page paths segment by segment for reading order - #79

Merged
chrox merged 1 commit into
mainfrom
fix/cbz-split-folder-page-order
Aug 17, 2026
Merged

fix(cbz): compare page paths segment by segment for reading order#79
chrox merged 1 commit into
mainfrom
fix/cbz-split-folder-page-order

Conversation

@chrox

@chrox chrox commented Aug 17, 2026

Copy link
Copy Markdown
Member

Problem

A CBZ chapter split across folders that share a chapter number, such as Chapter 0060/ and Chapter 0060 (2)/, renders part 2 before part 1 (readest/readest#5745).

Image paths are flattened into one list and sorted as plain strings. After the shared prefix Chapter 0060, the comparison is " (2)/001.jpg" vs "/001.jpg", and a space (0x20) sorts before a slash (0x2F), so the (2) folder wins.

Upstream's newer .sort(new Intl.Collator([], { numeric: true }).compare) on whole paths does not fix this either: the collator still ranks the space-paren run before the slash (verified empirically).

Fix

Compare paths segment by segment: split on /, compare each segment with a numeric collator, and let the shorter path win at divergence so a folder that is a prefix of a sibling sorts first. Numeric collation also fixes unpadded page names (2.jpg before 10.jpg).

Order produced for the report's layout:

Chapter 0060/001.jpg
Chapter 0060/002.jpg
Chapter 0060 (2)/001.jpg
Chapter 0060 (2)/002.jpg
Chapter 0060 (3)/001.jpg
Chapter 0060 (10)/001.jpg
Chapter 0061/001.jpg

Tests

Regression test lands in the app repo (src/__tests__/foliate-cbz-page-order.test.ts) alongside the submodule bump; it fails against the old sort and passes with this change.

🤖 Generated with Claude Code

Sorting flattened archive paths as plain strings puts
"Chapter 0060 (2)/001.jpg" before "Chapter 0060/001.jpg" because a space
sorts before a slash, so part 2 of a split chapter renders ahead of
part 1. Compare paths per segment with a numeric collator instead, with
the shorter path winning at divergence, so a folder that is a prefix of
a sibling sorts first. Numeric collation also keeps unpadded pages such
as 2.jpg ahead of 10.jpg.

Fixes readest/readest#5745

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@chrox
chrox merged commit 4735c0a into main Aug 17, 2026
2 checks passed
chrox added a commit to readest/readest that referenced this pull request Aug 17, 2026
…js#79

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
chrox added a commit to readest/readest that referenced this pull request Aug 17, 2026
* fix(cbz): order split chapter folders base-first (#5745)

A CBZ chapter split across folders sharing a chapter number, such as
Chapter 0060/ and Chapter 0060 (2)/, rendered part 2 before part 1:
image paths were flattened and sorted as plain strings, and a space
sorts before a slash. Bump foliate-js to compare paths segment by
segment with a numeric collator so a folder that is a prefix of a
sibling sorts first, and add a regression test for the reported layout.

Fixes #5745

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: pin foliate-js to the squash-merged commit of readest/foliate-js#79

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: bump foliate-js to latest main

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
@chrox
chrox deleted the fix/cbz-split-folder-page-order branch August 17, 2026 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant