Skip to content

Commit 7cee024

Browse files
authored
Merge pull request #2 from jamf/dont-render-comments
Don't render markdown comments
2 parents a9c7615 + ac4adaf commit 7cee024

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

Sources/MarkdownUI/Parser/MarkdownParser.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ extension BlockNode {
6060
case .codeBlock:
6161
self = .codeBlock(fenceInfo: unsafeNode.fenceInfo, content: unsafeNode.literal ?? "")
6262
case .htmlBlock:
63+
// Don't render comments
64+
let trimmedLiteral = unsafeNode.literal?.trimmingCharacters(in: .whitespacesAndNewlines)
65+
if let trimmedLiteral, trimmedLiteral.hasPrefix("<!--") && trimmedLiteral.hasSuffix("-->") {
66+
return nil
67+
}
6368
self = .htmlBlock(content: unsafeNode.literal ?? "")
6469
case .paragraph:
6570
self = .paragraph(content: unsafeNode.children.compactMap(InlineNode.init(unsafeNode:)))

Tests/MarkdownUITests/MarkdownContentTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,22 @@ final class MarkdownContentTests: XCTestCase {
423423
)
424424
XCTAssertEqual(markdown, content.renderMarkdown())
425425
}
426+
427+
func testComment() {
428+
// given
429+
let markdown = """
430+
<!--This is a comment-->
431+
<!--
432+
This is a
433+
multiline comment
434+
-->
435+
"""
436+
437+
// when
438+
let content = MarkdownContent(markdown)
439+
440+
// then
441+
XCTAssertEqual(MarkdownContent {}, content)
442+
XCTAssertEqual("", content.renderMarkdown())
443+
}
426444
}

Tests/MarkdownUITests/MarkdownTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,5 +342,21 @@
342342

343343
assertSnapshot(of: view, as: .image(layout: layout))
344344
}
345+
346+
func testComment() {
347+
let view = Markdown {
348+
#"""
349+
This is rendered
350+
<!--This is a comment-->
351+
<!--
352+
This is a multiline
353+
comment
354+
-->
355+
So is this
356+
"""#
357+
}.markdownSoftBreakMode(.lineBreak)
358+
359+
assertSnapshot(of: view, as: .image(layout: layout))
360+
}
345361
}
346362
#endif
52.1 KB
Loading

0 commit comments

Comments
 (0)