Skip to content

Smart navigation for non-root node links with scroll-to-target - #910

Merged
malibio merged 2 commits into
mainfrom
feature/issue-909-smart-navigation-scroll
Feb 25, 2026
Merged

malibio merged 2 commits into
mainfrom
feature/issue-909-smart-navigation-scroll

Conversation

@malibio

@malibio malibio commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator

Closes #909

… 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 malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 MutationObserver on 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--c vs a-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 malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@malibio

malibio commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator Author

Review Recommendations Addressed

All 7 recommendations from the code review have been addressed in commit c9482ed:

✅ Addressed (7/7)

# Finding Action
1 🟡 scrollToNode reliability (single 150ms retry) Replaced with 3-attempt retry loop at 100ms, 250ms, 500ms intervals
2 🟡 querySelector XSS defense Added CSS.escape(nodeId) for defense-in-depth; added CSS to eslint globals
3 🟢 Duplicated root-resolution logic (DRY) Extracted resolveWithRootAncestor() private helper used by both navigation methods
4 🟢 Misleading duplicate heading comment Updated comment to clearly note the limitation vs GitHub's -1 suffix behavior
5 🟢 Missing slugify_heading unit tests Added 8 direct unit tests covering: basic, no-hash, special chars, numbers, hyphens, multi-level, empty, emoji
6 🟢 Nit: file comment says "node://" Fixed to "nodespace://"
7 🟢 Nit: defensive trim_start_matches comment Added comment explaining the defensive behavior

⏭️ Skipped: 0

All recommendations addressed.

📊 Stats

  • Commits: 1
  • Files changed: 4 (navigation-service.ts, markdown.rs, markdown_test.rs, eslint.config.js)
  • Tests: All passing (3586 frontend, Rust anchor + slugify tests)
  • Quality: ESLint, svelte-check, clippy all clean

@malibio
malibio merged commit 606c7b9 into main Feb 25, 2026
@malibio
malibio deleted the feature/issue-909-smart-navigation-scroll branch February 25, 2026 16:17
malibio added a commit that referenced this pull request Feb 26, 2026
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Smart navigation for non-root node links with scroll-to-target

1 participant