mirror of
https://github.com/panatgithub/AnkiHeadingSync.git
synced 2026-07-22 06:51:43 +00:00
在挖空数量减少时重新构建笔记,以确保 Anki 中的卡片正确更新。 Rebuild notes when cloze numbers shrink to ensure Anki cards are updated correctly.
4.7 KiB
4.7 KiB
Cloze Shrink Rebuild Gap Report
Scope
This report records the actual repository state before implementing automatic note rebuild for sequential-to-all Cloze shrink.
Current Cloze rendering path
- Heading recognition happens in
CardIndexingService.ts. #anki-clozeindexes as runtimecardType: "cloze"withclozeMode: "sequential".#anki-cloze-allindexes as runtimecardType: "cloze"withclozeMode: "all".ManualCardRenderer.tsis the only place that currently applies automatic Cloze numbering.- Current renderer behavior is:
- explicit
{c2:内容}preserves the explicit index - native
{{c2::内容}}is left intact because the brace-based replacement does not rewrite it - automatic
{内容}usesc1/c2/c3/...in sequential mode - automatic
{内容}usesc1/c1/c1/...in all mode ==highlight==becomes brace-based Cloze input first whenconvertHighlightsToClozeis enabled- fenced code and inline code are protected before replacement
- explicit
RenderConfigService.tsalready includesclozeMode: "all"in render hashing, so sequential-to-all already counts as rendered-output drift.
Current state persistence behavior
CardStateinPluginState.tsdoes not containclozeMode.ManualSyncService.buildNextState()writescardType, render hash, deck, tags, and raw block data, but does not persistclozeMode.DataJsonPluginStateRepository.tsmigrates onlybasicandclozeruntime card types and ignores any Cloze mode metadata because none exists yet.FileIndexerService.restoreIndexedCard()rebuildsIndexedCardfrom saved state for unchanged files, but currently dropsclozeModeentirely.- Result: once a Cloze card is restored from state instead of re-indexed from Markdown, mode information is lost.
Current diff planning behavior
ManualSyncPlan.tscurrently has:toCreatetoUpdatetoVerifyDecktoChangeDecktoRewriteMarkertoOrphan
DiffPlannerService.tsonly compares:rawBlockHashrenderConfigHash- tag set
- deck change
- marker rewrite conditions
- There is no rebuild path.
- When sequential-to-all changes rendered output, the card simply enters
toUpdatebecause render hash changed. - There is no current comparison of old vs new final Cloze number sets.
Current Anki execution behavior
AnkiBatchExecutor.tscurrently supports:- create via
addNotes - update via
updateNotes - note type migration via
updateNoteModel - deck migration via
changeDecks - marker rewrite via
markerWrites
- create via
- Missing-note fallback already exists indirectly: when a planned update noteId is absent from fetched summaries, the executor adds a new note and marks it for marker rewrite.
- There is no rebuild queue and no create-then-delete flow for an existing note.
deleteNotesexists on the gateway and fake gateway, but is currently unused by manual sync execution.resolvedNoteIdsandmarkerWritesare already the right integration seam for replacing a noteId after a successful create.
Current marker writeback behavior
MarkdownWriteBackService.tswrites markers fromPlannedCard[].- It uses the
noteIdpresent on each planned card at execution-result time. - This means rebuild can reuse the current writeback path as long as execution returns the replacement
noteIdinresolvedNoteIdsand marker writes for that sync key.
Exact missing integration points
- Persist
clozeModeonCardState. - Load legacy state without
clozeModeas sequential for runtimecardType: "cloze". - Restore
clozeModeinFileIndexerService.restoreIndexedCard(). - Add a pure helper that predicts the final Cloze number set from Markdown with the same counting rules the renderer uses.
- Extend
ManualSyncPlanwithtoRebuild. - Extend
DiffPlannerServiceto detect sequential-to-all shrink only when the final number set becomes smaller. - Extend
AnkiBatchExecutorwith create-then-delete rebuild handling and failure rollback. - Persist rebuilt cards under the new noteId only.
- Expose rebuild count in execution/manual sync results if chosen.
Risky failure paths
- If state persistence is not updated, unchanged-file restore will lose mode information and planner decisions will be inconsistent.
- If the Cloze number helper drifts from renderer behavior, planner may rebuild too often or miss rebuilds.
- If rebuild deletes the old note before create succeeds, the note can be lost.
- If create succeeds but delete fails and the new note is not best-effort cleaned up, duplicate notes can remain while local marker/state stays ambiguous.
- If rebuild cards are still allowed into
toUpdate,toChangeDeck, ortoRewriteMarker, the same sync key can be executed twice with conflicting noteIds.