Skip to content

Commit c945406

Browse files
committed
Fix attachments without newline
1 parent 39550b3 commit c945406

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

Modules/Sources/BBBuilder/Builder/BBBuilder.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,14 @@ public struct BBBuilder {
113113
let isNextNodeMedia = nodes[safe: index + 1]?.isMedia ?? false
114114
if isNextNodeTextable || isNextNodeMedia {
115115
logger.info("Next node is textable OR media, unwrapping")
116-
var isAttachmentDelimeneter = false
117-
if case .attachment = nodes[safe: index - 1] { isAttachmentDelimeneter = true }
116+
var isAttachmentDelimeter = false
117+
if case .attachment = nodes[safe: index - 1] {
118+
isAttachmentDelimeter = true
119+
}
118120
mergedNodes[mergedNodes.count - 1] = unwrap(
119121
node: node,
120122
with: mutableText,
121-
isAttachmentDelimeter: isAttachmentDelimeneter,
123+
isAttachmentDelimeter: isAttachmentDelimeter,
122124
listType: listType
123125
)
124126
if listType != nil,
@@ -156,11 +158,15 @@ public struct BBBuilder {
156158
mergedNodes.lastOrAppend = unwrap(node: textNode, with: mutableText)
157159
continue
158160
}
159-
let attachmentType = attachments[attachmentIndex].type
160-
switch attachmentType {
161+
162+
switch attachments[attachmentIndex].type {
161163
case .file:
162164
logger.info("FILE attachment")
163-
let textNode = unwrap(node: node, with: mutableText)
165+
var isAttachmentDelimeter = false
166+
if case let .text(text) = mergedNodes.last, text.string.last != "\n" {
167+
isAttachmentDelimeter = true
168+
}
169+
let textNode = unwrap(node: node, with: mutableText, isAttachmentDelimeter: isAttachmentDelimeter)
164170
if !textNode.isEmptyText {
165171
mergedNodes.lastOrAppend = textNode
166172
} else {
@@ -169,7 +175,7 @@ public struct BBBuilder {
169175
continue
170176

171177
case .image:
172-
break
178+
break // TODO: Move image attachment below here
173179
}
174180

175181
logger.info("IMAGE attachment")
@@ -392,6 +398,10 @@ public struct BBBuilder {
392398
.foregroundColor: UIColor(resource: .Labels.teritary)
393399
]))
394400
}
401+
402+
if isAttachmentDelimeter {
403+
mutableString.insert(NSAttributedString(string: "\n"), at: 0)
404+
}
395405

396406
attachmentString = mutableString
397407
}

Modules/Tests/BBBuilderTests/BBBuilderTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,18 @@ struct BBBuilderTests {
219219
Issue.record("First node is not text or there's more nodes")
220220
}
221221
}
222+
223+
@Test func fileAttachmentsWithoutNewlineAreNewlined() async throws {
224+
let firstId = 0
225+
let secondId = 1
226+
let text = String.fileAttachment(id: firstId) + String.fileAttachment(id: secondId)
227+
let nodes = BBBuilder.build(text: text, attachments: [.file(id: firstId), .file(id: secondId)])
228+
if case let .text(text) = nodes.first, nodes.count == 1 {
229+
#expect(text.string.contains("\n"))
230+
} else {
231+
Issue.record("First node is not text or there's more nodes")
232+
}
233+
}
222234
}
223235

224236
// MARK: - Helpers

0 commit comments

Comments
 (0)