← Corpus / lost-in-public / other

Refactor Plan: Consolidate Callout Processing to Pipeline Architecture

Path
refactors/Extended-Markdown-Rendering-Consolidation.md

Non-Destructive Refactor Plan: Callout Processing Consolidation

Goal

Consolidate callout processing into the four-phase pipeline architecture while preserving existing code for reference and rollback capability.

Current State

We have two parallel implementations:

  1. Monolithic approach in remark-callout.ts
  2. Four-phase pipeline in callouts/ directory

Refactor Steps

1. Archive Current Implementation

# Create archive directory in site-archive submodule
mkdir -p site-archive/2025-04-03/markdown-processing/

# Copy files to archive with clear documentation
cp site/src/utils/markdown/plugins/remark-callout.ts \
   site-archive/2025-04-03/markdown-processing/remark-callout.original.ts

cp site/src/types/remark-callout-handler.d.ts \
   site-archive/2025-04-03/markdown-processing/remark-callout-handler.original.d.ts

# Add README explaining the archived code
touch site-archive/2025-04-03/markdown-processing/README.md

2. Extract Reusable Components

Before removing any code, extract valuable patterns from the monolithic implementation:

  1. Detection Pattern:
// Copy regex pattern to calloutCases.ts
const CALLOUT_PATTERN = /^\[!(\w+)\](?:\s+(.+))?/;
  1. Type Definitions:
// Merge relevant types into calloutTypes.ts
export interface CalloutNode {
  type: 'callout';
  calloutType: string;
  title?: string;
  children: Array<any>;
  data: {
    hName: string;
    hProperties: {
      className: string[];
      'data-type': string;
      'data-title': string;
    };
  };
}

3. Enhance Pipeline Implementation

  1. Update Detection Phase:
// In detectMarkdownCallouts.ts
import { CALLOUT_PATTERN } from './calloutCases';

export function detectCallouts(tree: Node): CalloutCandidate[] {
  const candidates: CalloutCandidate[] = [];
  
  visit(tree, 'blockquote', (node: Blockquote) => {
    const match = CALLOUT_PATTERN.exec(/* ... */);
    if (match) {
      candidates.push({
        node,
        type: match[1],
        title: match[2]
      });
    }
  });

  return candidates;
}
  1. Add Debug Points:
// In processCalloutPipeline.ts
astDebugger.writeDebugFile('callouts-detection', {
  phase: 'detect',
  candidates: detected.length,
  patterns: detected.map(d => d.type)
});

4. Create Deprecation Notice

// In remark-callout.ts
/**
 * @deprecated This monolithic implementation has been replaced by the 
 * four-phase pipeline in ../callouts/. See processCalloutPipeline.ts
 * for the new implementation.
 * 
 * Original code archived in site-archive/2025-04-03/markdown-processing/
 */

5. Update Documentation

  1. Add Migration Guide:
# Callout Processing Migration
- Previous: Single-pass transformation in remark-callout.ts
- New: Four-phase pipeline in callouts/
  1. Detection (detectMarkdownCallouts.ts)
  2. Isolation (isolateCalloutContent.ts)
  3. Transform (transformCalloutStructure.ts)
  4. Embed (embedCalloutNodes.ts)
  1. Update Debug Documentation:
Debug output points:
1. callouts-detection.json
2. callouts-isolation.json
3. callouts-transform.json
4. callouts-embed.json
5. callouts-complete.json

Benefits

  1. Single source of truth in pipeline architecture
  2. Better debugging through phase isolation
  3. Clearer separation of concerns
  4. Original code preserved in archive
  5. Non-destructive transition path

Validation Steps

  1. Run existing test suite against pipeline
  2. Compare debug output between implementations
  3. Verify all callout types are handled
  4. Check HTML output matches original

Rollback Plan

  1. Code preserved in site-archive
  2. Deprecation notice includes migration path
  3. Types and patterns extracted for reuse

Post-Mortem: Breaking Change

[!ERROR] Failed to Follow Plan We deviated from our own refactoring plan by:

  1. Deleting files immediately without archiving
  2. Not adding deprecation notices
  3. Making breaking changes to astro.config.mjs without testing

Impact

  • Build failed due to missing remark-callout-handler.ts
  • No rollback path available
  • Lost original implementation reference

Recovery Steps

  1. Restored proper pipeline implementation in detectMarkdownCallouts.ts
  2. Updated Astro config to use new pipeline
  3. Documented failure in this log

Lessons Learned

  1. ALWAYS FOLLOW THE REFACTOR PLAN
  2. Never delete files without archiving
  3. Use deprecation notices for breaking changes
  4. Test configuration changes before removing old code

Next Steps

  1. Create proper archive in site-archive
  2. Add comprehensive tests for new pipeline
  3. Document the new implementation thoroughly