Skip to content

Convert view mode HTML rendering from $effect to $derived #625

Closed

Description

@malibio

Overview

Replace the imperative $effect that sets innerHTML with a $derived expression that computes the HTML string declaratively, letting Svelte handle the DOM update.

Problem Statement

Current implementation sets innerHTML imperatively in an effect:

// Lines 742-793 in base-node.svelte
$effect(() => {
  if (!isEditing && viewElement) {
    let processedContent = displayContent ?? content;
    // ... markdown processing logic ...
    viewElement.innerHTML = html;  // Imperative DOM manipulation
  }
});

This is a classic "derived state as effect" anti-pattern. The HTML is purely derived from content, displayContent, metadata, and isEditing - it should be a $derived value.

Proposed Solution

Convert to declarative $derived:

// Compute HTML declaratively
let viewHtml = $derived.by(() => {
  if (isEditing) return '';
  
  let processedContent = displayContent ?? content;
  const disableMarkdown = metadata?.disableMarkdown === true;
  
  if (disableMarkdown) {
    return processedContent.split('\n').map(line => line || '').join('<br>');
  }
  
  // Markdown processing...
  return html;
});

Then in template, use {@html viewHtml} with proper sanitization, or better yet, render structured content via a dedicated component.

Acceptance Criteria

  • View mode HTML computed via $derived.by() instead of $effect
  • One $effect eliminated from base-node.svelte
  • View mode rendering works identically (markdown, line breaks, etc.)
  • No XSS vulnerabilities introduced (content already trusted/sanitized)
  • Code passes bun run quality:fix
  • Tests pass (bun run test:all)

Technical Specifications

Files to Modify

  • base-node.svelte: packages/desktop-app/src/lib/design/components/base-node.svelte - Lines 742-793

Current Effect Logic

  1. Check if not editing and viewElement exists
  2. Process content (displayContent or content)
  3. Handle disableMarkdown flag
  4. Process blank lines with placeholders
  5. Run markdownToHtml
  6. Preserve leading/trailing newlines
  7. Set innerHTML

Migration Approach

  1. Extract HTML computation to $derived.by()
  2. Return empty string when editing
  3. Use {@html viewHtml} in template OR structured rendering
  4. Remove the $effect block entirely

Testing Requirements

  • Verify markdown renders correctly in view mode
  • Verify blank lines preserved
  • Verify leading/trailing newlines preserved
  • Verify code blocks render with disableMarkdown
  • Verify edit/view transition works smoothly

Related Issues

Activity

  1. self-assigned this
    on Nov 28, 2025
  2. added a commit that references this issue on Nov 28, 2025
    d2f2f81
  3. added a commit that references this issue on Nov 28, 2025
    9324d85
  4. added 2 commits that reference this issue on Feb 4, 2026
    1e41a39
    b2b5834
  5. added a commit that references this issue on Feb 26, 2026
    d9a0bc2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions