Skip to content

Commit a42676a

Browse files
author
Guille Gonzalez
authored
Fix crash when rendering a code block without a closing fence (#112)
1 parent 956a45b commit a42676a

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

Sources/MarkdownUI/Rendering/AttributedStringRenderer.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,10 @@ extension AttributedStringRenderer {
266266
state.addFirstLineIndent()
267267

268268
var code = codeBlock.code.replacingOccurrences(of: "\n", with: String.lineSeparator)
269-
// Remove the last line separator
270-
code.removeLast()
269+
if !code.isEmpty {
270+
// Remove the last line separator
271+
code.removeLast()
272+
}
271273

272274
return renderParagraph(.init(text: [.text(code)]), hasSuccessor: hasSuccessor, state: state)
273275
}

Tests/MarkdownUITests/MarkdownTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,20 @@
239239
assertSnapshot(matching: view, as: .image(layout: layout), named: platformName)
240240
}
241241

242+
func testOpenCodeBlock() {
243+
let view = Markdown(
244+
#"""
245+
An code block without a closing fence:
246+
247+
```swift
248+
"""#
249+
)
250+
.background(Color.orange)
251+
.padding()
252+
253+
assertSnapshot(matching: view, as: .image(layout: layout), named: platformName)
254+
}
255+
242256
func testVerbatimHTML() {
243257
let view = Markdown(
244258
#"""
28.7 KB
Loading
43.8 KB
Loading

0 commit comments

Comments
 (0)