MCPcopy Create free account
hub / github.com/ZenNotes/zennotes / hydrateSearchOffsets

Function hydrateSearchOffsets

apps/desktop/src/main/vault.ts:2311–2359  ·  view source on GitHub ↗
(
  root: string,
  query: string,
  candidates: VaultTextSearchCandidate[]
)

Source from the content-addressed store, hash-verified

2309}
2310
2311async function hydrateSearchOffsets(
2312 root: string,
2313 query: string,
2314 candidates: VaultTextSearchCandidate[]
2315): Promise<VaultTextSearchMatch[]> {
2316 const bodyCache = new Map<string, string>()
2317 return await Promise.all(
2318 candidates.map(async (candidate) => {
2319 if (typeof candidate.offset === 'number') {
2320 const rawPath = resolveSafe(root, candidate.path)
2321 let body = bodyCache.get(candidate.path)
2322 if (body == null) {
2323 body = await fs.readFile(rawPath, 'utf8')
2324 bodyCache.set(candidate.path, body)
2325 }
2326 const rawLine = body.split('\n')[candidate.lineNumber - 1] ?? ''
2327 return {
2328 path: candidate.path,
2329 title: candidate.title,
2330 folder: candidate.folder,
2331 lineNumber: candidate.lineNumber,
2332 offset: candidate.offset + Math.max(0, Math.min(firstMatchColumn(query, rawLine), rawLine.length)),
2333 lineText: candidate.lineText
2334 }
2335 }
2336
2337 const abs = resolveSafe(root, candidate.path)
2338 let body = bodyCache.get(candidate.path)
2339 if (body == null) {
2340 body = await fs.readFile(abs, 'utf8')
2341 bodyCache.set(candidate.path, body)
2342 }
2343 const lines = body.split('\n')
2344 let lineOffset = 0
2345 for (let index = 0; index < candidate.lineNumber - 1; index += 1) {
2346 lineOffset += (lines[index] ?? '').length + 1
2347 }
2348 const rawLine = lines[candidate.lineNumber - 1] ?? ''
2349 return {
2350 path: candidate.path,
2351 title: candidate.title,
2352 folder: candidate.folder,
2353 lineNumber: candidate.lineNumber,
2354 offset: lineOffset + Math.max(0, Math.min(firstMatchColumn(query, rawLine), rawLine.length)),
2355 lineText: candidate.lineText
2356 }
2357 })
2358 )
2359}
2360
2361async function readMeta(
2362 root: string,

Callers 1

searchVaultTextFunction · 0.85

Calls 2

resolveSafeFunction · 0.70
firstMatchColumnFunction · 0.70

Tested by

no test coverage detected