Extract comprehensive wikilink processing functionality into separate module: - Move processWikilinks() method with complete implementation - Extract sanitizeFilePath() as public method for reuse by embed processors - Move sanitizeHeadingForLink() and resolveRelativePath() methods - Remove duplicate WikilinkConfig interface - Update all embed processors to use WikilinkProcessor.sanitizeFilePath() - Add WikilinkProcessor configuration sync in updateConfig() - Preserve all existing functionality and error handling Reduces MarkdownPreprocessor from 895 to 816 lines (-79 lines). WikilinkProcessor: 172 lines of focused wikilink processing logic. Build passes successfully with no breaking changes. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
9.7 KiB
Typst PDF Export Plugin Refactoring Plan
CRITICAL SAFETY REQUIREMENTS ⚠️
Previous refactor broke workflows - We must be EXTREMELY careful
- STOP after EVERY file creation for verification
- STOP after EVERY file modification for verification
- NO jumping ahead - wait for explicit confirmation
- Use Context7 for Obsidian and TypeScript API documentation
- Test build after EVERY change
Overview
Refactoring 6 large files (200-1800 lines) into smaller, focused modules to improve maintainability and reduce complexity.
Files Requiring Refactoring
- main.ts - 1792 lines → ~200 lines + 6 modules
- MarkdownPreprocessor.ts - 1235 lines → ~200 lines + 5 modules
- PandocTypstConverter.ts - 741 lines → ~200 lines + 4 modules
- ExportConfigModal.ts - 492 lines → ~200 lines + 3 modules
- PdfToImageConverter.ts - 444 lines → ~150 lines + 3 modules
- DependencyChecker.ts - 319 lines → ~170 lines + 2 modules
PHASE 1: Refactor main.ts (MOST CRITICAL)- DONE
The main.ts file is the plugin entry point and most critical to get right.
Step 1.1: Create PluginLifecycle.ts - DONE
Location: src/plugin/PluginLifecycle.ts (~150 lines)
Extract:
onload()methodonunload()method- Template initialization logic
- Settings loading/saving
- Cleanup on startup
STOP → Verify: npm run build passes
STOP → Test: Plugin loads in Obsidian without errors
Step 1.2: Create CommandRegistry.ts - DONE
Location: src/plugin/CommandRegistry.ts (~150 lines)
Extract:
registerCommands()method- All command handler methods
- Command definitions and callbacks
STOP → Verify: npm run build passes
STOP → Test: Commands appear and work in command palette
Step 1.3: Create EventHandlers.ts - DONE
Location: src/plugin/EventHandlers.ts (~150 lines)
Extract:
registerEventHandlers()methodhandleRibbonClick()method- File menu integration
- Context menu handlers
STOP → Verify: npm run build passes
STOP → Test: Context menus and ribbon icon work
Step 1.4: Create ExportOrchestrator.ts
Location: src/plugin/ExportOrchestrator.ts (~400 lines)
Extract:
exportFile()methodexportFiles()methodexportFileWithConfig()methodhandleFolderExport()method- Export workflow coordination
STOP → Verify: npm run build passes
STOP → Test: Basic single note export works
Step 1.5: Create FontManager.ts - DONE
Location: src/plugin/FontManager.ts (~150 lines)
Extract:
cacheAvailableFonts()methodgetCachedFonts()method- Font discovery and caching logic
STOP → Verify: npm run build passes
STOP → Test: Font detection works in settings
Step 1.6: Create PathResolver.ts - DONE
Location: src/plugin/PathResolver.ts (~200 lines)
Extract:
resolveExecutablePath()methodprepareOutputPath()method- Path validation utilities
- Executable finding logic
STOP → Verify: npm run build passes
STOP → Test: Pandoc/Typst detection works
Step 1.7: Update main.ts - DONE
Final size: ~200 lines Keep:
- Plugin class declaration
- Settings property
- Core Obsidian plugin methods
- Delegation to modules
STOP → Full integration test of all functionality
CHECKPOINT: Complete test of all Phase 1 functionality - DONE
PHASE 2: Refactor MarkdownPreprocessor.ts - IN PROGRESS
Step 2.1: Create FrontmatterProcessor.ts - DONE
Location: src/converters/preprocessors/FrontmatterProcessor.ts (~200 lines)
Extract:
processFrontmatter()methodformatFrontmatterForDisplay()method- Frontmatter parsing logic
STOP → Verify build → Test frontmatter parsing works
Step 2.2: Create WikilinkProcessor.ts - DONE
Location: src/converters/preprocessors/WikilinkProcessor.ts (~150 lines)
Extract:
parseWikilinks()methodsanitizeFilePath()methodsanitizeHeadingForLink()methodresolveRelativePath()method
STOP → Verify build → Test wikilink conversion works
Step 2.3: Create EmbedProcessor.ts - IN PROGRESS
Location: src/converters/preprocessors/EmbedProcessor.ts (~250 lines)
Extract:
parseEmbeds()methodprocessImageEmbed()methodprocessVideoEmbed()methodprocessAudioEmbed()methodprocessPdfEmbed()methodprocessFileEmbed()method
STOP → Verify build → Test all embed types work
Step 2.4: Create CalloutProcessor.ts
Location: src/converters/preprocessors/CalloutProcessor.ts (~200 lines)
Extract:
parseCallouts()methodparseEmailBlocks()methodprocessMultiLineCallouts()methodprocessEmailBlock()method
STOP → Verify build → Test callouts and email blocks work
Step 2.5: Create MetadataExtractor.ts
Location: src/converters/preprocessors/MetadataExtractor.ts (~150 lines)
Extract:
extractTags()methodextractTitle()methodcalculateWordCount()method- Metadata processing utilities
STOP → Verify build → Test metadata extraction works
Step 2.6: Update MarkdownPreprocessor.ts
Final size: ~200 lines Keep:
- Main
preprocess()method - Orchestration logic
- Result compilation
STOP → Full preprocessing test with various note types
CHECKPOINT: Test various note types with different content
PHASE 3: Refactor PandocTypstConverter.ts
Step 3.1: Create PandocCommandBuilder.ts
Location: src/converters/pandoc/PandocCommandBuilder.ts (~200 lines)
Extract:
- Command building logic
- Argument construction
- Template handling
STOP → Verify build → Test Pandoc command generation
Step 3.2: Create TypstVariableMapper.ts
Location: src/converters/pandoc/TypstVariableMapper.ts (~200 lines)
Extract:
- Variable mapping logic
- Settings to Typst conversion
- Font and layout variable handling
STOP → Verify build → Test variable application in templates
Step 3.3: Create ResourcePathResolver.ts
Location: src/converters/pandoc/ResourcePathResolver.ts (~150 lines)
Extract:
resolveResourcePaths()method- Path caching logic
- Resource discovery
STOP → Verify build → Test resource resolution works
Step 3.4: Update PandocTypstConverter.ts
Final size: ~200 lines Keep:
- Main conversion method
- Result processing
- Error handling
STOP → Full conversion test with various settings
CHECKPOINT: Test PDF generation with various settings
PHASE 4: Refactor ExportConfigModal.ts
Step 4.1: Create ModalRenderer.ts
Location: src/ui/modal/ModalRenderer.ts (~150 lines)
Extract:
createHeader()methodrenderSections()methodcreateProgressContainer()methodcreateActionButtons()method
STOP → Verify build → Test modal displays correctly
Step 4.2: Create ModalValidator.ts
Location: src/ui/modal/ModalValidator.ts (~100 lines)
Extract:
validateAllSections()methodshowValidationErrors()methodshowValidationWarnings()method- Validation logic
STOP → Verify build → Test validation works
Step 4.3: Update ExportConfigModal.ts
Final size: ~200 lines Keep:
- Modal lifecycle methods
- Event handlers
- State management
STOP → Full modal functionality test
CHECKPOINT: Test all modal interactions
PHASE 5: Refactor PdfToImageConverter.ts
Step 5.1: Create PdfProcessor.ts
Location: src/converters/pdf/PdfProcessor.ts (~150 lines)
Extract:
- Core PDF processing logic
- PDF parsing and page extraction
STOP → Verify build → Test PDF processing
Step 5.2: Create ImageOptimizer.ts
Location: src/converters/pdf/ImageOptimizer.ts (~150 lines)
Extract:
- Image optimization logic
- Resizing and compression
- Quality management
STOP → Verify build → Test image optimization
Step 5.3: Update PdfToImageConverter.ts
Final size: ~150 lines Keep:
- Main conversion method
- Error handling
- Result processing
STOP → Test PDF to image conversion works
CHECKPOINT: Test embedded PDF conversion
PHASE 6: Refactor DependencyChecker.ts
Step 6.1: Create ExecutableChecker.ts
Location: src/core/dependencies/ExecutableChecker.ts (~150 lines)
Extract:
- Executable checking utilities
- Version detection
- Path validation
STOP → Verify build → Test executable detection
Step 6.2: Update DependencyChecker.ts
Final size: ~170 lines Keep:
- Main dependency checking logic
- Status reporting
- Error handling
STOP → Test all dependency checking functionality
CHECKPOINT: Test all external tool detection
Final Verification
After ALL phases complete:
- Full plugin functionality test
- Export various note types (simple, complex, with embeds)
- Test all settings combinations
- Verify no regressions from original functionality
- Performance check - ensure no significant slowdown
Execution Process (CRITICAL)
For EACH file modification:
- Use Context7 to check Obsidian/TypeScript API documentation
- Create new file with minimal extraction
- Update imports in original file
- Run
npm run build - STOP and wait for user verification of build
- Test specific functionality in Obsidian
- STOP and wait for user confirmation functionality works
- Only then proceed to next step
Success Criteria
- All 6 large files reduced to manageable size (150-200 lines each)
- No loss of functionality
- No performance degradation
- Clean module boundaries
- Maintainable code structure
- All tests pass (build + manual verification)
Rollback Plan
If any phase fails:
- Revert changes using git
- Analyze what went wrong
- Adjust approach before retrying
- Consider smaller extraction steps
Plan created: 2025-01-08 Status: Ready to execute Phase 1