Repository navigation
Smart navigation for non-root node links with scroll-to-target - #910
Conversation
… during import (closes #909) Phase 1 (Frontend): NavigationService now detects non-root node targets, walks up the structureTree to find the root ancestor, and navigates to it while scrolling to the target child. If already viewing the root, just scrolls without reload. Works for all navigation methods. Phase 2 (Rust): Anchor links (#section-slug) in imported markdown are now resolved to nodespace://uuid links using GitHub-style heading slugification. Unresolved anchors become plain text. Co-Authored-By: Claude <[email protected]>
malibio
left a comment
There was a problem hiding this comment.
Code Review Summary
Verdict: Net positive change. This PR cleanly implements two complementary features -- frontend smart navigation for non-root node links, and Rust-side anchor link resolution during markdown import. The architecture is sound: Phase 2 (Rust) resolves #anchor links to nodespace://uuid at import time, and Phase 1 (Frontend) handles navigating to non-root node UUIDs by resolving root ancestors and scrolling to the target.
The code is well-structured, properly documented, and the Rust test coverage is thorough. A few items worth discussing below.
Findings
Suggested Improvements
1. [Improvement] scrollToNode reliability -- single 150ms retry may be fragile
/packages/desktop-app/src/lib/services/navigation-service.ts (lines 154-171)
The scroll-to-target mechanism uses requestAnimationFrame + a single 150ms setTimeout retry. This works for most cases, but if the viewer loads nodes asynchronously (e.g., lazy-loaded children from the backend), 150ms may not be sufficient. Consider one of:
- A small polling loop (e.g., 3 attempts at 100ms intervals) with exponential backoff
- A
MutationObserveron the scroll container that resolves when the target[data-node-id]appears
This is not a blocker for merge -- the current approach handles the common case. But if users report scroll-to misses on slow machines or large documents, this is the place to look.
2. [Improvement] scrollToNode -- potential XSS via nodeId in querySelector
/packages/desktop-app/src/lib/services/navigation-service.ts (line 156)
const el = document.querySelector(`[data-node-id="${nodeId}"]`);The nodeId is a UUID coming from the backend (trusted source), so this is low risk in practice. However, if a maliciously crafted UUID were ever injected (e.g., "]), it could break the selector or cause unexpected behavior. A defensive measure would be to validate UUID format before interpolation:
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
if (!UUID_RE.test(nodeId)) return;Or use CSS.escape(nodeId). Not a blocker given the trusted source, but worth noting for defense-in-depth.
3. [Improvement] Duplicated root-resolution logic across navigateToNode and navigateToNodeInOtherPane
/packages/desktop-app/src/lib/services/navigation-service.ts (lines 196-212 and 286-301)
The root ancestor resolution + target re-resolution block is duplicated verbatim between both navigation methods. Consider extracting this into a private helper:
private async resolveNavigationTarget(nodeId: string): Promise<{
target: NavigationTarget;
navNodeId: string;
navNodeType: string;
navTitle: string;
isNonRoot: boolean;
} | null> { ... }This follows DRY and ensures both paths stay in sync as the logic evolves. Low priority but would reduce maintenance surface.
4. [Improvement] slugify_heading does not handle GitHub's duplicate heading disambiguation
/packages/core/src/mcp/handlers/markdown.rs (lines 349-359)
GitHub appends -1, -2, etc. to duplicate heading slugs (e.g., two ## Setup headings become #setup and #setup-1). The current build_slug_to_uuid_map uses entry().or_insert_with() which correctly gives first-wins behavior, but markdown authors using [link](#setup-1) to reference the second duplicate heading will get an unresolved anchor (plain text).
This is an acceptable trade-off for Phase 1 -- the comment "First heading with this slug wins (matches GitHub behavior)" is slightly misleading since GitHub does create the -1 variant. Consider adding a brief comment noting this limitation, or a // TODO: Handle duplicate heading disambiguation (#setup-1) for future reference. Not a blocker.
5. [Improvement] Missing unit tests for slugify_heading edge cases
/packages/core/src/mcp/handlers/markdown_test.rs
The slugify_heading function is only tested implicitly through the integration tests. Consider adding direct unit tests for edge cases:
- Headings with special characters:
# Hello, World!->hello-world - Headings with emoji:
# 🚀 Launch->-launch(or should emoji be stripped?) - Headings with consecutive spaces/hyphens:
# A B--C->a--b--cvsa-b-c - Empty heading after stripping:
# ###->""
These would document the exact slugification behavior and catch regressions if the implementation changes.
Nitpicks
Nit: /packages/desktop-app/src/lib/services/navigation-service.ts (line 2) -- The file-level comment still says "node:// link navigation" but the protocol is nodespace://. Minor but could confuse future readers.
Nit: /packages/core/src/mcp/handlers/markdown.rs (line 357) -- The slugify_heading function strips # characters via trim_start_matches('#') but the node content stored in PreparedNode may or may not include the # prefix depending on the parser. This works because trim_start_matches is a no-op if there are no # characters, but a brief comment noting this defensive behavior would help future readers.
Acceptance Criteria Check
| Criterion | Status |
|---|---|
| Clicking nodespace:// link to non-root node navigates to root + scrolls | Implemented |
| If already viewing root, no reload -- just scroll | Implemented (line 246) |
| If viewing different node, navigate then scroll | Implemented |
| Works for any non-root node type | Yes -- generic via structureTree |
| Markdown #anchor links resolved to nodespace://uuid during import | Implemented |
| Unresolved anchors become plain text | Implemented + tested |
| Existing nodespace:// behavior for root nodes unchanged | Yes -- isNonRoot guard |
| Tests pass | Rust tests updated; no frontend tests added (see note below) |
| Code passes quality:fix | Not verified in this review |
Note on frontend test coverage: No new unit tests were added for the NavigationService changes (findRootAncestor, scrollToNode, the root-resolution logic in navigateToNode). The Rust side has good test coverage. Frontend tests for navigation logic would strengthen confidence, particularly for the "already viewing root" optimization path. This is acceptable for a first pass but worth tracking.
Overall: Approve with minor suggestions. The implementation is clean, well-documented, and correctly addresses both phases of the issue. The suggestions above are improvements, not blockers.
malibio
left a comment
There was a problem hiding this comment.
Review status: APPROVE (advisory) -- cannot formally approve own PR via GitHub. Net positive change; see detailed review comment above for suggested improvements (none are blockers).
- Improve scrollToNode with CSS.escape() and retry polling (3 attempts) - Extract resolveWithRootAncestor() to eliminate duplicated root-resolution - Add 8 unit tests for slugify_heading edge cases - Fix misleading comment about duplicate heading disambiguation - Add defensive behavior comment on trim_start_matches - Fix file-level comment: "node://" → "nodespace://" - Add CSS to eslint globals for CSS.escape() usage Co-Authored-By: Claude <[email protected]>
Review Recommendations AddressedAll 7 recommendations from the code review have been addressed in commit c9482ed: ✅ Addressed (7/7)
⏭️ Skipped: 0All recommendations addressed. 📊 Stats
|
* Add smart navigation for non-root node links and resolve anchor links during import (closes #909) Phase 1 (Frontend): NavigationService now detects non-root node targets, walks up the structureTree to find the root ancestor, and navigates to it while scrolling to the target child. If already viewing the root, just scrolls without reload. Works for all navigation methods. Phase 2 (Rust): Anchor links (#section-slug) in imported markdown are now resolved to nodespace://uuid links using GitHub-style heading slugification. Unresolved anchors become plain text. Co-Authored-By: Claude <[email protected]> * Address review: improve scroll reliability, DRY refactor, slugify tests - Improve scrollToNode with CSS.escape() and retry polling (3 attempts) - Extract resolveWithRootAncestor() to eliminate duplicated root-resolution - Add 8 unit tests for slugify_heading edge cases - Fix misleading comment about duplicate heading disambiguation - Add defensive behavior comment on trim_start_matches - Fix file-level comment: "node://" → "nodespace://" - Add CSS to eslint globals for CSS.escape() usage Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
Closes #909