obsidian-sync-mcp 0.2.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  ![Node](https://img.shields.io/badge/node-22%2B-green.svg)
8
8
  ![TypeScript](https://img.shields.io/badge/TypeScript-5-blue.svg)
9
9
 
10
- Give any AI agent remote read/write access to your Obsidian vault over MCP. Run it locally from your machine, or deploy it to the cloud so it works even when your laptop is off.
10
+ Give any AI agent access to your Obsidian vault over MCP. Run it locally against your vault files, or pair it with [Self-hosted LiveSync](https://github.com/vrtmrz/obsidian-livesync) and deploy to the cloud so it works even when your machine is off.
11
11
 
12
12
  > **Example:** From your phone, ask your AI: "What's in my daily note for today?" — and get the full content back, with a link to open it in Obsidian.
13
13
 
@@ -15,10 +15,10 @@ Give any AI agent remote read/write access to your Obsidian vault over MCP. Run
15
15
 
16
16
  ## How it works
17
17
 
18
- The server has two modes:
18
+ The server connects to your vault in two ways:
19
19
 
20
- - **Local mode** — reads `.md` files directly from your vault folder. No database needed.
21
- - **Remote mode** — reads from CouchDB via [Self-hosted LiveSync](https://github.com/vrtmrz/obsidian-livesync), the community plugin that replaces iCloud/Obsidian Sync with a CouchDB backend (600k+ downloads). The server uses [livesync-commonlib](https://github.com/vrtmrz/livesync-commonlib) — the same library that powers the plugin — for proper chunk handling and E2E encryption support.
20
+ - **Filesystem mode** — reads `.md` files directly from your vault folder. No database needed.
21
+ - **CouchDB mode** — reads from a CouchDB database, locally or in the cloud. Your vault syncs to CouchDB via [Self-hosted LiveSync](https://github.com/vrtmrz/obsidian-livesync), the community Obsidian plugin (600k+ downloads). The MCP server reads from CouchDB directly using [livesync-commonlib](https://github.com/vrtmrz/livesync-commonlib) — the same library that powers the plugin — for proper chunk handling and E2E encryption support.
22
22
 
23
23
  Both modes expose the same MCP tools over HTTP, so any MCP-compatible agent can connect: Claude, Copilot, custom agents, anything that speaks the [Model Context Protocol](https://modelcontextprotocol.io).
24
24
 
@@ -26,175 +26,169 @@ Both modes expose the same MCP tools over HTTP, so any MCP-compatible agent can
26
26
 
27
27
  ## Choose your setup
28
28
 
29
- | You have... | Do this | What happens |
29
+ | Need it always available? | Have LiveSync? | Go to |
30
30
  |---|---|---|
31
- | **LiveSync already** | Point MCP server at your CouchDB | Add AI access to your existing setup |
32
- | **A Mac that's always on** | Run in local mode + tunnel | Reads files directly, no database |
33
- | **Nothing yet, want it always on** | Deploy to Fly.io | CouchDB + MCP server in the cloud |
31
+ | Yes | Yes | [Setup A](#a-deploy-mcp-to-the-cloud) — add MCP alongside your existing CouchDB |
32
+ | Yes | No | [Setup B](#b-deploy-everything-to-the-cloud) — CouchDB + MCP + LiveSync from scratch |
33
+ | No | — | [Setup C](#c-run-on-your-machine) — filesystem or CouchDB, npx or Docker |
34
34
 
35
35
  ---
36
36
 
37
- ## Already have LiveSync?
37
+ ## A. Deploy MCP to the cloud
38
38
 
39
- If you're already using Self-hosted LiveSync, you just need the MCP server. No database setup, no plugin changes — point it at your existing CouchDB.
39
+ You already have LiveSync and CouchDB on an always-on server. You just need the MCP server deployed alongside it.
40
40
 
41
- ### Run locally
41
+ **Using Fly.io setup script** (macOS/Linux, or WSL on Windows):
42
42
 
43
43
  ```bash
44
- npx obsidian-sync-mcp
44
+ git clone https://github.com/es617/obsidian-sync-mcp.git
45
+ cd obsidian-sync-mcp
46
+ ./deploy/setup.sh # choose option 2 (MCP only)
45
47
  ```
46
48
 
47
- Set your CouchDB connection:
49
+ The script asks for your CouchDB connection details, vault name, and encryption passphrase.
48
50
 
49
- ```bash
50
- COUCHDB_URL=https://your-couchdb:5984 \
51
- COUCHDB_USER=admin \
52
- COUCHDB_PASSWORD=yourpassword \
53
- COUCHDB_DATABASE=obsidian \
54
- VAULT_NAME=MyVault \
55
- MCP_AUTH_TOKEN=yourpassword \
56
- npx obsidian-sync-mcp
57
- ```
58
-
59
- ### Or deploy to Fly.io (MCP only, no CouchDB)
51
+ **Or run the Docker image on any always-on server:**
60
52
 
61
53
  ```bash
62
- fly launch --image ghcr.io/es617/obsidian-sync-mcp:latest
63
- fly secrets set \
64
- COUCHDB_URL=https://your-couchdb:5984 \
65
- COUCHDB_USER=admin \
66
- COUCHDB_PASSWORD=yourpassword \
67
- COUCHDB_DATABASE=obsidian \
68
- VAULT_NAME=MyVault \
69
- MCP_AUTH_TOKEN=$(openssl rand -hex 16) \
70
- BASE_URL=https://your-app.fly.dev
54
+ docker run -p 8787:8787 \
55
+ -e COUCHDB_URL=https://your-couchdb:5984 \
56
+ -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=yourpassword \
57
+ -e COUCHDB_DATABASE=obsidian -e VAULT_NAME=MyVault \
58
+ -e COUCHDB_PASSPHRASE=your-encryption-passphrase \
59
+ -e MCP_AUTH_TOKEN=yourpassword \
60
+ -e BASE_URL=https://your-server-url \
61
+ ghcr.io/es617/obsidian-sync-mcp:latest
71
62
  ```
72
63
 
73
- No volume needed — the MCP server is stateless (it reads from your CouchDB).
74
-
75
- Your MCP endpoint is `https://your-app.fly.dev/mcp`.
76
-
77
- > If you use E2E encryption in LiveSync, also set `COUCHDB_PASSPHRASE` to the same passphrase.
78
-
79
- ---
80
-
81
- ## Local mode + tunnel
82
-
83
- No database, no containers. The server reads `.md` files directly from your vault. Your Mac needs to stay on.
84
-
85
- ```bash
86
- npx obsidian-sync-mcp
87
- ```
64
+ Set `COUCHDB_PASSPHRASE` if you use E2E encryption in LiveSync. Set `BASE_URL` to your public URL (required for OAuth callbacks when agents connect over HTTPS).
88
65
 
89
- With configuration:
66
+ Your MCP endpoint is `https://your-app.fly.dev/mcp` (Fly.io) or `https://your-server:8787/mcp` (Docker behind HTTPS).
90
67
 
91
- ```bash
92
- VAULT_PATH=~/Documents/MyVault \
93
- VAULT_NAME=MyVault \
94
- MCP_AUTH_TOKEN=yourpassword \
95
- npx obsidian-sync-mcp
96
- ```
68
+ See [Cost](#cost-flyio) for Fly.io pricing.
97
69
 
98
- Expose it for remote agents:
70
+ Requires [flyctl](https://fly.io/docs/flyctl/install/) for the Fly.io path:
99
71
 
100
72
  ```bash
101
- # Pick one:
102
- cloudflared tunnel --url http://localhost:8787 # Cloudflare (free)
103
- tailscale funnel 8787 # Tailscale
104
- ngrok http 8787 # ngrok
105
- ```
106
-
107
- Use the tunnel URL + `/mcp` as your MCP server endpoint. Set `BASE_URL` to the tunnel URL when using authentication.
108
-
109
- ```
110
- Your Mac
111
- ├── Obsidian (vault on disk)
112
- ├── obsidian-sync-mcp (reads files directly)
113
- └── tunnel → remote MCP agents
73
+ curl -L https://fly.io/install.sh | sh
74
+ export PATH="$HOME/.fly/bin:$PATH" # add to ~/.zshrc or ~/.bashrc
75
+ fly auth login
114
76
  ```
115
77
 
116
78
  ---
117
79
 
118
- ## Full Fly.io deploy (CouchDB + MCP)
119
-
120
- For a fresh setup with everything in the cloud. One Fly.io app runs both CouchDB and the MCP server. A persistent volume keeps your data.
80
+ ## B. Deploy everything to the cloud
121
81
 
122
- Requires [flyctl](https://fly.io/docs/flyctl/install/) and a Fly.io account.
82
+ Starting fresh — no LiveSync yet. Deploy CouchDB and MCP together, then set up LiveSync in Obsidian.
123
83
 
124
- ### Setup script
84
+ **Using Fly.io setup script** (macOS/Linux, or WSL on Windows):
125
85
 
126
86
  ```bash
127
87
  git clone https://github.com/es617/obsidian-sync-mcp.git
128
88
  cd obsidian-sync-mcp
129
- ./deploy/setup.sh
89
+ ./deploy/setup.sh # choose option 1 (CouchDB + MCP)
130
90
  ```
131
91
 
132
- Generates credentials, creates the volume, and deploys. Save the credentials it prints.
92
+ The script generates credentials, creates the database, and deploys. Save the credentials it prints.
133
93
 
134
- ### Manual CLI
94
+ **Or with Docker Compose on any always-on server:**
135
95
 
136
96
  ```bash
137
- fly launch --no-deploy --copy-config
138
- fly secrets set \
139
- COUCHDB_PASSWORD=$(openssl rand -hex 16) \
140
- VAULT_NAME=MyVault \
141
- MCP_AUTH_TOKEN=$(openssl rand -hex 16)
142
- fly ips allocate-v4 --shared
143
- fly ips allocate-v6
144
- fly volumes create couchdb_data --size 1
145
- fly deploy
97
+ git clone https://github.com/es617/obsidian-sync-mcp.git
98
+ cd obsidian-sync-mcp
99
+
100
+ cat > .env <<EOF
101
+ COUCHDB_PASSWORD=changeme
102
+ VAULT_NAME=MyVault
103
+ EOF
104
+
105
+ docker compose up -d
146
106
  ```
147
107
 
148
- ### After deployment
108
+ **After deployment:**
149
109
 
150
- 1. In Obsidian, install [Self-hosted LiveSync](https://github.com/vrtmrz/obsidian-livesync) and configure it:
151
- - Server URL: `https://your-app.fly.dev:5984`
152
- - Username / password: from setup
153
- - Database: `obsidian`
154
- 2. Your MCP endpoint is `https://your-app.fly.dev/mcp`
110
+ 1. In Obsidian, install [Self-hosted LiveSync](https://github.com/vrtmrz/obsidian-livesync) and configure it with the credentials from the setup output
111
+ 2. Your MCP endpoint is `https://your-app.fly.dev/mcp` (Fly.io) or `http://your-server:8787/mcp` (Docker)
155
112
  3. The `MCP_AUTH_TOKEN` is the password you enter when an agent connects
156
113
 
157
- If you enable E2E encryption in LiveSync, also set: `fly secrets set COUCHDB_PASSPHRASE=your-passphrase`
158
-
159
114
  ```
160
- Fly.io (always on)
161
- ├── CouchDB + persistent volume
115
+ Always-on server
116
+ ├── CouchDB + persistent storage
162
117
  └── MCP server
163
118
  ↑ ↑
164
- Obsidian + LiveSync Remote MCP agents
119
+ Obsidian + LiveSync AI agents
120
+ ```
121
+
122
+ Requires [flyctl](https://fly.io/docs/flyctl/install/) for the Fly.io path:
123
+
124
+ ```bash
125
+ curl -L https://fly.io/install.sh | sh
126
+ export PATH="$HOME/.fly/bin:$PATH" # add to ~/.zshrc or ~/.bashrc
127
+ fly auth login
165
128
  ```
166
129
 
167
- ### Cost
130
+ ---
131
+
132
+ ### Cost (Fly.io)
133
+
134
+ Applies to both Setup A and Setup B.
168
135
 
169
136
  | Component | Cost |
170
137
  |---|---|
171
- | Fly.io VM (shared, 512MB) | ~$3-4/month |
138
+ | CouchDB + MCP VM (shared, 512MB) | ~$3-4/month (kept alive by LiveSync) |
139
+ | MCP-only VM (shared, 256MB) | ~$0-2/month (suspends when idle) |
172
140
  | 1GB persistent volume | ~$0.15/month |
173
- | **Total** | **~$3-4/month** |
174
141
 
175
- Comparable to Obsidian Sync ($4/month) and you own the data.
142
+ As of March 2026, Fly.io [may waive charges under $5/month](https://community.fly.io/t/bill-clarification-under-5-usd-of-usage-bill-charges-are-waived/26366), which could make this effectively free with a shared IPv4. Either way, cheaper than Obsidian Sync ($4/month) and you own the data.
176
143
 
177
144
  ---
178
145
 
179
- ## Docker Compose (local dev)
146
+ ## C. Run on your machine
180
147
 
181
- CouchDB and the MCP server run side by side in Docker.
148
+ Run the MCP server locally. Works with filesystem mode (reads vault files directly) or CouchDB mode (if you have LiveSync). Machine must stay on for agents to reach it.
149
+
150
+ **Filesystem mode (simplest):**
182
151
 
183
152
  ```bash
184
- git clone --recursive https://github.com/es617/obsidian-sync-mcp.git
185
- cd obsidian-sync-mcp
153
+ VAULT_PATH=~/Documents/MyVault \
154
+ VAULT_NAME=MyVault \
155
+ npx obsidian-sync-mcp
156
+ ```
186
157
 
187
- cat > .env <<EOF
188
- COUCHDB_PASSWORD=changeme
189
- VAULT_NAME=MyVault
190
- EOF
158
+ **CouchDB mode (if you have LiveSync):**
191
159
 
192
- docker compose up -d
160
+ ```bash
161
+ COUCHDB_URL=http://localhost:5984 \
162
+ COUCHDB_USER=admin \
163
+ COUCHDB_PASSWORD=yourpassword \
164
+ COUCHDB_DATABASE=obsidian \
165
+ COUCHDB_PASSPHRASE=your-encryption-passphrase \
166
+ VAULT_NAME=MyVault \
167
+ npx obsidian-sync-mcp
193
168
  ```
194
169
 
195
- Then set up LiveSync in Obsidian: server `http://localhost:5984`, database `obsidian`.
170
+ Omit `COUCHDB_PASSPHRASE` if you don't use E2E encryption in LiveSync.
171
+
172
+ **Or with Docker:**
173
+
174
+ ```bash
175
+ docker run -p 8787:8787 \
176
+ -e VAULT_PATH=/vault -v ~/Documents/MyVault:/vault \
177
+ -e VAULT_NAME=MyVault \
178
+ ghcr.io/es617/obsidian-sync-mcp:latest
179
+ ```
180
+
181
+ Your MCP endpoint is `http://localhost:8787/mcp`.
182
+
183
+ **Want remote access?** Add a tunnel (machine must stay on):
184
+
185
+ ```bash
186
+ cloudflared tunnel --url http://localhost:8787 # free
187
+ tailscale funnel 8787 # or Tailscale
188
+ ngrok http 8787 # or ngrok
189
+ ```
196
190
 
197
- Your MCP server is at `http://localhost:8787/mcp`.
191
+ Set `BASE_URL` to the tunnel URL when using authentication.
198
192
 
199
193
  ---
200
194
 
@@ -203,16 +197,19 @@ Your MCP server is at `http://localhost:8787/mcp`.
203
197
  | Tool | Description |
204
198
  |---|---|
205
199
  | `read_note` | Read a note's markdown content by path |
206
- | `write_note` | Create or overwrite a note (preserves creation time on updates) |
207
- | `list_notes` | List all `.md` files, optionally filtered by folder |
208
- | `search_vault` | Sub-millisecond full-text search across all notes (capped at 50 results) |
200
+ | `write_note` | Create or overwrite a note (replaces entire content) |
201
+ | `edit_note` | Edit a note without rewriting it — append, prepend (after frontmatter), or replace exact text |
202
+ | `list_folders` | List all folders in the vault with note counts — use to discover folder names |
203
+ | `list_tags` | List all tags in the vault with counts — use to discover tags before filtering |
204
+ | `list_notes` | List notes with timestamps. Filter by folder, tag, or date. Sort by name or modified. |
205
+ | `search_vault` | Full-text search across all notes. Filter by tag or date, optionally include content snippets. |
209
206
  | `delete_note` | Delete a note |
210
207
  | `move_note` | Move or rename a note — works across folders, creates destination folders automatically |
211
- | `get_note_metadata` | Get frontmatter, tags, links, size, and timestamps without reading the full content |
208
+ | `get_note_metadata` | Get frontmatter, tags, outgoing links, backlinks, size, and timestamps — navigate the knowledge graph |
212
209
 
213
210
  Every tool response includes an [Obsidian deep link](https://help.obsidian.md/Extending+Obsidian/Obsidian+URI) (`obsidian://open?vault=...&file=...`) that works on Mac and iOS.
214
211
 
215
- > "List all my notes in the projects/ folder, then read the one about the MCP server."
212
+ > "Add a bullet point to my daily note." "Find my notes about the MCP server and fix the typo in the second one."
216
213
 
217
214
  ---
218
215
 
@@ -234,7 +231,7 @@ The session is shared across all your Claude interfaces (Desktop, Web, Mobile) a
234
231
 
235
232
  For non-OAuth clients (curl, MCP Inspector, custom agents), you can also pass the token directly as `Authorization: Bearer <MCP_AUTH_TOKEN>`.
236
233
 
237
- Without `MCP_AUTH_TOKEN`, the server runs without authentication — suitable for local testing or use behind a private tunnel.
234
+ Without `MCP_AUTH_TOKEN`, the server runs without authentication — suitable for local use or behind a private network.
238
235
 
239
236
  ---
240
237
 
@@ -242,21 +239,22 @@ Without `MCP_AUTH_TOKEN`, the server runs without authentication — suitable fo
242
239
 
243
240
  | Variable | Required | Default | Description |
244
241
  |---|---|---|---|
245
- | `VAULT_PATH` | Local mode | — | Path to your Obsidian vault directory |
246
- | `COUCHDB_URL` | Remote mode | — | CouchDB server URL |
247
- | `COUCHDB_USER` | Remote mode | `admin` | CouchDB username |
248
- | `COUCHDB_PASSWORD` | Remote mode | `password` | CouchDB password |
249
- | `COUCHDB_DATABASE` | Remote mode | `obsidian` | CouchDB database name |
250
- | `COUCHDB_PASSPHRASE` | Remote mode | — | LiveSync E2E encryption passphrase (must match plugin setting) |
251
- | `VAULT_NAME` | Both | `MyVault` | Vault name for Obsidian deep links |
242
+ | `VAULT_PATH` | Filesystem mode | — | Path to your Obsidian vault directory |
243
+ | `COUCHDB_URL` | CouchDB mode | — | CouchDB server URL |
244
+ | `COUCHDB_USER` | CouchDB mode | `admin` | CouchDB username |
245
+ | `COUCHDB_PASSWORD` | CouchDB mode | — | CouchDB password (required) |
246
+ | `COUCHDB_DATABASE` | CouchDB mode | `obsidian` | CouchDB database name |
247
+ | `COUCHDB_PASSPHRASE` | CouchDB mode | — | LiveSync E2E encryption passphrase (must match plugin setting) |
248
+ | `VAULT_NAME` | Both | `MyVault` | Vault name (used for deep links and index storage) |
252
249
  | `MCP_AUTH_TOKEN` | Optional | — | Password for authentication |
253
250
  | `BASE_URL` | Optional | `http://localhost:PORT` | Public URL (for OAuth callbacks when using a tunnel) |
254
251
  | `PORT` | Optional | `8787` | HTTP port |
255
252
  | `HOST` | Optional | `0.0.0.0` | Bind address (`127.0.0.1` to restrict to localhost) |
256
253
  | `DATA_DIR` | Optional | `~/.obsidian-mcp` | Directory for persisted data (search index, auth tokens) |
254
+ | `LOG_LEVEL` | Optional | — | Set to `debug` for verbose logging (library logs, change feed, index sync) |
257
255
  | `MCP_REFRESH_DAYS` | Optional | `14` | Days before auth session expires |
258
256
 
259
- Set `VAULT_PATH` for local mode or `COUCHDB_URL` for remote mode.
257
+ Set `VAULT_PATH` for filesystem mode or `COUCHDB_URL` for CouchDB mode.
260
258
 
261
259
  ---
262
260
 
@@ -273,25 +271,25 @@ Set transport to **Streamable HTTP**, enter `http://localhost:8787/mcp`, and con
273
271
 
274
272
  ---
275
273
 
276
- ## Updating
274
+ ## How to update
277
275
 
278
276
  | How you run it | How to update |
279
277
  |---|---|
280
278
  | `npx obsidian-sync-mcp` | Automatic — npx pulls latest |
281
- | Fly.io (MCP only) | `fly deploy --image ghcr.io/es617/obsidian-sync-mcp:latest` |
282
- | Fly.io (full deploy) | Clone the repo, `fly deploy` from the repo root |
283
- | Docker Compose | `docker compose pull && docker compose up -d` |
279
+ | Fly.io | From the same directory where you ran setup: `fly deploy`. If you lost the fly.toml, run `fly config save --app your-app-name` to restore it. |
280
+ | Docker | `docker pull ghcr.io/es617/obsidian-sync-mcp:latest` and restart |
284
281
 
285
282
  ---
286
283
 
287
284
  ## Known limitations
288
285
 
289
286
  - **Single vault per instance.** Each server connects to one vault. For multiple vaults, run multiple instances on different ports.
290
- - **Search rebuilds on cold start.** The FlexSearch index persists to disk and rebuilds in ~200ms for 2000 notes. External edits are picked up automatically.
287
+ - **Single machine on Fly.io.** Auth state is in-memory, so multiple machines break the OAuth flow. The setup script enforces this automatically.
291
288
  - **No conflict resolution.** If an agent and Obsidian edit the same note simultaneously, last write wins.
292
289
  - **Text only.** Binary attachments are not exposed through MCP tools.
293
290
  - **Deep links depend on the client.** Obsidian `obsidian://` deep links are included in every tool response. They work on Claude Mobile and in browsers, but some clients (Claude Desktop) may not render them as clickable links.
294
291
  - **Node 22+ required.**
292
+ - **Setup script requires bash.** The `deploy/setup.sh` script works on macOS and Linux. On Windows, use WSL or Git Bash.
295
293
 
296
294
  ---
297
295
 
@@ -315,7 +313,7 @@ This software is provided as-is under the [MIT license](https://github.com/es617
315
313
  git clone --recursive https://github.com/es617/obsidian-sync-mcp.git
316
314
  cd obsidian-sync-mcp
317
315
  npm install && npm run build
318
- npm test # 79 unit tests
316
+ npm test # unit tests
319
317
  npm run test:e2e # integration tests
320
318
  ```
321
319
 
@@ -66,7 +66,48 @@ function parseFrontmatterAndLinks(content) {
66
66
  return { frontmatter, tags: [...tags], links: [...new Set(links)] };
67
67
  }
68
68
  function extractSnippet(content, query, context = 80) {
69
- const idx = content.toLowerCase().indexOf(query.toLowerCase());
69
+ const lower = content.toLowerCase();
70
+ let idx = lower.indexOf(query.toLowerCase());
71
+ if (idx === -1) {
72
+ const words = query.split(/\s+/).filter((w) => w.length >= 3).map((w) => w.toLowerCase());
73
+ if (words.length > 1) {
74
+ let bestStart = -1;
75
+ let bestLen = Infinity;
76
+ const first = words[0];
77
+ let pos = 0;
78
+ while (pos < lower.length) {
79
+ const start2 = lower.indexOf(first, pos);
80
+ if (start2 === -1) break;
81
+ let spanEnd = start2 + first.length;
82
+ let allFound = true;
83
+ for (let i = 1; i < words.length; i++) {
84
+ const wi = lower.indexOf(words[i], Math.max(0, start2 - 200));
85
+ if (wi === -1) {
86
+ allFound = false;
87
+ break;
88
+ }
89
+ spanEnd = Math.max(spanEnd, wi + words[i].length);
90
+ }
91
+ if (allFound) {
92
+ const spanStart = Math.min(start2, ...words.map((w) => lower.indexOf(w, Math.max(0, start2 - 200))).filter((i) => i >= 0));
93
+ const len = spanEnd - spanStart;
94
+ if (len < bestLen) {
95
+ bestStart = spanStart;
96
+ bestLen = len;
97
+ }
98
+ }
99
+ pos = start2 + 1;
100
+ }
101
+ if (bestStart >= 0 && bestLen <= 500) idx = bestStart;
102
+ }
103
+ }
104
+ if (idx === -1) {
105
+ const words = query.split(/\s+/).filter((w) => w.length >= 3).sort((a, b) => b.length - a.length);
106
+ for (const word of words) {
107
+ idx = lower.indexOf(word.toLowerCase());
108
+ if (idx !== -1) break;
109
+ }
110
+ }
70
111
  if (idx === -1) {
71
112
  return content.slice(0, 160) + (content.length > 160 ? "..." : "");
72
113
  }
@@ -80,4 +121,4 @@ export {
80
121
  parseFrontmatterAndLinks,
81
122
  extractSnippet
82
123
  };
83
- //# sourceMappingURL=chunk-TFJDIQK4.js.map
124
+ //# sourceMappingURL=chunk-GK3PAZZJ.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/parse.ts"],"sourcesContent":["/**\n * Parse Obsidian markdown content for frontmatter, tags, and links.\n */\n\nexport interface NoteMetadata {\n frontmatter: Record<string, string>;\n tags: string[];\n links: string[];\n}\n\nexport function parseFrontmatterAndLinks(content: string): NoteMetadata {\n const frontmatter: Record<string, any> = {};\n const tags = new Set<string>();\n const links: string[] = [];\n\n // Parse YAML frontmatter\n if (content.startsWith(\"---\\n\")) {\n const end = content.indexOf(\"\\n---\", 4);\n if (end !== -1) {\n const yaml = content.slice(4, end);\n let inTagsList = false;\n for (const line of yaml.split(\"\\n\")) {\n // Multi-line tags list item: \" - tagname\"\n if (inTagsList) {\n const listItem = line.match(/^\\s+-\\s+(.+)/);\n if (listItem) {\n const trimmed = listItem[1].trim();\n if (trimmed) tags.add(trimmed);\n continue;\n }\n inTagsList = false;\n }\n\n const match = line.match(/^(\\w[\\w-]*)\\s*:\\s*(.+)/);\n if (match) {\n frontmatter[match[1]] = match[2].trim();\n }\n // Frontmatter tags (inline: [a, b] or start of multi-line list)\n if (/^tags\\s*:/.test(line)) {\n const value = line.replace(/^tags\\s*:\\s*/, \"\").trim();\n if (value) {\n // Inline: tags: [a, b] or tags: a, b\n const tagValues = value.replace(/[[\\]]/g, \"\");\n for (const t of tagValues.split(\",\")) {\n const trimmed = t.trim();\n if (trimmed) tags.add(trimmed);\n }\n } else {\n // Multi-line list follows\n inTagsList = true;\n }\n }\n }\n }\n }\n\n // Inline #tags\n for (const match of content.matchAll(/(^|\\s)#([\\w/-]+)/g)) {\n tags.add(match[2]);\n }\n\n // [[wikilinks]]\n for (const match of content.matchAll(/\\[\\[([^\\]|]+)(?:\\|[^\\]]+)?\\]\\]/g)) {\n links.push(match[1]);\n }\n\n // [markdown links](path.md)\n for (const match of content.matchAll(/\\[([^\\]]+)\\]\\(([^)]+\\.md)\\)/g)) {\n links.push(match[2]);\n }\n\n return { frontmatter, tags: [...tags], links: [...new Set(links)] };\n}\n\nexport function extractSnippet(content: string, query: string, context = 80): string {\n const lower = content.toLowerCase();\n\n // Try exact phrase first\n let idx = lower.indexOf(query.toLowerCase());\n\n // Try to find the smallest span containing all query words\n if (idx === -1) {\n const words = query.split(/\\s+/).filter((w) => w.length >= 3).map((w) => w.toLowerCase());\n if (words.length > 1) {\n let bestStart = -1;\n let bestLen = Infinity;\n // For each occurrence of the first word, find the nearest span containing all words\n const first = words[0];\n let pos = 0;\n while (pos < lower.length) {\n const start = lower.indexOf(first, pos);\n if (start === -1) break;\n // Find last position needed to include all words from this start\n let spanEnd = start + first.length;\n let allFound = true;\n for (let i = 1; i < words.length; i++) {\n const wi = lower.indexOf(words[i], Math.max(0, start - 200));\n if (wi === -1) { allFound = false; break; }\n spanEnd = Math.max(spanEnd, wi + words[i].length);\n }\n if (allFound) {\n const spanStart = Math.min(start, ...words.map((w) => lower.indexOf(w, Math.max(0, start - 200))).filter((i) => i >= 0));\n const len = spanEnd - spanStart;\n if (len < bestLen) { bestStart = spanStart; bestLen = len; }\n }\n pos = start + 1;\n }\n if (bestStart >= 0 && bestLen <= 500) idx = bestStart;\n }\n }\n\n // Fall back to longest matching word\n if (idx === -1) {\n const words = query.split(/\\s+/).filter((w) => w.length >= 3).sort((a, b) => b.length - a.length);\n for (const word of words) {\n idx = lower.indexOf(word.toLowerCase());\n if (idx !== -1) break;\n }\n }\n\n if (idx === -1) {\n return content.slice(0, 160) + (content.length > 160 ? \"...\" : \"\");\n }\n const start = Math.max(0, idx - context);\n const end = Math.min(content.length, idx + query.length + context);\n return (start > 0 ? \"...\" : \"\") + content.slice(start, end) + (end < content.length ? \"...\" : \"\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAUO,SAAS,yBAAyB,SAA+B;AACpE,QAAM,cAAmC,CAAC;AAC1C,QAAM,OAAO,oBAAI,IAAY;AAC7B,QAAM,QAAkB,CAAC;AAGzB,MAAI,QAAQ,WAAW,OAAO,GAAG;AAC7B,UAAM,MAAM,QAAQ,QAAQ,SAAS,CAAC;AACtC,QAAI,QAAQ,IAAI;AACZ,YAAM,OAAO,QAAQ,MAAM,GAAG,GAAG;AACjC,UAAI,aAAa;AACjB,iBAAW,QAAQ,KAAK,MAAM,IAAI,GAAG;AAEjC,YAAI,YAAY;AACZ,gBAAM,WAAW,KAAK,MAAM,cAAc;AAC1C,cAAI,UAAU;AACV,kBAAM,UAAU,SAAS,CAAC,EAAE,KAAK;AACjC,gBAAI,QAAS,MAAK,IAAI,OAAO;AAC7B;AAAA,UACJ;AACA,uBAAa;AAAA,QACjB;AAEA,cAAM,QAAQ,KAAK,MAAM,wBAAwB;AACjD,YAAI,OAAO;AACP,sBAAY,MAAM,CAAC,CAAC,IAAI,MAAM,CAAC,EAAE,KAAK;AAAA,QAC1C;AAEA,YAAI,YAAY,KAAK,IAAI,GAAG;AACxB,gBAAM,QAAQ,KAAK,QAAQ,gBAAgB,EAAE,EAAE,KAAK;AACpD,cAAI,OAAO;AAEP,kBAAM,YAAY,MAAM,QAAQ,UAAU,EAAE;AAC5C,uBAAW,KAAK,UAAU,MAAM,GAAG,GAAG;AAClC,oBAAM,UAAU,EAAE,KAAK;AACvB,kBAAI,QAAS,MAAK,IAAI,OAAO;AAAA,YACjC;AAAA,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