Add support for horizontal line

This commit is contained in:
0x8664b2
2025-06-20 09:32:14 -07:00
parent 07b70455a3
commit e17eba0a1b
2 changed files with 11 additions and 8 deletions

View File

@@ -14,12 +14,6 @@ struct ContentView: View {
}
.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)

View File

@@ -77,6 +77,10 @@ struct MarkdownRenderer: NSViewRepresentable {
let codeRegex = try! NSRegularExpression(pattern: "`([^`]+)`", options: [])
html = codeRegex.stringByReplacingMatches(in: html, options: [], range: NSRange(location: 0, length: html.count), withTemplate: "<code>$1</code>")
// Handle horizontal rules (-- on its own line)
let hrRegex = try! NSRegularExpression(pattern: "^--$", options: [.anchorsMatchLines])
html = hrRegex.stringByReplacingMatches(in: html, options: [], range: NSRange(location: 0, length: html.count), withTemplate: "<hr>")
// 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>")
@@ -88,8 +92,8 @@ struct MarkdownRenderer: NSViewRepresentable {
if trimmed.isEmpty {
return ""
}
// Don't wrap headers in paragraphs
if trimmed.hasPrefix("<h") {
// Don't wrap headers or horizontal rules in paragraphs
if trimmed.hasPrefix("<h") || trimmed == "<hr>" {
return trimmed.replacingOccurrences(of: "\n", with: "<br>")
}
return "<p>" + trimmed.replacingOccurrences(of: "\n", with: "<br>") + "</p>"
@@ -140,6 +144,11 @@ struct MarkdownRenderer: NSViewRepresentable {
em {
font-style: italic;
}
hr {
border: none;
border-top: 2px solid #eee;
margin: 24px 0;
}
</style>
</head>
<body>