Skip to content

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

Closed

Description

@malibio

Overview

When clicking a nodespace://uuid link where the target is a non-root node (e.g., a heading within a document), NodeSpace should navigate to the target's root ancestor and scroll the viewer to the target node's position. Currently, non-root node links either fail to navigate meaningfully or open the child node out of context.

Additionally, markdown #anchor links (e.g., [Section](#section-a)) are preserved as-is during import instead of being resolved to nodespace://uuid links pointing to the heading node created from that section.

Problem Statement

Current behavior:

  • nodespace://child-uuid navigates to the child node directly, losing parent document context
  • #anchor links in imported markdown are preserved verbatim but have no click handling — NodeSpace is not a markdown viewer, headings are separate nodes

Desired behavior:

  • nodespace://child-uuid → resolve to root ancestor → navigate to root (or stay if already viewing it) → scroll viewer to child node position
  • #anchor links → resolved to nodespace://heading-uuid during import, then handled by the generic navigation above

Implementation Approach

Phase 1: Frontend — Smart navigation for non-root node links

Modify NavigationService.navigateToNode() to detect non-root targets and handle them:

  1. Resolve the target node and walk up structureTree.getParent() to find the root ancestor
  2. If the current tab is already showing that root node → just scroll to [data-node-id="uuid"] (no reload, no navigation)
  3. If showing a different root → navigate to the root node, then scroll to the target child after the viewer renders

Key considerations:

  • Scroll timing: must wait for viewer to render child nodes before calling scrollIntoView()
  • This is generic behavior for ALL non-root node links, not specific to headers

Phase 2: Rust — Resolve anchor links to nodespace:// during markdown import

Modify the anchor link handling in transform_single_link_with_target():

  1. Build a slug-to-UUID map from heading PreparedNodes in the current document (slugify heading content using GitHub-style slugification → node UUID)
  2. When a #anchor link is encountered, look up the slug in the map
  3. If found, transform to nodespace://uuid (same as inter-file links)
  4. If not found, preserve as plain text (dead anchor)

The heading nodes and their pre-assigned UUIDs are already available in PrepareContext during link transformation.

Acceptance Criteria

  • Clicking nodespace:// link to a non-root node navigates to its root ancestor and scrolls to the target
  • If already viewing the root node, no reload occurs — just smooth scroll to target
  • If viewing a different node, navigation to root occurs first, then scroll after render
  • Works for any non-root node type (headers, text, tasks, etc.)
  • Markdown #anchor links are resolved to nodespace://uuid during import when a matching heading node exists
  • Unresolved anchors (no matching heading) become plain text
  • Existing nodespace:// link behavior for root nodes is unchanged
  • Tests pass (bun run test:all)
  • Code passes bun run quality:fix

Technical Specifications

Reference Files

  • NavigationService: packages/desktop-app/src/lib/services/navigation-service.ts — navigateToNode() and resolveNodeTarget() need modification
  • App Shell click handler: packages/desktop-app/src/lib/components/layout/app-shell.svelte (line 378) — global nodespace:// link interception
  • BaseNode DOM: packages/desktop-app/src/lib/design/components/base-node.svelte (line 819) — nodes have data-node-id={nodeId} attribute
  • SharedNodeStore: packages/desktop-app/src/lib/services/shared-node-store.svelte.ts — structureTree.getParent() for walking up hierarchy, findContainer() pattern for root resolution
  • Markdown link transformer: packages/core/src/mcp/handlers/markdown.rs (line 314) — #anchor handling in transform_single_link_with_target()
  • Markdown tests: packages/core/src/mcp/handlers/markdown_test.rs — existing anchor link tests to update
  • PrepareContext: packages/core/src/mcp/handlers/markdown.rs (line 108) — heading stack with node IDs and levels

Implementation Details

Frontend root resolution pattern (from existing findContainer in SharedNodeStore):

// Walk up structureTree to find root
let currentId = targetNodeId;
while (true) {
  const parentId = structureTree.getParent(currentId);
  if (!parentId || parentId === '__root__') break;
  currentId = parentId;
}
// currentId is now the root node

Scroll after navigation — need to handle async render timing:

// After navigation completes, scroll to target node
requestAnimationFrame(() => {
  const el = document.querySelector(`[data-node-id="${targetNodeId}"]`);
  el?.scrollIntoView({ behavior: 'smooth', block: 'center' });
});

Rust slug generation — GitHub-style heading slugification:

fn slugify_heading(content: &str) -> String {
    content.to_lowercase()
        .replace(|c: char| !c.is_alphanumeric() && c != ' ' && c != '-', "")
        .replace(' ', "-")
}

Non-Goals

  • Custom scroll offset or highlight animation for the target node (can be added later)
  • Deep-linking via URL fragment (e.g., nodespace://root-uuid#child-uuid)
  • Anchor link support in the editor (edit mode) — this is import and view mode only

Activity

  1. malibio commented on Feb 25, 2026

    @malibio
    CollaboratorAuthor

    Test baseline: Frontend 3586 passed (108 files), Rust 25 passed (0 failed)

  2. self-assigned this
    on Feb 25, 2026
  3. added a commit that references this issue on Feb 25, 2026
    cdde14c
  4. added 2 commits that reference this issue on Feb 25, 2026
    606c7b9
    0ed4000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

backendenhancementfrontend

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions