Skip to content

Prevent duplicate files for existing Readwise books#99

Draft
javenfang wants to merge 1 commit into
readwiseio:masterfrom
javenfang:agent/readwise-stable-book-paths
Draft

Prevent duplicate files for existing Readwise books#99
javenfang wants to merge 1 commit into
readwiseio:masterfrom
javenfang:agent/readwise-stable-book-paths

Conversation

@javenfang

Copy link
Copy Markdown

Summary

This PR prevents the Obsidian plugin from creating a new suffixed Markdown file when an export artifact contains a filename for a book that the vault already knows by book_id.

The plugin now resolves the write target through the existing booksIDsMap before writing highlight files. If the same book ID already has a known vault path, the sync updates that stable path instead of trusting a newly exported path such as Book Title (467).md.

User impact / bug report

This comes from a recurring real-world sync failure in a private Obsidian vault. The vault repeatedly accumulated numeric-suffix duplicates under Readwise/, for example paths shaped like Title (467).md and Title (485).md. In one cleanup pass, there were 624 duplicate Markdown files across three stable Readwise book/document IDs, with 208 suffixed copies per ID.

The pattern suggests the export artifact can sometimes contain a new suffixed filename for content that is already represented in the vault. Since the current plugin maps filePath -> bookID and writes to the artifact filename directly, those suffixed filenames become new files even when the same book_id already exists locally.

What changed

  • Added stablePathForBookExport() to choose the existing path for the same book ID before creating a new file.
  • Preserved user-renamed paths by preferring the existing mapped path.
  • Kept full document content paths separate from normal highlight paths.
  • Added regression tests for duplicate numeric suffix paths and existing duplicate candidates.

Validation

  • npm test
  • npm run typecheck
  • npm run build

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