From 06a8ca67c3252399d2450eeb5f5e905b0dc6e125 Mon Sep 17 00:00:00 2001 From: Nathan Reed Date: Mon, 27 Nov 2023 03:15:27 -0500 Subject: [PATCH] Improve display of HTML ul (bullet list) and ol (numbered list) (#1690) While SwiftUI's `Text` view won't display these in an `AttributedString` even if they get parsed from Markdown (which would also require the use of the `.full` option instead of the `.inlineOnlyPresrevingWhitespace` option), we can improve the appearance somewhat. Currently, list elements are clumped together with no spaces between them, and there's no indication whatsoever that the author indicated these to be a list. Change to insert Markdown list syntax with linebreaks and dashes, so users can at least understand there's a list there. Similar change for ordered lists. This will still be broken for nested lists, but it didn't seem worth it to put a lot of effort into this (or other revamps, like making bold/italics/code work properly) because it seems like the current text handling in Ice Cubes is suboptimal and eventually slated for improvement (according to https://github.com/Dimillian/IceCubesApp/issues/1459#issuecomment-1638562657). So this is more designed to make lists "less broken" in some cases, rather than be a comprehensive fix for all lists in all cases. --- .../Sources/Models/Alias/HTMLString.swift | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Packages/Models/Sources/Models/Alias/HTMLString.swift b/Packages/Models/Sources/Models/Alias/HTMLString.swift index ea19a449..686e030c 100644 --- a/Packages/Models/Sources/Models/Alias/HTMLString.swift +++ b/Packages/Models/Sources/Models/Alias/HTMLString.swift @@ -183,6 +183,28 @@ public struct HTMLString: Codable, Equatable, Hashable, @unchecked Sendable { } // Strip newlines and line separators - they should be being sent as
s asMarkdown += txt.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\u{2028}", with: "") + } else if node.nodeName() == "ul" { + // Unordered (bulleted) list + // SwiftUI's Text won't display these in an AttributedString, but we can at least improve the appearance + asMarkdown += "\n\n" + for nn in node.getChildNodes() { + asMarkdown += "- " + handleNode(node: nn) + asMarkdown += "\n" + } + return + } else if node.nodeName() == "ol" { + // Ordered (numbered) list + // Same thing, won't display in a Text, but this is just an attempt to improve the appearance + asMarkdown += "\n\n" + var curNumber = 1 + for nn in node.getChildNodes() { + asMarkdown += "\(curNumber). " + handleNode(node: nn) + asMarkdown += "\n" + curNumber += 1 + } + return } for n in node.getChildNodes() {