( root: string, query: string, candidates: VaultTextSearchCandidate[] )
| 2309 | } |
| 2310 | |
| 2311 | async 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 | |
| 2361 | async function readMeta( |
| 2362 | root: string, |
no test coverage detected