Skip to content

Commit a341d76

Browse files
committed
Fix bb parser list tag counting
1 parent 81e24c8 commit a341d76

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

Modules/Sources/BBBuilder/Builder/BBBuilder.swift

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ func timeElapsed(from start: DispatchTime) -> String {
1111
return String(format: "%.2f ms", elapsedTimeInMilliSeconds)
1212
}
1313

14+
struct ListInfo {
15+
var count = 0
16+
let type: BBContainerNode.ListType
17+
}
18+
1419
public struct BBBuilder {
1520

1621
// MARK: - Build Interface
@@ -38,8 +43,9 @@ public struct BBBuilder {
3843

3944
// MARK: - Implementation
4045

41-
private func mergeTextNodes(_ nodes: [BBContainerNode], listType: BBContainerNode.ListType? = nil) -> [BBContainerNode] {
46+
private func mergeTextNodes(_ nodes: [BBContainerNode], listInfo: ListInfo? = nil) -> [BBContainerNode] {
4247
var mergedNodes: [BBContainerNode] = []
48+
var listInfo = listInfo
4349

4450
func hasPreviousNode(_ index: Int) -> Bool {
4551
return nodes[safe: index - 1] != nil
@@ -62,6 +68,12 @@ public struct BBBuilder {
6268
switch node {
6369
case .text, .snapback, .mergetime, .smile:
6470
logger.info("In textable node")
71+
72+
if listInfo != nil, node.isListTag {
73+
logger.info("List tag, increasing counter")
74+
listInfo?.count += 1
75+
}
76+
6577
if mutableText.string.isEmpty {
6678
var trimLeading = false
6779
var trimTrailing = false
@@ -96,7 +108,7 @@ public struct BBBuilder {
96108
let textNode = unwrap(
97109
node: node,
98110
with: mutableText,
99-
listType: listType,
111+
listInfo: listInfo,
100112
trimLeading: trimLeading,
101113
trimTrailing: trimTrailing
102114
)
@@ -121,9 +133,9 @@ public struct BBBuilder {
121133
node: node,
122134
with: mutableText,
123135
isAttachmentDelimeter: isAttachmentDelimeter,
124-
listType: listType
136+
listInfo: listInfo
125137
)
126-
if listType != nil,
138+
if listInfo != nil,
127139
case let .text(text) = nodes[safe: index - 1],
128140
text.string == "[*]",
129141
case let .text(text) = node,
@@ -136,7 +148,7 @@ public struct BBBuilder {
136148
mergedNodes[mergedNodes.count - 1] = unwrap(
137149
node: node,
138150
with: mutableText,
139-
listType: listType,
151+
listInfo: listInfo,
140152
trimTrailing: true
141153
)
142154
}
@@ -253,7 +265,7 @@ public struct BBBuilder {
253265
mergedNodes.append(.quote(attributed, mergeTextNodes(array)))
254266

255267
case .list(let type, let array):
256-
mergedNodes.append(.list(type, mergeTextNodes(array, listType: type)))
268+
mergedNodes.append(.list(type, mergeTextNodes(array, listInfo: ListInfo(type: type))))
257269

258270
case .code(let title, let array):
259271
mergedNodes.append(.code(title, mergeTextNodes(array)))
@@ -283,7 +295,7 @@ public struct BBBuilder {
283295
with combinedText: NSAttributedString,
284296
isFirst: Bool = false,
285297
isAttachmentDelimeter: Bool = false,
286-
listType: BBContainerNode.ListType? = nil,
298+
listInfo: ListInfo? = nil,
287299
trimLeading: Bool = false,
288300
trimTrailing: Bool = false
289301
) -> BBContainerNode {
@@ -303,7 +315,7 @@ public struct BBBuilder {
303315
with combinedText: NSMutableAttributedString,
304316
isFirst: Bool = false,
305317
isAttachmentDelimeter: Bool = false,
306-
listType: BBContainerNode.ListType? = nil,
318+
listInfo: ListInfo? = nil,
307319
trimLeading: Bool = false,
308320
trimTrailing: Bool = false
309321
) -> BBContainerNode {
@@ -321,8 +333,8 @@ public struct BBBuilder {
321333
mutableString.replaceCharacters(in: NSRange(location: 0, length: 1), with: "\n")
322334
}
323335
}
324-
if let listType {
325-
mutableString.replaceListTags(of: listType)
336+
if let listInfo {
337+
mutableString.replaceListTags(for: listInfo)
326338
}
327339
if isFirst {
328340
return .text(mutableString)
@@ -504,14 +516,12 @@ extension NSAttributedString {
504516
}
505517

506518
extension NSMutableAttributedString {
507-
func replaceListTags(of type: BBContainerNode.ListType) {
519+
func replaceListTags(for info: ListInfo) {
508520
let target = "[*]"
509521

510-
var counter = 0
511522
while let range = string.range(of: target) {
512523
let nsRange = NSRange(range, in: string)
513-
replaceCharacters(in: nsRange, with: getReplacement(for: type, at: counter))
514-
counter += 1
524+
replaceCharacters(in: nsRange, with: getReplacement(for: info.type, at: info.count))
515525
}
516526
}
517527

@@ -520,7 +530,7 @@ extension NSMutableAttributedString {
520530
case .bullet:
521531
return ""
522532
case .numeric:
523-
return "\(index + 1). "
533+
return "\(index). "
524534
case .alphabet:
525535
return "\(Character(UnicodeScalar(96 + index)!)). "
526536
case .romanBig:

Modules/Sources/BBBuilder/Parser/Attributed/BBContainerNode.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ public enum BBContainerNode: Equatable {
9595
return startsWithSpace || startsWithNewline
9696
}
9797

98+
public var isListTag: Bool {
99+
if case let .text(text) = self {
100+
return text.string == "[*]"
101+
}
102+
return false
103+
}
104+
98105
init?(tag: BBTag, attribute: AttributedString?, children: [BBContainerNode]) {
99106
self.init(tag: tag, attribute: attribute.map { NSAttributedString($0) }, children: children)
100107
}

0 commit comments

Comments
 (0)