Add support for horizontal line
This commit is contained in:
@@ -14,12 +14,6 @@ struct ContentView: View {
|
|||||||
}
|
}
|
||||||
.padding()
|
.padding()
|
||||||
|
|
||||||
Button("Test with Sample Content") {
|
|
||||||
markdownContent = "# Test\n\nThis is **test** content."
|
|
||||||
print("Set test content: \(markdownContent)")
|
|
||||||
}
|
|
||||||
.padding()
|
|
||||||
|
|
||||||
if let url = selectedFileURL {
|
if let url = selectedFileURL {
|
||||||
Text("File: \(url.lastPathComponent)")
|
Text("File: \(url.lastPathComponent)")
|
||||||
.foregroundColor(.secondary)
|
.foregroundColor(.secondary)
|
||||||
|
|||||||
@@ -77,6 +77,10 @@ struct MarkdownRenderer: NSViewRepresentable {
|
|||||||
let codeRegex = try! NSRegularExpression(pattern: "`([^`]+)`", options: [])
|
let codeRegex = try! NSRegularExpression(pattern: "`([^`]+)`", options: [])
|
||||||
html = codeRegex.stringByReplacingMatches(in: html, options: [], range: NSRange(location: 0, length: html.count), withTemplate: "<code>$1</code>")
|
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
|
// Handle links
|
||||||
let linkRegex = try! NSRegularExpression(pattern: "\\[([^\\]]+)\\]\\(([^\\)]+)\\)", options: [])
|
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>")
|
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 {
|
if trimmed.isEmpty {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
// Don't wrap headers in paragraphs
|
// Don't wrap headers or horizontal rules in paragraphs
|
||||||
if trimmed.hasPrefix("<h") {
|
if trimmed.hasPrefix("<h") || trimmed == "<hr>" {
|
||||||
return trimmed.replacingOccurrences(of: "\n", with: "<br>")
|
return trimmed.replacingOccurrences(of: "\n", with: "<br>")
|
||||||
}
|
}
|
||||||
return "<p>" + trimmed.replacingOccurrences(of: "\n", with: "<br>") + "</p>"
|
return "<p>" + trimmed.replacingOccurrences(of: "\n", with: "<br>") + "</p>"
|
||||||
@@ -140,6 +144,11 @@ struct MarkdownRenderer: NSViewRepresentable {
|
|||||||
em {
|
em {
|
||||||
font-style: italic;
|
font-style: italic;
|
||||||
}
|
}
|
||||||
|
hr {
|
||||||
|
border: none;
|
||||||
|
border-top: 2px solid #eee;
|
||||||
|
margin: 24px 0;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
Reference in New Issue
Block a user