diff --git a/Sources/MarkdownUI/Parser/MarkdownParser.swift b/Sources/MarkdownUI/Parser/MarkdownParser.swift index 76bbd193..1dea889e 100644 --- a/Sources/MarkdownUI/Parser/MarkdownParser.swift +++ b/Sources/MarkdownUI/Parser/MarkdownParser.swift @@ -60,6 +60,11 @@ extension BlockNode { case .codeBlock: self = .codeBlock(fenceInfo: unsafeNode.fenceInfo, content: unsafeNode.literal ?? "") case .htmlBlock: + // Don't render comments + let trimmedLiteral = unsafeNode.literal?.trimmingCharacters(in: .whitespacesAndNewlines) + if let trimmedLiteral, trimmedLiteral.hasPrefix("") { + return nil + } self = .htmlBlock(content: unsafeNode.literal ?? "") case .paragraph: self = .paragraph(content: unsafeNode.children.compactMap(InlineNode.init(unsafeNode:))) diff --git a/Tests/MarkdownUITests/MarkdownContentTests.swift b/Tests/MarkdownUITests/MarkdownContentTests.swift index 14e6df1f..66ad1111 100644 --- a/Tests/MarkdownUITests/MarkdownContentTests.swift +++ b/Tests/MarkdownUITests/MarkdownContentTests.swift @@ -423,4 +423,22 @@ final class MarkdownContentTests: XCTestCase { ) XCTAssertEqual(markdown, content.renderMarkdown()) } + + func testComment() { + // given + let markdown = """ + + + """ + + // when + let content = MarkdownContent(markdown) + + // then + XCTAssertEqual(MarkdownContent {}, content) + XCTAssertEqual("", content.renderMarkdown()) + } } diff --git a/Tests/MarkdownUITests/MarkdownTests.swift b/Tests/MarkdownUITests/MarkdownTests.swift index 488d991c..8f5afbee 100644 --- a/Tests/MarkdownUITests/MarkdownTests.swift +++ b/Tests/MarkdownUITests/MarkdownTests.swift @@ -342,5 +342,21 @@ assertSnapshot(of: view, as: .image(layout: layout)) } + + func testComment() { + let view = Markdown { + #""" + This is rendered + + + So is this + """# + }.markdownSoftBreakMode(.lineBreak) + + assertSnapshot(of: view, as: .image(layout: layout)) + } } #endif diff --git a/Tests/MarkdownUITests/__Snapshots__/MarkdownTests/testComment.1.png b/Tests/MarkdownUITests/__Snapshots__/MarkdownTests/testComment.1.png new file mode 100644 index 00000000..9a05e4e6 Binary files /dev/null and b/Tests/MarkdownUITests/__Snapshots__/MarkdownTests/testComment.1.png differ