Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/MarkdownUI/Parser/MarkdownParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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("<!--") && trimmedLiteral.hasSuffix("-->") {
return nil
}
self = .htmlBlock(content: unsafeNode.literal ?? "")
case .paragraph:
self = .paragraph(content: unsafeNode.children.compactMap(InlineNode.init(unsafeNode:)))
Expand Down
18 changes: 18 additions & 0 deletions Tests/MarkdownUITests/MarkdownContentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,22 @@ final class MarkdownContentTests: XCTestCase {
)
XCTAssertEqual(markdown, content.renderMarkdown())
}

func testComment() {
// given
let markdown = """
<!--This is a comment-->
<!--
This is a
multiline comment
-->
"""

// when
let content = MarkdownContent(markdown)

// then
XCTAssertEqual(MarkdownContent {}, content)
XCTAssertEqual("", content.renderMarkdown())
}
}
16 changes: 16 additions & 0 deletions Tests/MarkdownUITests/MarkdownTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -342,5 +342,21 @@

assertSnapshot(of: view, as: .image(layout: layout))
}

func testComment() {
let view = Markdown {
#"""
This is rendered
<!--This is a comment-->
<!--
This is a multiline
comment
-->
So is this
"""#
}.markdownSoftBreakMode(.lineBreak)

assertSnapshot(of: view, as: .image(layout: layout))
}
}
#endif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.