sillsdev / sillsdev/interlinearizer-extension
sortByDocOrder ties unplaceable refs to position zero, reordering stored phrase tokens
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 2
- Forks
- 0
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 55
Description
sortByDocOrder scores a ref the order map cannot place as 0:
(a, b) => (tokenDocOrder.get(a.tokenRef) ?? 0) - (tokenDocOrder.get(b.tokenRef) ?? 0)
so it ties with whatever sits at position zero rather than sorting to a place of its own. The sort is stable, so a stranded ref lands second, not first:
sort keys : 0, 1, 0
input : tok-a, tok-b, tok-z (tok-z stranded)
sorted : tok-a, tok-z, tok-b
Stranded refs are ordinary rather than hypothetical: Token.ref is `${sid}:${charStart}` (src/parsers/papi/bookTokenizer.ts), the book is re-tokenized from the current USJ on every load (src/hooks/useInterlinearizerBookData.ts), and the analysis persists separately, so any upstream text edit strands every ref after it. See #136.
Where it reaches stored data
splitPhraseAtBoundary (via sliceAtBoundary), the merge updatePhrase calls in TokenLinkIcon, and PhraseBox's edit-mode writes all sort through this helper before writing. A split or merge on a phrase holding a stranded member can therefore write a token list whose order no longer matches document order.
Severity is moderated by the fact that such a phrase is already half-broken: the stranded member never renders, so the reordering moves a snapshot nobody can see. It is worth fixing because it is cheap, and because the next reader will not expect it.
Fix
Decorate-sort-undecorate: give an unplaceable token the order of the last placeable one before it, so it holds its stored position instead of tying with position zero.
src/utils/phrase-text.ts already took this position by not re-sorting at all (ba23de3, #329) — the label it builds reads the phrase link's stored order, which every writer sets to document order. This issue generalizes that for the callers that must sort.
Also check src/utils/segmentation.ts:105, which repeats the same (order.get(a) ?? 0) - (order.get(b) ?? 0) pattern.
Size: S
Priority: P3 — latent. #136's re-anchoring would reduce but not remove the exposure, since unmatched links stay stale and their refs stay stranded.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with sortByDocOrder and trace its callers, especially splitPhraseAtBoundary, updatePhrase in TokenLinkIcon, and PhraseBox edit-mode writes. Compare the matching sort in src/utils/segmentation.ts:105 and review src/utils/phrase-text.ts for the existing stored-order approach. Done means stranded refs retain their stored position without tying with position zero in either sorting path.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100