Skip to content

Fix "Readwise Syncs.md" JSON parse error when base folder is vault root#97

Merged
krzyssikora merged 1 commit into
masterfrom
fix/readwise-syncs-vault-root-json-parse
May 28, 2026
Merged

Fix "Readwise Syncs.md" JSON parse error when base folder is vault root#97
krzyssikora merged 1 commit into
masterfrom
fix/readwise-syncs-vault-root-json-parse

Conversation

@gregbot-rw

Copy link
Copy Markdown
Contributor

Summary

Fixes the Obsidian plugin error reported in DevHelp:

error writing Readwise Syncs.md: SyntaxError: No number after minus sign in JSON at position 1 (line 1 column 2)

Highlights export fine, but the sync notification file fails to write.

Root cause

In downloadExport (src/main.ts), each export ZIP entry is classified as either a JSON document file (→ JSON.parse) or the special plain-markdown sync-log file Readwise Syncs.md (→ used verbatim). The classification compares the normalized processedFileName against an un-normalized expected path:

const isReadwiseSyncFile = processedFileName === `${this.settings.readwiseDir}/${READWISE_SYNC_FILENAME}.md`;

processedFileName is built with normalizePath(...), which strips leading slashes. When the user's base folder (readwiseDir) is the vault root ("/" or ""), the right-hand side keeps a stray leading slash ("/Readwise Syncs.md") while processedFileName becomes "Readwise Syncs.md". They no longer match, so the markdown sync file falls into the else branch and is fed to JSON.parse. Because the rendered notification commonly begins with - (e.g. - [[2026-05-28]] ...), V8 throws No number after minus sign in JSON at position 1.

Per-document files are unaffected because they are genuine .json entries — which is why highlights export fine while only the sync notification errors.

Fix

Normalize both sides of the comparison via a small pure helper readwiseSyncFilePath(readwiseDir, normalizePath) in src/paths.ts, and use it in main.ts. No behavior change for the default Readwise base folder or nested folders; the root-folder case is now correctly recognized.

Reproduction (exact user error)

A standalone repro of Obsidian's normalizePath + the original comparison reproduces the user's error verbatim and confirms the fix:

readwiseDir = "/"   BEFORE: isReadwiseSyncFile=false → SyntaxError: No number after minus sign in JSON at position 1 (line 1 column 2)
readwiseDir = "/"   AFTER : isReadwiseSyncFile=true  → no error
readwiseDir = ""    BEFORE: isReadwiseSyncFile=false → SyntaxError: No number after minus sign in JSON at position 1 (line 1 column 2)
readwiseDir = ""    AFTER : isReadwiseSyncFile=true  → no error
readwiseDir = "Readwise"        BEFORE/AFTER: isReadwiseSyncFile=true (unchanged)
readwiseDir = "Notes/Readwise"  BEFORE/AFTER: isReadwiseSyncFile=true (unchanged)

Test plan

  • npm test — full suite passes (10/10), including new tests/paths.test.js covering root, default, and nested base folders
  • npm run build — production rollup bundle builds clean
  • Manual: set base folder to vault root in Obsidian, run a sync, confirm Readwise Syncs.md is written without error

🤖 Generated with Claude Code

When the plugin's base folder ("readwiseDir") is set to the vault root, the
exported markdown sync-log file ("Readwise Syncs.md") was misidentified as a
JSON book entry and run through JSON.parse, throwing:

  SyntaxError: No number after minus sign in JSON at position 1

`processedFileName` is normalized with `normalizePath`, but the expected sync
path it was compared against was not. With a root base folder ("/" or ""), the
unnormalized side keeps a stray leading slash, the equality check fails, and the
plain-markdown sync file (which commonly starts with "- ") falls into the JSON
branch.

Normalize both sides of the comparison via a small `readwiseSyncFilePath`
helper, and add a regression test covering root, default, and nested base
folders.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@krzyssikora krzyssikora merged commit c1495bb into master May 28, 2026
1 check passed
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.

2 participants