This commit is contained in:
0x8664b2
2025-06-20 09:06:01 -07:00
commit 07b70455a3
12 changed files with 735 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,13 @@
{
"images" : [
{
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,86 @@
import SwiftUI
import UniformTypeIdentifiers
import AppKit
struct ContentView: View {
@State private var selectedFileURL: URL?
@State private var markdownContent: String = ""
var body: some View {
NavigationSplitView {
VStack {
Button("Open Markdown File") {
openFile()
}
.padding()
Button("Test with Sample Content") {
markdownContent = "# Test\n\nThis is **test** content."
print("Set test content: \(markdownContent)")
}
.padding()
if let url = selectedFileURL {
Text("File: \(url.lastPathComponent)")
.foregroundColor(.secondary)
.font(.caption)
.truncationMode(.middle)
.padding(.horizontal)
}
Spacer()
}
.frame(minWidth: 200)
.navigationSplitViewColumnWidth(min: 200, ideal: 250)
} detail: {
if !markdownContent.isEmpty {
MarkdownRenderer(content: markdownContent)
.onAppear {
print("MarkdownRenderer appeared with content length: \(markdownContent.count)")
}
} else {
VStack {
Image(systemName: "doc.text")
.font(.system(size: 60))
.foregroundColor(.secondary)
Text("Select a markdown file to preview")
.foregroundColor(.secondary)
.padding()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
.navigationTitle("Markdown Viewer")
}
private func openFile() {
let panel = NSOpenPanel()
panel.allowsMultipleSelection = false
panel.canChooseDirectories = false
panel.canChooseFiles = true
panel.allowedContentTypes = [UTType.plainText, UTType(filenameExtension: "md")!]
if panel.runModal() == .OK {
if let url = panel.url {
selectedFileURL = url
loadMarkdownFile(from: url)
}
}
}
private func loadMarkdownFile(from url: URL) {
do {
let content = try String(contentsOf: url, encoding: .utf8)
print("File loaded successfully, content length: \(content.count)")
print("First 100 characters: \(String(content.prefix(100)))")
markdownContent = content
} catch {
print("Error reading file: \(error)")
markdownContent = "Error loading file: \(error.localizedDescription)"
}
}
}
#Preview {
ContentView()
}

View File

@@ -0,0 +1,153 @@
import SwiftUI
import WebKit
import AppKit
struct MarkdownRenderer: NSViewRepresentable {
let content: String
func makeNSView(context: Context) -> WKWebView {
print("MarkdownRenderer makeNSView called")
let webView = WKWebView()
webView.navigationDelegate = context.coordinator
// Set a background color to see if the view is created
webView.wantsLayer = true
webView.layer?.backgroundColor = NSColor.systemYellow.cgColor
return webView
}
func updateNSView(_ webView: WKWebView, context: Context) {
print("MarkdownRenderer updateNSView called with content length: \(content.count)")
let htmlContent = convertMarkdownToHTML(content)
print("Generated HTML length: \(htmlContent.count)")
webView.loadHTMLString(htmlContent, baseURL: nil)
}
func makeCoordinator() -> Coordinator {
Coordinator()
}
class Coordinator: NSObject, WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("WebView finished loading")
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print("WebView navigation failed: \(error)")
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
print("WebView provisional navigation failed: \(error)")
}
}
private func convertMarkdownToHTML(_ markdown: String) -> String {
guard !markdown.isEmpty else {
return "<p>No content to display</p>"
}
var html = markdown
// Handle headers (more robust approach)
let headerRegex1 = try! NSRegularExpression(pattern: "^# (.+)$", options: [.anchorsMatchLines])
let headerRegex2 = try! NSRegularExpression(pattern: "^## (.+)$", options: [.anchorsMatchLines])
let headerRegex3 = try! NSRegularExpression(pattern: "^### (.+)$", options: [.anchorsMatchLines])
let headerRegex4 = try! NSRegularExpression(pattern: "^#### (.+)$", options: [.anchorsMatchLines])
let headerRegex5 = try! NSRegularExpression(pattern: "^##### (.+)$", options: [.anchorsMatchLines])
let headerRegex6 = try! NSRegularExpression(pattern: "^###### (.+)$", options: [.anchorsMatchLines])
let range = NSRange(location: 0, length: html.count)
html = headerRegex6.stringByReplacingMatches(in: html, options: [], range: range, withTemplate: "<h6>$1</h6>")
html = headerRegex5.stringByReplacingMatches(in: html, options: [], range: NSRange(location: 0, length: html.count), withTemplate: "<h5>$1</h5>")
html = headerRegex4.stringByReplacingMatches(in: html, options: [], range: NSRange(location: 0, length: html.count), withTemplate: "<h4>$1</h4>")
html = headerRegex3.stringByReplacingMatches(in: html, options: [], range: NSRange(location: 0, length: html.count), withTemplate: "<h3>$1</h3>")
html = headerRegex2.stringByReplacingMatches(in: html, options: [], range: NSRange(location: 0, length: html.count), withTemplate: "<h2>$1</h2>")
html = headerRegex1.stringByReplacingMatches(in: html, options: [], range: NSRange(location: 0, length: html.count), withTemplate: "<h1>$1</h1>")
// Handle bold (before italic to avoid conflicts)
let boldRegex = try! NSRegularExpression(pattern: "\\*\\*(.*?)\\*\\*", options: [])
html = boldRegex.stringByReplacingMatches(in: html, options: [], range: NSRange(location: 0, length: html.count), withTemplate: "<strong>$1</strong>")
// Handle italic
let italicRegex = try! NSRegularExpression(pattern: "\\*(.*?)\\*", options: [])
html = italicRegex.stringByReplacingMatches(in: html, options: [], range: NSRange(location: 0, length: html.count), withTemplate: "<em>$1</em>")
// Handle inline code
let codeRegex = try! NSRegularExpression(pattern: "`([^`]+)`", options: [])
html = codeRegex.stringByReplacingMatches(in: html, options: [], range: NSRange(location: 0, length: html.count), withTemplate: "<code>$1</code>")
// Handle links
let linkRegex = try! NSRegularExpression(pattern: "\\[([^\\]]+)\\]\\(([^\\)]+)\\)", options: [])
html = linkRegex.stringByReplacingMatches(in: html, options: [], range: NSRange(location: 0, length: html.count), withTemplate: "<a href=\"$2\">$1</a>")
// Convert line breaks to paragraphs
let paragraphs = html.components(separatedBy: "\n\n")
html = paragraphs.map { paragraph in
let trimmed = paragraph.trimmingCharacters(in: .whitespacesAndNewlines)
if trimmed.isEmpty {
return ""
}
// Don't wrap headers in paragraphs
if trimmed.hasPrefix("<h") {
return trimmed.replacingOccurrences(of: "\n", with: "<br>")
}
return "<p>" + trimmed.replacingOccurrences(of: "\n", with: "<br>") + "</p>"
}.filter { !$0.isEmpty }.joined(separator: "\n")
let fullHTML = """
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
line-height: 1.6;
color: #333;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #ffffff;
}
h1, h2, h3, h4, h5, h6 {
color: #2c3e50;
margin-top: 24px;
margin-bottom: 16px;
}
h1 { font-size: 2em; border-bottom: 2px solid #eee; padding-bottom: 10px; }
h2 { font-size: 1.5em; border-bottom: 1px solid #eee; padding-bottom: 8px; }
h3 { font-size: 1.25em; }
p { margin-bottom: 16px; }
code {
background-color: #f4f4f4;
padding: 2px 4px;
border-radius: 3px;
font-family: 'SF Mono', Monaco, Menlo, monospace;
font-size: 0.9em;
}
a {
color: #007AFF;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
strong {
font-weight: 600;
}
em {
font-style: italic;
}
</style>
</head>
<body>
\(html)
</body>
</html>
"""
return fullHTML
}
}

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<false/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
</dict>
</plist>

View File

@@ -0,0 +1,10 @@
import SwiftUI
@main
struct MarkdownViewerApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}

View File

@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}