Attach exact Obsidian wikilink matches to extracted reference candidates.
(candidates: list[dict[str, Any]], config: dict[str, Any])
| 352 | |
| 353 | |
| 354 | def resolve_reference_links(candidates: list[dict[str, Any]], config: dict[str, Any]) -> list[dict[str, Any]]: |
| 355 | """Attach exact Obsidian wikilink matches to extracted reference candidates.""" |
| 356 | index = build_vault_note_index(config) |
| 357 | if index.get("status") != "ok": |
| 358 | return [ |
| 359 | { |
| 360 | **candidate, |
| 361 | "wikilink": "", |
| 362 | "vault_target": "", |
| 363 | "match_status": "vault_unavailable", |
| 364 | "match_reason": "none", |
| 365 | } |
| 366 | for candidate in candidates |
| 367 | if isinstance(candidate, dict) |
| 368 | ] |
| 369 | |
| 370 | notes = [note for note in index.get("notes", []) if isinstance(note, dict)] |
| 371 | |
| 372 | matched: list[dict[str, Any]] = [] |
| 373 | for candidate in candidates: |
| 374 | if not isinstance(candidate, dict): |
| 375 | continue |
| 376 | raw_text = str(candidate.get("raw_text", "") or candidate.get("display_text", "")) |
| 377 | display_text = normalize_whitespace(str(candidate.get("display_text", ""))) or raw_text |
| 378 | candidate_text = normalize_whitespace(f"{raw_text} {display_text}") |
| 379 | resolved = dict(candidate) |
| 380 | resolved.setdefault("match_status", "no_vault_match") |
| 381 | resolved.setdefault("match_reason", "none") |
| 382 | resolved.setdefault("wikilink", "") |
| 383 | resolved.setdefault("vault_target", "") |
| 384 | |
| 385 | priority_matches: list[tuple[str, list[dict[str, Any]]]] = [] |
| 386 | doi = _candidate_doi(candidate, candidate_text) |
| 387 | if doi: |
| 388 | priority_matches.append( |
| 389 | ("doi", [note for note in notes if doi and doi == str(note.get("doi_norm", ""))]) |
| 390 | ) |
| 391 | arxiv_ids = _candidate_arxiv_ids(candidate, candidate_text) |
| 392 | if arxiv_ids: |
| 393 | priority_matches.append( |
| 394 | ( |
| 395 | "arxiv_id", |
| 396 | [ |
| 397 | note |
| 398 | for note in notes |
| 399 | if arxiv_ids.intersection(set(note.get("arxiv_ids", []) or [])) |
| 400 | ], |
| 401 | ) |
| 402 | ) |
| 403 | priority_matches.append( |
| 404 | ( |
| 405 | "basename_or_title_or_alias", |
| 406 | [ |
| 407 | note |
| 408 | for note in notes |
| 409 | if any( |
| 410 | _contains_key(candidate_text, key) |
| 411 | for key in note.get("text_keys", []) or [] |