Skip to content

armgit / ente

Public Imported from GitHub
0

💚 End-to-end encrypted cloud for everything.

Fix Ensu math rendering and log loading

Manav Rathi45ef151a0f1a
diff --git a/apple/apps/ensu/Ensu.xcodeproj/project.pbxproj b/apple/apps/ensu/Ensu.xcodeproj/project.pbxproj
index a3a4b3cfe4..6f70f2531f 100644
--- a/apple/apps/ensu/Ensu.xcodeproj/project.pbxproj
+++ b/apple/apps/ensu/Ensu.xcodeproj/project.pbxproj
@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
+ A30000012F11111100AA0001 = {isa = PBXBuildFile; fileRef = A30000022F11111100AA0001; };
A20000012F11111100AA0001 = {isa = PBXBuildFile; fileRef = A20000022F11111100AA0001; };
A10000012F11111100AA0001 /* LlmIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A10000022F11111100AA0001 /* LlmIntegrationTests.swift */; };
DAE20A022F00000100000002 /* EnteOnnxRuntime in Frameworks */ = {isa = PBXBuildFile; productRef = DAE20A012F00000100000001 /* EnteOnnxRuntime */; };
@@ -26,6 +27,7 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
+ A30000022F11111100AA0001 = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageMarkdownTests.swift; sourceTree = "<group>"; };
A20000022F11111100AA0001 = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotesIntegrationTests.swift; sourceTree = "<group>"; };
A10000022F11111100AA0001 /* LlmIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LlmIntegrationTests.swift; sourceTree = "<group>"; };
A10000032F11111100AA0001 /* EnsuTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = EnsuTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -80,6 +82,7 @@
children = (
A10000022F11111100AA0001 /* LlmIntegrationTests.swift */,
A20000022F11111100AA0001,
+ A30000022F11111100AA0001,
);
path = EnsuTests;
sourceTree = "<group>";
@@ -248,6 +251,7 @@
files = (
A10000012F11111100AA0001 /* LlmIntegrationTests.swift in Sources */,
A20000012F11111100AA0001,
+ A30000012F11111100AA0001,
);
runOnlyForDeploymentPostprocessing = 0;
};
diff --git a/apple/apps/ensu/Ensu/Chat/MessageList/MessageListComponents.swift b/apple/apps/ensu/Ensu/Chat/MessageList/MessageListComponents.swift
index 8bed17b3cb..25908d5ab5 100644
--- a/apple/apps/ensu/Ensu/Chat/MessageList/MessageListComponents.swift
+++ b/apple/apps/ensu/Ensu/Chat/MessageList/MessageListComponents.swift
@@ -1,5 +1,4 @@
import SwiftUI
-import Markdown
import QuickLook
import UIKit
@@ -726,8 +725,9 @@ struct MarkdownView: View {
let isLast = block.id == (blocks.last?.id ?? -1)
switch block.kind {
case .heading(let level, let text):
- let displayText = showCursor && isLast ? text + StreamingCursor.glyph : text
- if containsInlineMath(displayText) {
+ let displayText =
+ showCursor && isLast ? text.appending(StreamingCursor.glyph) : text
+ if displayText.hasMath {
InlineMathTextView(
text: displayText,
fonts: InlineFontSet(
@@ -739,26 +739,28 @@ struct MarkdownView: View {
textColor: EnsuColor.textPrimary
)
} else {
- markdownText(displayText)
+ Text(displayText.attributedText)
.font(headingFont(for: level))
.foregroundStyle(EnsuColor.textPrimary)
}
case .paragraph(let text):
- let displayText = showCursor && isLast ? text + StreamingCursor.glyph : text
- if containsInlineMath(displayText) {
+ let displayText =
+ showCursor && isLast ? text.appending(StreamingCursor.glyph) : text
+ if displayText.hasMath {
InlineMathTextView(
text: displayText,
fonts: messageInlineFonts,
textColor: EnsuColor.textPrimary
)
} else {
- markdownText(displayText)
+ Text(displayText.attributedText)
.font(EnsuTypography.message)
.foregroundStyle(EnsuColor.textPrimary)
.lineSpacing(messageLineSpacing)
}
case .blockquote(let text):
- let displayText = showCursor && isLast ? text + StreamingCursor.glyph : text
+ let displayText =
+ showCursor && isLast ? text.appending(StreamingCursor.glyph) : text
BlockQuoteView(text: displayText)
case .code(let code):
CodeBlockView(code: code)
@@ -768,7 +770,7 @@ struct MarkdownView: View {
let resolvedItems =
(showCursor && isLast)
? items.enumerated().map { offset, item in
- offset == items.count - 1 ? item + StreamingCursor.glyph : item
+ offset == items.count - 1 ? item.appending(StreamingCursor.glyph) : item
}
: items
@@ -778,14 +780,14 @@ struct MarkdownView: View {
Text("•")
.font(EnsuTypography.message)
.foregroundStyle(EnsuColor.textPrimary)
- if containsInlineMath(item) {
+ if item.hasMath {
InlineMathTextView(
text: item,
fonts: messageInlineFonts,
textColor: EnsuColor.textPrimary
)
} else {
- markdownText(item)
+ Text(item.attributedText)
.font(EnsuTypography.message)
.foregroundStyle(EnsuColor.textPrimary)
.lineSpacing(messageLineSpacing)
@@ -824,30 +826,6 @@ struct MarkdownView: View {
}
}
-func markdownText(_ text: String) -> SwiftUI.Text {
- if let attributed = try? AttributedString(markdown: text) {
- return SwiftUI.Text(attributed)
- }
- return SwiftUI.Text(text)
-}
-
-private enum InlineMarkdownStyle: Equatable {
- case normal
- case bold
- case italic
- case code
-}
-
-private enum InlineSegment: Equatable {
- case text(String, InlineMarkdownStyle)
- case math(String)
-}
-
-private struct InlineMathMatch {
- let latex: String
- let endIndex: Int
-}
-
private struct InlineFontSet {
let normal: Font
let code: Font
@@ -860,273 +838,51 @@ private let messageInlineFonts = InlineFontSet(
mathSize: 15
)
-private func parseInlineSegments(_ text: String) -> [InlineSegment] {
- let characters = Array(text)
- var segments: [InlineSegment] = []
- var buffer: [Character] = []
- var currentStyle: InlineMarkdownStyle = .normal
- var index = 0
-
- func flushBuffer() {
- if !buffer.isEmpty {
- segments.append(.text(String(buffer), currentStyle))
- buffer.removeAll(keepingCapacity: true)
- }
+private func inlineText(_ content: AttributedString, fonts: InlineFontSet) -> Text {
+ var attributed = content
+ for run in content.runs where run.inlinePresentationIntent?.contains(.code) == true {
+ attributed[run.range].font = fonts.code
}
-
- while index < characters.count {
- if currentStyle != .code,
- let mathMatch = findInlineMathMatch(characters, startIndex: index)
- {
- flushBuffer()
- segments.append(.math(mathMatch.latex))
- index = mathMatch.endIndex
- continue
- }
-
- switch currentStyle {
- case .code:
- if characters[index] == "`" {
- flushBuffer()
- currentStyle = .normal
- index += 1
- } else {
- buffer.append(characters[index])
- index += 1
- }
- case .bold:
- if hasPrefix(characters, at: index, token: ["*", "*"]) {
- flushBuffer()
- currentStyle = .normal
- index += 2
- } else {
- buffer.append(characters[index])
- index += 1
- }
- case .italic:
- if characters[index] == "*" && !hasPrefix(characters, at: index, token: ["*", "*"]) {
- flushBuffer()
- currentStyle = .normal
- index += 1
- } else {
- buffer.append(characters[index])
- index += 1
- }
- case .normal:
- switch true {
- case hasPrefix(characters, at: index, token: ["*", "*"])
- && hasClosingDelimiter(characters, startIndex: index + 2, delimiter: ["*", "*"]):
- flushBuffer()
- currentStyle = .bold
- index += 2
- case characters[index] == "*" && !hasPrefix(characters, at: index, token: ["*", "*"])
- && hasClosingSingleAsterisk(characters, startIndex: index + 1):
- flushBuffer()
- currentStyle = .italic
- index += 1
- case characters[index] == "`"
- && hasClosingDelimiter(characters, startIndex: index + 1, delimiter: ["`"]):
- flushBuffer()
- currentStyle = .code
- index += 1
- default:
- buffer.append(characters[index])
- index += 1
- }
- }
- }
-
- flushBuffer()
- return segments
+ return Text(attributed).font(fonts.normal)
}
-private func findInlineMathMatch(_ characters: [Character], startIndex: Int) -> InlineMathMatch? {
- if startIndex >= characters.count {
- return nil
- }
-
- if hasPrefix(characters, at: startIndex, token: ["\\", "("]) {
- var index = startIndex + 2
- while index < characters.count {
- if characters[index] == "\n" {
- return nil
- }
- if characters[index] == "\\" {
- if index + 1 >= characters.count {
- return nil
- }
- if characters[index + 1] == ")" {
- guard index > startIndex + 2 else { return nil }
- return InlineMathMatch(
- latex: String(characters[(startIndex + 2)..<index]),
- endIndex: index + 2
- )
- }
- index += 2
- continue
- }
- index += 1
+private func splitTextChunks(_ text: AttributedString) -> [AttributedString] {
+ var chunks: [AttributedString] = []
+ var start = text.startIndex
+ var previousWasWhitespace = false
+ for index in text.characters.indices {
+ let character = text.characters[index]
+ if previousWasWhitespace && !character.isWhitespace {
+ chunks.append(AttributedString(text[start..<index]))
+ start = index
}
- return nil
+ previousWasWhitespace = character.isWhitespace
}
-
- if characters[startIndex] != "$"
- || (startIndex > 0
- && (characters[startIndex - 1] == "\\" || characters[startIndex - 1] == "$"))
- {
- return nil
- }
- if startIndex + 1 < characters.count && characters[startIndex + 1] == "$" {
- return nil
- }
-
- var index = startIndex + 1
- while index < characters.count {
- if characters[index] == "\n" {
- return nil
- }
- if characters[index] == "$" && characters[index - 1] != "\\" {
- if index == startIndex + 1
- || (index + 1 < characters.count && characters[index + 1] == "$")
- {
- return nil
- }
- return InlineMathMatch(
- latex: String(characters[(startIndex + 1)..<index]),
- endIndex: index + 1
- )
- }
- index += 1
- }
-
- return nil
-}
-
-private func hasPrefix(_ characters: [Character], at startIndex: Int, token: [Character]) -> Bool {
- let endIndex = startIndex + token.count
- if startIndex < 0 || endIndex > characters.count {
- return false
- }
- return Array(characters[startIndex..<endIndex]) == token
-}
-
-private func hasClosingDelimiter(_ characters: [Character], startIndex: Int, delimiter: [Character])
- -> Bool
-{
- var index = startIndex
- while index < characters.count {
- if characters[index] == "\n" {
- return false
- }
- if hasPrefix(characters, at: index, token: delimiter) {
- return true
- }
- index += 1
- }
- return false
-}
-
-private func hasClosingSingleAsterisk(_ characters: [Character], startIndex: Int) -> Bool {
- var index = startIndex
- while index < characters.count {
- if characters[index] == "\n" {
- return false
- }
- if characters[index] == "*" && !hasPrefix(characters, at: index, token: ["*", "*"]) {
- return true
- }
- index += 1
- }
- return false
-}
-
-private func containsInlineMath(_ text: String) -> Bool {
- parseInlineSegments(text).contains {
- if case .math = $0 { return true }
- return false
- }
-}
-
-private func styledInlineText(
- _ content: String,
- style: InlineMarkdownStyle,
- fonts: InlineFontSet,
- textColor: Color
-) -> some View {
- switch style {
- case .normal:
- return AnyView(
- markdownText(content)
- .font(fonts.normal)
- .foregroundStyle(textColor)
- )
- case .bold:
- return AnyView(
- markdownText(content)
- .font(fonts.normal)
- .fontWeight(.semibold)
- .foregroundStyle(textColor)
- )
- case .italic:
- return AnyView(
- markdownText(content)
- .font(fonts.normal)
- .italic()
- .foregroundStyle(textColor)
- )
- case .code:
- return AnyView(
- Text(verbatim: content)
- .font(fonts.code)
- .foregroundStyle(textColor)
- )
- }
-}
-
-private func splitTextChunks(_ text: String) -> [String] {
- var chunks: [String] = []
- var current = ""
- for ch in text {
- if ch.isWhitespace {
- current.append(ch)
- } else {
- if !current.isEmpty && current.last?.isWhitespace == true {
- chunks.append(current)
- current = ""
- }
- current.append(ch)
- }
- }
- if !current.isEmpty {
- chunks.append(current)
+ if start != text.endIndex {
+ chunks.append(AttributedString(text[start..<text.endIndex]))
}
return chunks
}
private struct InlineMathTextView: View {
- let text: String
+ let text: InlineContent
let fonts: InlineFontSet
let textColor: Color
var body: some View {
VStack(alignment: .leading, spacing: 2) {
- ForEach(
- Array(text.split(separator: "\n", omittingEmptySubsequences: false).enumerated()),
- id: \.offset
- ) { _, rawLine in
- let line = String(rawLine)
+ ForEach(Array(text.lines.enumerated()), id: \.offset) { _, line in
FlowLayout(spacing: 0) {
- ForEach(Array(parseInlineSegments(line).enumerated()), id: \.offset) {
- _, segment in
+ ForEach(Array(line.segments.enumerated()), id: \.offset) { _, segment in
switch segment {
case .math(let latex):
InlineLaTeXView(latex: latex, fontSize: fonts.mathSize)
.fixedSize()
- case .text(let content, let style):
+ case .text(let content):
ForEach(Array(splitTextChunks(content).enumerated()), id: \.offset) {
_, chunk in
- styledInlineText(
- chunk, style: style, fonts: fonts, textColor: textColor)
+ inlineText(chunk, fonts: fonts)
+ .foregroundStyle(textColor)
}
}
}
@@ -1136,325 +892,6 @@ private struct InlineMathTextView: View {
}
}
-struct MarkdownBlock: Identifiable, Equatable {
- enum Kind: Equatable {
- case heading(level: Int, text: String)
- case paragraph(text: String)
- case blockquote(text: String)
- case code(text: String)
- case math(text: String)
- case list(items: [String])
- case divider
- }
-
- let id: Int
- let kind: Kind
-}
-
-enum MarkdownParser {
- static func parse(_ text: String) -> [MarkdownBlock] {
- var blocks: [MarkdownBlock] = []
- var nextId = 0
-
- func append(_ kind: MarkdownBlock.Kind) {
- blocks.append(MarkdownBlock(id: nextId, kind: kind))
- nextId += 1
- }
-
- let segments = text.components(separatedBy: "```")
-
- for (index, segment) in segments.enumerated() {
- if index % 2 == 1 {
- let code = segment.trimmingCharacters(in: .whitespacesAndNewlines)
- if !code.isEmpty {
- append(.code(text: code))
- }
- continue
- }
-
- for piece in splitByMathBlocks(segment) {
- switch piece {
- case .math(let latex):
- let trimmed = latex.trimmingCharacters(in: .whitespacesAndNewlines)
- if !trimmed.isEmpty {
- append(.math(text: trimmed))
- }
- case .markdown(let markdown):
- parseMarkdownBlocks(markdown).forEach { append($0) }
- }
- }
- }
-
- return blocks
- }
-
- private enum Segment {
- case markdown(String)
- case math(String)
- }
-
- private static func splitByMathBlocks(_ text: String) -> [Segment] {
- let lines = text.split(separator: "\n", omittingEmptySubsequences: false).map(String.init)
- var segments: [Segment] = []
- var markdownLines: [String] = []
- var mathLines: [String] = []
- var mathEndDelimiter: String? = nil
-
- func flushMarkdown() {
- if !markdownLines.isEmpty {
- segments.append(.markdown(markdownLines.joined(separator: "\n")))
- markdownLines.removeAll()
- }
- }
-
- func flushMath() {
- if !mathLines.isEmpty {
- segments.append(.math(mathLines.joined(separator: "\n")))
- }
- mathLines.removeAll()
- mathEndDelimiter = nil
- }
-
- func startMath(endDelimiter: String, initialContent: String? = nil) {
- flushMarkdown()
- mathEndDelimiter = endDelimiter
- mathLines.removeAll()
- if let initial = initialContent?.trimmingCharacters(in: .whitespacesAndNewlines),
- !initial.isEmpty
- {
- mathLines.append(initial)
- }
- }
-
- func isBracketMathLine(_ trimmed: String) -> Bool {
- guard trimmed.hasPrefix("["), trimmed.hasSuffix("]"), trimmed.count > 2 else {
- return false
- }
- if trimmed.contains("](") || trimmed.contains("]:") {
- return false
- }
- return true
- }
-
- for line in lines {
- let trimmed = line.trimmingCharacters(in: .whitespaces)
-
- if let endDelimiter = mathEndDelimiter {
- if trimmed == endDelimiter {
- flushMath()
- continue
- }
- if endDelimiter != "]" && trimmed.hasSuffix(endDelimiter) {
- let content = String(trimmed.dropLast(endDelimiter.count)).trimmingCharacters(
- in: .whitespaces)
- if !content.isEmpty {
- mathLines.append(content)
- }
- flushMath()
- continue
- }
- mathLines.append(line)
- continue
- }
-
- if trimmed == "\\[" || trimmed == "$$" || trimmed == "[" {
- let endDelimiter = trimmed == "\\[" ? "\\]" : (trimmed == "$$" ? "$$" : "]")
- startMath(endDelimiter: endDelimiter)
- continue
- }
-
- if trimmed.hasPrefix("\\[") {
- let content = String(trimmed.dropFirst(2)).trimmingCharacters(in: .whitespaces)
- if content.hasSuffix("\\]") {
- let inner = String(content.dropLast(2)).trimmingCharacters(in: .whitespaces)
- flushMarkdown()
- segments.append(.math(inner))
- } else {
- startMath(endDelimiter: "\\]", initialContent: content)
- }
- continue
- }
-
- if trimmed.hasPrefix("$$") {
- let content = String(trimmed.dropFirst(2)).trimmingCharacters(in: .whitespaces)
- if content.hasSuffix("$$") {
- let inner = String(content.dropLast(2)).trimmingCharacters(in: .whitespaces)
- flushMarkdown()
- segments.append(.math(inner))
- } else {
- startMath(endDelimiter: "$$", initialContent: content)
- }
- continue
- }
-
- if isBracketMathLine(trimmed) {
- let inner = String(trimmed.dropFirst().dropLast()).trimmingCharacters(
- in: .whitespaces)
- flushMarkdown()
- segments.append(.math(inner))
- continue
- }
-
- markdownLines.append(line)
- }
-
- if mathEndDelimiter != nil {
- flushMath()
- } else {
- flushMarkdown()
- }
-
- return segments
- }
-
- private static func parseMarkdownBlocks(_ markdown: String) -> [MarkdownBlock.Kind] {
- guard !markdown.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return [] }
- let document = Document(parsing: markdown)
- var parsedBlocks: [MarkdownBlock.Kind] = []
- for child in document.children {
- parsedBlocks.append(contentsOf: blocks(for: child))
- }
- return parsedBlocks
- }
-
- private static func blocks(for markup: Markup) -> [MarkdownBlock.Kind] {
- switch markup {
- case let heading as Heading:
- let text = renderInlineChildren(heading)
- guard !text.isEmpty else { return [] }
- let level = max(1, min(heading.level, 3))
- return [.heading(level: level, text: text)]
- case let paragraph as Paragraph:
- let text = renderInlineChildren(paragraph)
- guard !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return [] }
- return [.paragraph(text: text)]
- case let blockQuote as BlockQuote:
- let text = renderBlockQuote(blockQuote)
- guard !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return [] }
- return [.blockquote(text: text)]
- case let codeBlock as CodeBlock:
- let code = codeBlock.code.trimmingCharacters(in: .whitespacesAndNewlines)
- guard !code.isEmpty else { return [] }
- return [.code(text: code)]
- case _ as ThematicBreak:
- return [.divider]
- case let orderedList as OrderedList:
- let items = orderedList.children.compactMap { $0 as? ListItem }.map(renderListItem)
- .filter { !$0.isEmpty }
- return items.isEmpty ? [] : [.list(items: items)]
- case let unorderedList as UnorderedList:
- let items = unorderedList.children.compactMap { $0 as? ListItem }.map(renderListItem)
- .filter { !$0.isEmpty }
- return items.isEmpty ? [] : [.list(items: items)]
- default:
- var nestedBlocks: [MarkdownBlock.Kind] = []
- for child in markup.children {
- nestedBlocks.append(contentsOf: blocks(for: child))
- }
- return nestedBlocks
- }
- }
-
- private static func renderBlockQuote(_ quote: BlockQuote) -> String {
- var parts: [String] = []
- for child in quote.children {
- if let paragraph = child as? Paragraph {
- let text = renderInlineChildren(paragraph)
- if !text.isEmpty {
- parts.append(text)
- }
- continue
- }
- if let heading = child as? Heading {
- let text = renderInlineChildren(heading)
- if !text.isEmpty {
- parts.append(text)
- }
- continue
- }
- if let list = child as? OrderedList {
- let items = list.children.compactMap { $0 as? ListItem }.map(renderListItem).filter
- { !$0.isEmpty }
- if !items.isEmpty {
- parts.append(items.joined(separator: "\n"))
- }
- continue
- }
- if let list = child as? UnorderedList {
- let items = list.children.compactMap { $0 as? ListItem }.map(renderListItem).filter
- { !$0.isEmpty }
- if !items.isEmpty {
- parts.append(items.joined(separator: "\n"))
- }
- continue
- }
- }
- return parts.joined(separator: "\n")
- }
-
- private static func renderListItem(_ item: ListItem) -> String {
- var parts: [String] = []
- for child in item.children {
- if let paragraph = child as? Paragraph {
- let text = renderInlineChildren(paragraph)
- if !text.isEmpty {
- parts.append(text)
- }
- continue
- }
- if let list = child as? OrderedList {
- let items = list.children.compactMap { $0 as? ListItem }.map(renderListItem).filter
- { !$0.isEmpty }
- if !items.isEmpty {
- parts.append(items.joined(separator: "\n"))
- }
- continue
- }
- if let list = child as? UnorderedList {
- let items = list.children.compactMap { $0 as? ListItem }.map(renderListItem).filter
- { !$0.isEmpty }
- if !items.isEmpty {
- parts.append(items.joined(separator: "\n"))
- }
- continue
- }
- }
- return parts.joined(separator: "\n")
- }
-
- private static func renderInlineChildren(_ markup: Markup) -> String {
- markup.children.map(renderInline(from:)).joined()
- }
-
- private static func renderInline(from markup: Markup) -> String {
- switch markup {
- case let text as Markdown.Text:
- return text.string
- case _ as SoftBreak:
- return " "
- case _ as LineBreak:
- return "\n"
- case let emphasis as Emphasis:
- return "*" + renderInlineChildren(emphasis) + "*"
- case let strong as Strong:
- return "**" + renderInlineChildren(strong) + "**"
- case let inlineCode as InlineCode:
- return "`\(inlineCode.code)`"
- case let strikethrough as Strikethrough:
- return "~~" + renderInlineChildren(strikethrough) + "~~"
- case let link as Markdown.Link:
- let label = renderInlineChildren(link)
- let destination = link.destination ?? ""
- return destination.isEmpty ? label : "[\(label)](\(destination))"
- default:
- if !markup.children.contains(where: { _ in true }) {
- return ""
- }
- return renderInlineChildren(markup)
- }
- }
-}
-
struct CodeBlockView: View {
let code: String
@@ -1510,18 +947,18 @@ struct MathBlockView: View {
}
struct BlockQuoteView: View {
- let text: String
+ let text: InlineContent
var body: some View {
Group {
- if containsInlineMath(text) {
+ if text.hasMath {
InlineMathTextView(
text: text,
fonts: messageInlineFonts,
textColor: EnsuColor.textPrimary
)
} else {
- markdownText(text)
+ Text(text.attributedText)
.font(EnsuTypography.message)
.foregroundStyle(EnsuColor.textPrimary)
}
diff --git a/apple/apps/ensu/Ensu/Chat/MessageList/MessageMarkdown.swift b/apple/apps/ensu/Ensu/Chat/MessageList/MessageMarkdown.swift
new file mode 100644
index 0000000000..e958f877a1
--- /dev/null
+++ b/apple/apps/ensu/Ensu/Chat/MessageList/MessageMarkdown.swift
@@ -0,0 +1,522 @@
+import Foundation
+import Markdown
+
+struct InlineContent: Equatable {
+ enum Segment: Equatable {
+ case text(AttributedString)
+ case math(String)
+ }
+
+ var segments: [Segment] = []
+
+ var isEmpty: Bool { segments.isEmpty }
+
+ var hasMath: Bool {
+ segments.contains {
+ if case .math = $0 { return true }
+ return false
+ }
+ }
+
+ var attributedText: AttributedString {
+ segments.reduce(into: AttributedString()) { result, segment in
+ if case .text(let text) = segment { result.append(text) }
+ }
+ }
+
+ func appending(_ text: String) -> Self {
+ Self(segments: segments + [.text(AttributedString(text))])
+ }
+
+ static func joined(_ items: [Self], separator: String) -> Self {
+ var result = Self()
+ for (index, item) in items.enumerated() {
+ if index > 0 { result = result.appending(separator) }
+ result.segments += item.segments
+ }
+ return result
+ }
+
+ var lines: [Self] {
+ var result = [Self()]
+ for segment in segments {
+ switch segment {
+ case .math:
+ result[result.count - 1].segments.append(segment)
+ case .text(let text):
+ for (index, line) in text.characters.split(
+ separator: "\n", omittingEmptySubsequences: false
+ ).enumerated() {
+ if index > 0 { result.append(Self()) }
+ if !line.isEmpty {
+ result[result.count - 1].segments.append(
+ .text(AttributedString(text[line.startIndex..<line.endIndex])))
+ }
+ }
+ }
+ }
+ return result
+ }
+}
+
+struct MarkdownBlock: Identifiable, Equatable {
+ enum Kind: Equatable {
+ case heading(level: Int, text: InlineContent)
+ case paragraph(text: InlineContent)
+ case blockquote(text: InlineContent)
+ case code(text: String)
+ case math(text: String)
+ case list(items: [InlineContent])
+ case divider
+ }
+
+ let id: Int
+ let kind: Kind
+}
+
+struct MarkdownParser {
+ private var math: [String: String] = [:]
+
+ static func parse(_ text: String) -> [MarkdownBlock] {
+ var blocks: [MarkdownBlock] = []
+ var nextId = 0
+
+ func append(_ kind: MarkdownBlock.Kind) {
+ blocks.append(MarkdownBlock(id: nextId, kind: kind))
+ nextId += 1
+ }
+
+ let segments = text.components(separatedBy: "```")
+
+ for (index, segment) in segments.enumerated() {
+ if index % 2 == 1 {
+ let code = segment.trimmingCharacters(in: .whitespacesAndNewlines)
+ if !code.isEmpty {
+ append(.code(text: code))
+ }
+ continue
+ }
+
+ for piece in splitByMathBlocks(segment) {
+ switch piece {
+ case .math(let latex):
+ let trimmed = latex.trimmingCharacters(in: .whitespacesAndNewlines)
+ if !trimmed.isEmpty {
+ append(.math(text: trimmed))
+ }
+ case .markdown(let markdown):
+ parseMarkdownBlocks(markdown).forEach { append($0) }
+ }
+ }
+ }
+
+ return blocks
+ }
+
+ private enum Segment {
+ case markdown(String)
+ case math(String)
+ }
+
+ private static func splitByMathBlocks(_ text: String) -> [Segment] {
+ let lines = text.split(separator: "\n", omittingEmptySubsequences: false).map(String.init)
+ var segments: [Segment] = []
+ var markdownLines: [String] = []
+ var mathLines: [String] = []
+ var mathEndDelimiter: String? = nil
+
+ func flushMarkdown() {
+ if !markdownLines.isEmpty {
+ segments.append(.markdown(markdownLines.joined(separator: "\n")))
+ markdownLines.removeAll()
+ }
+ }
+
+ func flushMath() {
+ if !mathLines.isEmpty {
+ segments.append(.math(mathLines.joined(separator: "\n")))
+ }
+ mathLines.removeAll()
+ mathEndDelimiter = nil
+ }
+
+ func startMath(endDelimiter: String, initialContent: String? = nil) {
+ flushMarkdown()
+ mathEndDelimiter = endDelimiter
+ mathLines.removeAll()
+ if let initial = initialContent?.trimmingCharacters(in: .whitespacesAndNewlines),
+ !initial.isEmpty
+ {
+ mathLines.append(initial)
+ }
+ }
+
+ func isBracketMathLine(_ trimmed: String) -> Bool {
+ guard trimmed.hasPrefix("["), trimmed.hasSuffix("]"), trimmed.count > 2 else {
+ return false
+ }
+ if trimmed.contains("](") || trimmed.contains("]:") {
+ return false
+ }
+ return true
+ }
+
+ for line in lines {
+ let trimmed = line.trimmingCharacters(in: .whitespaces)
+
+ if let endDelimiter = mathEndDelimiter {
+ if trimmed == endDelimiter {
+ flushMath()
+ continue
+ }
+ if endDelimiter != "]" && trimmed.hasSuffix(endDelimiter) {
+ let content = String(trimmed.dropLast(endDelimiter.count)).trimmingCharacters(
+ in: .whitespaces)
+ if !content.isEmpty {
+ mathLines.append(content)
+ }
+ flushMath()
+ continue
+ }
+ mathLines.append(line)
+ continue
+ }
+
+ if trimmed == "\\[" || trimmed == "$$" || trimmed == "[" {
+ let endDelimiter = trimmed == "\\[" ? "\\]" : (trimmed == "$$" ? "$$" : "]")
+ startMath(endDelimiter: endDelimiter)
+ continue
+ }
+
+ if trimmed.hasPrefix("\\[") {
+ let content = String(trimmed.dropFirst(2)).trimmingCharacters(in: .whitespaces)
+ if content.hasSuffix("\\]") {
+ let inner = String(content.dropLast(2)).trimmingCharacters(in: .whitespaces)
+ flushMarkdown()
+ segments.append(.math(inner))
+ } else {
+ startMath(endDelimiter: "\\]", initialContent: content)
+ }
+ continue
+ }
+
+ if trimmed.hasPrefix("$$") {
+ let content = String(trimmed.dropFirst(2)).trimmingCharacters(in: .whitespaces)
+ if content.hasSuffix("$$") {
+ let inner = String(content.dropLast(2)).trimmingCharacters(in: .whitespaces)
+ flushMarkdown()
+ segments.append(.math(inner))
+ } else {
+ startMath(endDelimiter: "$$", initialContent: content)
+ }
+ continue
+ }
+
+ if isBracketMathLine(trimmed) {
+ let inner = String(trimmed.dropFirst().dropLast()).trimmingCharacters(
+ in: .whitespaces)
+ flushMarkdown()
+ segments.append(.math(inner))
+ continue
+ }
+
+ markdownLines.append(line)
+ }
+
+ if mathEndDelimiter != nil {
+ flushMath()
+ } else {
+ flushMarkdown()
+ }
+
+ return segments
+ }
+
+ private static func parseMarkdownBlocks(_ markdown: String) -> [MarkdownBlock.Kind] {
+ guard !markdown.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return [] }
+ let (document, math) = protectInlineMath(markdown)
+ let parser = Self(math: math)
+ var parsedBlocks: [MarkdownBlock.Kind] = []
+ for child in document.children {
+ parsedBlocks.append(contentsOf: parser.blocks(for: child))
+ }
+ return parsedBlocks
+ }
+
+ private func blocks(for markup: Markup) -> [MarkdownBlock.Kind] {
+ switch markup {
+ case let heading as Heading:
+ let text = renderInlineChildren(heading)
+ guard !text.isEmpty else { return [] }
+ let level = max(1, min(heading.level, 3))
+ return [.heading(level: level, text: text)]
+ case let paragraph as Paragraph:
+ let text = renderInlineChildren(paragraph)
+ guard !text.isEmpty else { return [] }
+ return [.paragraph(text: text)]
+ case let blockQuote as BlockQuote:
+ let text = renderBlockQuote(blockQuote)
+ guard !text.isEmpty else { return [] }
+ return [.blockquote(text: text)]
+ case let codeBlock as CodeBlock:
+ let code = codeBlock.code.trimmingCharacters(in: .whitespacesAndNewlines)
+ guard !code.isEmpty else { return [] }
+ return [.code(text: code)]
+ case _ as ThematicBreak:
+ return [.divider]
+ case let orderedList as OrderedList:
+ let items = orderedList.children.compactMap { $0 as? ListItem }.map(renderListItem)
+ .filter { !$0.isEmpty }
+ return items.isEmpty ? [] : [.list(items: items)]
+ case let unorderedList as UnorderedList:
+ let items = unorderedList.children.compactMap { $0 as? ListItem }.map(renderListItem)
+ .filter { !$0.isEmpty }
+ return items.isEmpty ? [] : [.list(items: items)]
+ default:
+ var nestedBlocks: [MarkdownBlock.Kind] = []
+ for child in markup.children {
+ nestedBlocks.append(contentsOf: blocks(for: child))
+ }
+ return nestedBlocks
+ }
+ }
+
+ private func renderBlockQuote(_ quote: BlockQuote) -> InlineContent {
+ var parts: [InlineContent] = []
+ for child in quote.children {
+ if let paragraph = child as? Paragraph {
+ let text = renderInlineChildren(paragraph)
+ if !text.isEmpty {
+ parts.append(text)
+ }
+ continue
+ }
+ if let heading = child as? Heading {
+ let text = renderInlineChildren(heading)
+ if !text.isEmpty {
+ parts.append(text)
+ }
+ continue
+ }
+ if let list = child as? OrderedList {
+ let items = list.children.compactMap { $0 as? ListItem }.map(renderListItem).filter
+ { !$0.isEmpty }
+ if !items.isEmpty {
+ parts.append(InlineContent.joined(items, separator: "\n"))
+ }
+ continue
+ }
+ if let list = child as? UnorderedList {
+ let items = list.children.compactMap { $0 as? ListItem }.map(renderListItem).filter
+ { !$0.isEmpty }
+ if !items.isEmpty {
+ parts.append(InlineContent.joined(items, separator: "\n"))
+ }
+ continue
+ }
+ }
+ return InlineContent.joined(parts, separator: "\n")
+ }
+
+ private func renderListItem(_ item: ListItem) -> InlineContent {
+ var parts: [InlineContent] = []
+ for child in item.children {
+ if let paragraph = child as? Paragraph {
+ let text = renderInlineChildren(paragraph)
+ if !text.isEmpty {
+ parts.append(text)
+ }
+ continue
+ }
+ if let list = child as? OrderedList {
+ let items = list.children.compactMap { $0 as? ListItem }.map(renderListItem).filter
+ { !$0.isEmpty }
+ if !items.isEmpty {
+ parts.append(InlineContent.joined(items, separator: "\n"))
+ }
+ continue
+ }
+ if let list = child as? UnorderedList {
+ let items = list.children.compactMap { $0 as? ListItem }.map(renderListItem).filter
+ { !$0.isEmpty }
+ if !items.isEmpty {
+ parts.append(InlineContent.joined(items, separator: "\n"))
+ }
+ continue
+ }
+ }
+ return InlineContent.joined(parts, separator: "\n")
+ }
+
+ private func renderInlineChildren(
+ _ markup: Markup, attributes: AttributeContainer = AttributeContainer()
+ ) -> InlineContent {
+ InlineContent(
+ segments: markup.children.flatMap {
+ renderInline(from: $0, attributes: attributes).segments
+ })
+ }
+
+ private func renderInline(from markup: Markup, attributes: AttributeContainer) -> InlineContent
+ {
+ if let image = markup as? Markdown.Image, let source = image.source,
+ let latex = math[source]
+ {
+ return InlineContent(segments: [.math(latex)])
+ }
+ var attributes = attributes
+ var intent = attributes.inlinePresentationIntent ?? []
+ switch markup {
+ case let text as Markdown.Text:
+ return InlineContent(segments: [
+ .text(AttributedString(text.string, attributes: attributes))
+ ])
+ case is SoftBreak:
+ return InlineContent(segments: [.text(AttributedString(" ", attributes: attributes))])
+ case is LineBreak:
+ return InlineContent(segments: [.text(AttributedString("\n", attributes: attributes))])
+ case let code as InlineCode:
+ intent.insert(.code)
+ attributes.inlinePresentationIntent = intent
+ return InlineContent(segments: [
+ .text(AttributedString(code.code, attributes: attributes))
+ ])
+ case is Emphasis:
+ intent.insert(.emphasized)
+ case is Strong:
+ intent.insert(.stronglyEmphasized)
+ case is Strikethrough:
+ intent.insert(.strikethrough)
+ case let link as Markdown.Link:
+ attributes.link = link.destination.flatMap(URL.init(string:))
+ default:
+ break
+ }
+ attributes.inlinePresentationIntent = intent
+ return renderInlineChildren(markup, attributes: attributes)
+ }
+}
+
+private func protectInlineMath(_ markdown: String) -> (Document, [String: String]) {
+ let document = Document(parsing: markdown)
+ var textRanges: [SourceRange] = []
+ func collectTextRanges(_ markup: Markup) {
+ if let text = markup as? Markdown.Text, let range = text.range {
+ textRanges.append(range)
+ } else if (markup as? Markdown.Link)?.isAutolink != true {
+ for child in markup.children { collectTextRanges(child) }
+ }
+ }
+ collectTextRanges(document)
+
+ let bytes = Array(markdown.utf8)
+ let prefix = UUID().uuidString
+ var protected: [UInt8] = []
+ var math: [String: String] = [:]
+ var location = SourceLocation(line: 1, column: 1, source: nil)
+ var rangeIndex = 0
+ var index = 0
+ while index < bytes.count {
+ while rangeIndex < textRanges.count && textRanges[rangeIndex].upperBound <= location {
+ rangeIndex += 1
+ }
+ let isText =
+ rangeIndex < textRanges.count && textRanges[rangeIndex].contains(location)
+ if isText, let match = inlineMathMatch(bytes, at: index) {
+ let id = "\(prefix)-\(math.count)"
+ math[id] = match.latex
+ // Image nodes keep math opaque while Markdown parses the surrounding formatting.
+ protected += "![](\(id))".utf8
+ location.column += match.end - index
+ index = match.end
+ } else if bytes[index] == UInt8(ascii: "\\") && index + 1 < bytes.count
+ && bytes[index + 1] != UInt8(ascii: "\n") && bytes[index + 1] != UInt8(ascii: "\r")
+ {
+ protected += bytes[index...index + 1]
+ index += 2
+ location.column += 2
+ } else {
+ protected.append(bytes[index])
+ if bytes[index] == UInt8(ascii: "\r")
+ || (bytes[index] == UInt8(ascii: "\n")
+ && (index == 0 || bytes[index - 1] != UInt8(ascii: "\r")))
+ {
+ location.line += 1
+ location.column = 1
+ } else if bytes[index] != UInt8(ascii: "\n") {
+ location.column += 1
+ }
+ index += 1
+ }
+ }
+ guard !math.isEmpty else { return (document, math) }
+ let rewritten = Document(parsing: String(decoding: protected, as: UTF8.self))
+ var recovered: Set<String> = []
+ func literalContent(_ markup: Markup) -> [String] {
+ var content: [String] = []
+ switch markup {
+ case let image as Markdown.Image:
+ if let source = image.source, math[source] != nil {
+ recovered.insert(source)
+ return []
+ }
+ content = ["image", image.source ?? "", image.title ?? ""]
+ case let link as Markdown.Link:
+ content = ["link", link.destination ?? "", link.title ?? ""]
+ case let code as InlineCode:
+ content = ["code", code.code]
+ case let code as CodeBlock:
+ content = ["code block", code.code]
+ case let html as InlineHTML:
+ content = ["html", html.rawHTML]
+ case let html as HTMLBlock:
+ content = ["html block", html.rawHTML]
+ default:
+ break
+ }
+ return content + markup.children.flatMap(literalContent)
+ }
+ // Source ranges can drift on continuation lines.
+ guard literalContent(document) == literalContent(rewritten), recovered.count == math.count
+ else {
+ return (Document(Paragraph(Markdown.Text(markdown))), [:])
+ }
+ return (rewritten, math)
+}
+
+private func inlineMathMatch(_ bytes: [UInt8], at start: Int) -> (latex: String, end: Int)? {
+ let parenthesized =
+ bytes[start] == UInt8(ascii: "\\") && start + 1 < bytes.count
+ && bytes[start + 1] == UInt8(ascii: "(")
+ if !parenthesized {
+ guard bytes[start] == UInt8(ascii: "$"),
+ (start == 0 || bytes[start - 1] != UInt8(ascii: "$")),
+ start + 1 < bytes.count, bytes[start + 1] != UInt8(ascii: "$")
+ else { return nil }
+ }
+ let contentStart = start + (parenthesized ? 2 : 1)
+ var index = contentStart
+ while index < bytes.count {
+ if bytes[index] == UInt8(ascii: "\n") || bytes[index] == UInt8(ascii: "\r") { return nil }
+ if parenthesized && bytes[index] == UInt8(ascii: "\\") && index + 1 < bytes.count
+ && bytes[index + 1] == UInt8(ascii: ")")
+ {
+ guard index > contentStart else { return nil }
+ return (String(decoding: bytes[contentStart..<index], as: UTF8.self), index + 2)
+ }
+ if bytes[index] == UInt8(ascii: "\\") {
+ index += 2
+ continue
+ }
+ if !parenthesized && bytes[index] == UInt8(ascii: "$") {
+ guard index > contentStart,
+ index + 1 == bytes.count || bytes[index + 1] != UInt8(ascii: "$")
+ else {
+ return nil
+ }
+ return (String(decoding: bytes[contentStart..<index], as: UTF8.self), index + 1)
+ }
+ index += 1
+ }
+ return nil
+}
diff --git a/apple/apps/ensu/Ensu/Settings/LogsView.swift b/apple/apps/ensu/Ensu/Settings/LogsView.swift
index 7726c91bf4..cb05148f04 100644
--- a/apple/apps/ensu/Ensu/Settings/LogsView.swift
+++ b/apple/apps/ensu/Ensu/Settings/LogsView.swift
@@ -45,7 +45,7 @@ struct LogsView: View {
}
}
.task {
- refreshEntries()
+ entries = await Self.loadEntries()
}
.sheet(item: $shareArchive) { archive in
ActivityView(activityItems: [archive.url])
@@ -124,11 +124,9 @@ struct LogsView: View {
}
}
- private func refreshEntries() {
- entries = parseLogText(EnsuLogging.shared.readLogText())
- }
-
- private func parseLogText(_ text: String) -> [EnsuLogEntry] {
+ @concurrent
+ private static func loadEntries() async -> [EnsuLogEntry] {
+ let text = EnsuLogging.shared.readLogText()
var entries: [EnsuLogEntry] = []
for line in text.components(separatedBy: .newlines) {
guard !line.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { continue }
@@ -142,7 +140,8 @@ struct LogsView: View {
{
entries.append(
EnsuLogEntry(
- timestamp: logLineFormatter.date(from: String(line[timestampRange])) ?? Date(),
+ timestamp: logLineFormatter.date(from: String(line[timestampRange]))
+ ?? Date(),
level: EnsuLogLevel(rawValue: String(line[levelRange])) ?? .info,
tag: String(line[tagRange]),
message: String(line[messageRange]),
diff --git a/apple/apps/ensu/EnsuTests/MessageMarkdownTests.swift b/apple/apps/ensu/EnsuTests/MessageMarkdownTests.swift
new file mode 100644
index 0000000000..077b67bb16
--- /dev/null
+++ b/apple/apps/ensu/EnsuTests/MessageMarkdownTests.swift
@@ -0,0 +1,98 @@
+import Foundation
+import XCTest
+@testable import Ensu
+
+final class MessageMarkdownTests: XCTestCase {
+ func testInlineMathPreservesEscapesAndCode() {
+ let cases: [(String, [String], String)] = [
+ (#"Inline: \(a+b\)."#, ["a+b"], "Inline: ."),
+ (#"Escaped: \$5 and \$10; $x^2$."#, ["x^2"], "Escaped: $5 and $10; ."),
+ (#"Empty: \(\); $$; before $$x$$ after."#, [], "Empty: (); $$; before $$x$$ after."),
+ (#"Code: `$x$` and `\(y\)`; $z$."#, ["z"], #"Code: $x$ and \(y\); ."#),
+ (#"Code: `` `$x$` ``; \(z\)."#, ["z"], "Code: `$x$`; ."),
+ (#"Math: $\frac{a_b}{c_d} + *x*$."#, [#"\frac{a_b}{c_d} + *x*"#], "Math: ."),
+ (#"Escaped opener: \\(x\\); $y$."#, ["y"], #"Escaped opener: \(x\); ."#),
+ (#"Unicode: café 🙂 \(α + β\), $γ$."#, ["α + β", "γ"], "Unicode: café 🙂 , ."),
+ (#"Streaming: \(a+b"#, [], "Streaming: (a+b"),
+ (#"Streaming: $a+b"#, [], "Streaming: $a+b"),
+ (#"Unclosed code: `$x$."#, ["x"], "Unclosed code: `."),
+ (#"Literal tick: \`$x$."#, ["x"], "Literal tick: `."),
+ (#"URL: <https://example.com/$a$>; $x$."#, ["x"], "URL: https://example.com/$a$; ."),
+ ("a $x$\n b $y$", ["x", "y"], "a b "),
+ ("a $x$\r\n b $y$", ["x", "y"], "a b "),
+ ("a $x$\n b $y$", ["x"], "a b $y$"),
+ ("a $x$\\\nb $y$", ["x"], "a \nb $y$"),
+ ("![0]() and $x$", ["x"], "0 and "),
+ ]
+ for (source, math, text) in cases {
+ let inline = paragraph(source)
+ XCTAssertEqual(mathSpans(inline), math, source)
+ XCTAssertEqual(String(inline.attributedText.characters), text, source)
+ }
+ }
+
+ func testCodeInsideEmphasisRemainsLiteral() {
+ let inline = paragraph(#"**bold `$x$` and *italic `\(y\)`***; $z$."#)
+ XCTAssertEqual(mathSpans(inline), ["z"])
+ XCTAssertEqual(String(inline.attributedText.characters), #"bold $x$ and italic \(y\); ."#)
+ let code = inline.attributedText.runs.filter {
+ $0.inlinePresentationIntent?.contains(.code) == true
+ }
+ XCTAssertEqual(code.count, 2)
+ XCTAssertTrue(
+ code.allSatisfy { $0.inlinePresentationIntent?.contains(.stronglyEmphasized) == true })
+ XCTAssertTrue(code.last?.inlinePresentationIntent?.contains(.emphasized) == true)
+ }
+
+ func testUnsafeMathRewritePreservesOriginalText() {
+ let source = "a $x$\n b `$y$` and $z$"
+ let inline = paragraph(source)
+ XCTAssertEqual(String(inline.attributedText.characters), source)
+ XCTAssertTrue(mathSpans(inline).isEmpty)
+ }
+
+ func testMathInBlocksAndLinkLabels() {
+ let blocks = MarkdownParser.parse(
+ "# Heading \\(x\\)\n\n- Item $y$\n\n> Quote \\(z\\)\ncontinued $q$\n\n$$\\frac{1}{2}$$")
+ XCTAssertEqual(blocks.count, 4)
+ guard blocks.count == 4, case .heading(_, let heading) = blocks[0].kind,
+ case .list(let items) = blocks[1].kind,
+ case .blockquote(let quote) = blocks[2].kind,
+ case .math(let display) = blocks[3].kind
+ else { return XCTFail("Unexpected block kinds") }
+ XCTAssertEqual(mathSpans(heading), ["x"])
+ XCTAssertEqual(items.map(mathSpans), [["y"]])
+ XCTAssertEqual(mathSpans(quote), ["z", "q"])
+ XCTAssertEqual(display, #"\frac{1}{2}"#)
+
+ let inline = paragraph(#"See [math $x$](https://example.com/$a$) and **bold**."#)
+ XCTAssertEqual(mathSpans(inline), ["x"])
+ XCTAssertTrue(
+ inline.attributedText.runs.contains {
+ $0.link?.absoluteString == "https://example.com/$a$"
+ })
+
+ let reference = paragraph("See [link][ref] and $x$.\n\n[ref]: https://example.com/$a$")
+ XCTAssertEqual(mathSpans(reference), ["x"])
+ XCTAssertTrue(
+ reference.attributedText.runs.contains {
+ $0.link?.absoluteString == "https://example.com/$a$"
+ })
+ }
+
+ private func paragraph(_ source: String) -> InlineContent {
+ let blocks = MarkdownParser.parse(source)
+ guard blocks.count == 1, case .paragraph(let inline) = blocks[0].kind else {
+ XCTFail("Expected one paragraph: \(source)")
+ return InlineContent()
+ }
+ return inline
+ }
+
+ private func mathSpans(_ inline: InlineContent) -> [String] {
+ inline.segments.compactMap {
+ if case .math(let latex) = $0 { return latex }
+ return nil
+ }
+ }
+}