Overview
MacDown currently uses the deprecated WebView class for rendering the Markdown preview. This should be migrated to the modern WKWebView API.
Why This Is Important
1. WebView is Deprecated
WebView was deprecated in macOS 10.14 (Mojave)
- Apple recommends migrating to
WKWebView for all new development
- Future macOS versions may remove
WebView support entirely
2. Technical Benefits
- Better performance: WKWebView uses a separate process for rendering
- Modern web standards: Better JavaScript, CSS, and HTML5 support
- Improved security: Better sandboxing and security features
- Proper cache control: No need for private API hacks
- Better rendering: More accurate rendering of modern web content
3. Fixes Multiple Issues
Migrating to WKWebView could potentially resolve:
Current WebView Usage
Locations in the codebase:
Migration Challenges
API Differences
| WebView |
WKWebView |
Notes |
loadHTMLString:baseURL: |
loadHTMLString:baseURL: |
Similar API |
stringByEvaluatingJavaScriptFromString: |
evaluateJavaScript:completionHandler: |
Async in WKWebView |
WebFrameLoadDelegate |
WKNavigationDelegate |
Different delegate pattern |
mainFrame.DOMDocument |
No direct DOM access |
Need JavaScript bridge |
| Synchronous JS execution |
Asynchronous JS execution |
Requires code changes |
Key Changes Required
-
Asynchronous JavaScript Execution
- All
stringByEvaluatingJavaScriptFromString: calls need to be converted to async
- May require significant refactoring of code that depends on JS results
-
No Direct DOM Access
- The commented-out DOM update code (lines 1072-1112 in MPDocument.m) uses
DOMDocument
- Would need to use JavaScript instead:
document.documentElement.innerHTML = newHTML
-
Delegate Method Migration
- Convert
WebFrameLoadDelegate methods to WKNavigationDelegate
- Update method signatures and behavior
-
Cache Control
- Replace private
WebCache API with WKWebsiteDataStore
- Proper cache management for images
-
Testing
- Thorough testing of all Markdown features
- Verify JavaScript libraries work correctly (MathJax, Prism, Mermaid, etc.)
- Test scroll position preservation
- Verify all delegate callbacks work correctly
Implementation Plan
Phase 1: Research and Planning
Phase 2: Core Migration
Phase 3: JavaScript Library Integration
Phase 4: Testing and Polish
Phase 5: Image Cache Management
Success Criteria
Priority
Medium-High - Not urgent since WebView still works, but important for:
Estimated Effort
Large - This is a significant refactoring:
- Multiple files affected
- Async/await pattern changes required
- Extensive testing needed
- Potential for subtle bugs
Estimate: 1-2 weeks of focused development + 1 week of testing
References
Overview
MacDown currently uses the deprecated
WebViewclass for rendering the Markdown preview. This should be migrated to the modernWKWebViewAPI.Why This Is Important
1. WebView is Deprecated
WebViewwas deprecated in macOS 10.14 (Mojave)WKWebViewfor all new developmentWebViewsupport entirely2. Technical Benefits
3. Fixes Multiple Issues
Migrating to WKWebView could potentially resolve:
Current WebView Usage
Locations in the codebase:
MPDocument.m: Main preview WebView instanceMPMainController.m: Previously had WebCache disabling code (removed in PR Fix preview pane flickering by removing WebCache disabling #109)Migration Challenges
API Differences
loadHTMLString:baseURL:loadHTMLString:baseURL:stringByEvaluatingJavaScriptFromString:evaluateJavaScript:completionHandler:WebFrameLoadDelegateWKNavigationDelegatemainFrame.DOMDocumentKey Changes Required
Asynchronous JavaScript Execution
stringByEvaluatingJavaScriptFromString:calls need to be converted to asyncNo Direct DOM Access
DOMDocumentdocument.documentElement.innerHTML = newHTMLDelegate Method Migration
WebFrameLoadDelegatemethods toWKNavigationDelegateCache Control
WebCacheAPI withWKWebsiteDataStoreTesting
Implementation Plan
Phase 1: Research and Planning
Phase 2: Core Migration
Phase 3: JavaScript Library Integration
Phase 4: Testing and Polish
Phase 5: Image Cache Management
Success Criteria
Priority
Medium-High - Not urgent since WebView still works, but important for:
Estimated Effort
Large - This is a significant refactoring:
Estimate: 1-2 weeks of focused development + 1 week of testing
References