Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Preserve HTML blocks as text

Manav Rathi28d321382469
diff --git a/apple/apps/ensu/Ensu/Chat/MessageList/MessageMarkdown.swift b/apple/apps/ensu/Ensu/Chat/MessageList/MessageMarkdown.swift
index 057da6183b..3a2690f1d7 100644
--- a/apple/apps/ensu/Ensu/Chat/MessageList/MessageMarkdown.swift
+++ b/apple/apps/ensu/Ensu/Chat/MessageList/MessageMarkdown.swift
@@ -262,6 +262,8 @@ struct MarkdownParser {
let code = codeBlock.code.trimmingCharacters(in: .whitespacesAndNewlines)
guard !code.isEmpty else { return [] }
return [.code(text: code)]
+ case let html as HTMLBlock:
+ return [.paragraph(text: InlineContent().appending(html.rawHTML))]
case _ as ThematicBreak:
return [.divider]
case let orderedList as OrderedList:
@@ -284,6 +286,10 @@ struct MarkdownParser {
private func renderBlockQuote(_ quote: BlockQuote) -> InlineContent {
var parts: [InlineContent] = []
for child in quote.children {
+ if let html = child as? HTMLBlock {
+ parts.append(InlineContent().appending(html.rawHTML))
+ continue
+ }
if let paragraph = child as? Paragraph {
let text = renderInlineChildren(paragraph)
if !text.isEmpty {
@@ -321,6 +327,10 @@ struct MarkdownParser {
private func renderListItem(_ item: ListItem) -> InlineContent {
var parts: [InlineContent] = []
for child in item.children {
+ if let html = child as? HTMLBlock {
+ parts.append(InlineContent().appending(html.rawHTML))
+ continue
+ }
if let paragraph = child as? Paragraph {
let text = renderInlineChildren(paragraph)
if !text.isEmpty {
diff --git a/apple/apps/ensu/EnsuTests/MessageMarkdownTests.swift b/apple/apps/ensu/EnsuTests/MessageMarkdownTests.swift
index e9929e0db0..3d27fd2ac7 100644
--- a/apple/apps/ensu/EnsuTests/MessageMarkdownTests.swift
+++ b/apple/apps/ensu/EnsuTests/MessageMarkdownTests.swift
@@ -53,6 +53,35 @@ final class MessageMarkdownTests: XCTestCase {
XCTAssertTrue(mathSpans(inline).isEmpty)
}
+ func testHTMLBlocksRemainLiteral() {
+ let html = "<div>\nhello $x$\n</div>\n"
+ let sources = [
+ html,
+ "> <div>\n> hello $x$\n> </div>",
+ "- <div>\n hello $x$\n </div>",
+ ]
+ for source in sources {
+ let blocks = MarkdownParser.parse(source)
+ XCTAssertEqual(blocks.count, 1)
+ guard let block = blocks.first else { continue }
+ let content: InlineContent
+ switch block.kind {
+ case .paragraph(let text), .blockquote(let text):
+ content = text
+ case .list(let items):
+ XCTAssertEqual(items.count, 1)
+ content = InlineContent.joined(items, separator: "")
+ default:
+ XCTFail("Unexpected block kind")
+ continue
+ }
+ XCTAssertEqual(String(content.attributedText.characters), html)
+ XCTAssertFalse(content.hasMath)
+ }
+ let details = "<details>summary</details>"
+ XCTAssertEqual(String(paragraph(details).attributedText.characters), details + "\n")
+ }
+
func testMathInBlocksAndLinkLabels() {
let blocks = MarkdownParser.parse(
"# Heading \\(x\\)\n\n- Item $y$\n\n> Quote \\(z\\)\ncontinued $q$\n\n$$\\frac{1}{2}$$")