Skip to content
This repository was archived by the owner on Aug 11, 2024. It is now read-only.

Commit 6b72afe

Browse files
committed
Remove blocks parameter & test new call signature
1 parent df7e30a commit 6b72afe

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

xcode/Subconscious/Shared/Components/Common/SubtextView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct SubtextView: View {
3535
}
3636

3737
var blocks: [RenderableBlock] {
38-
Self.renderer.render(subtext, blocks: subtext.blocks)
38+
Self.renderer.render(subtext)
3939
.compactMap { rendered in
4040
guard !rendered.block.isEmpty else {
4141
return nil

xcode/Subconscious/Shared/Parsers/SubtextAttributedStringRenderer.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,14 @@ struct SubtextAttributedStringRenderer {
167167
return markup
168168
}
169169

170-
func render(_ subtext: Subtext, blocks: [Subtext.Block]) -> [Subtext.RenderedBlock] {
170+
// Convenience method to re-use parsed Subtext and return each block rendered independently.
171+
// This is a peformance optimization for SubtextView.
172+
func render(_ subtext: Subtext) -> [Subtext.RenderedBlock] {
171173
// Initial contiguous AttributedString creation with all attributes applied
172174
var markup = AttributedString(subtext.base)
173175
markup.font = .body
174176

175-
for block in blocks {
177+
for block in subtext.blocks {
176178
renderAttributesOf(attributedString: &markup, block: block)
177179
for inline in block.inline {
178180
renderAttributesOf(
@@ -186,7 +188,7 @@ struct SubtextAttributedStringRenderer {
186188
var attributedStringsForBlocks: [Subtext.RenderedBlock] = []
187189

188190
// Extract and append AttributedString for each block
189-
for block in blocks {
191+
for block in subtext.blocks {
190192
guard let blockRange = block.span.range.within(attributedString: markup) else {
191193
continue
192194
}

xcode/Subconscious/SubconsciousTests/Tests_SubtextAttributedStringRenderer.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,36 @@ final class Tests_SubtextAttributedStringRenderer: XCTestCase {
7070
XCTAssertEqual(link.fallback, "Lo and behold")
7171
}
7272

73+
func testAlternativeSignatures() {
74+
let subtext = Subtext(
75+
markup: """
76+
Related: [[Sara Walker]], [[Stuart Kauffman]] [[SFI]], [[Assembly Theory]], [[Emergence comes from alphabets]], [[Alphabet]]
77+
78+
# Mentioned Papers
79+
80+
- Intelligence as a planetary scale process by Adam Frank, David Grinspoon & [[Sara Walker]]
81+
- [[The Algorithmic Origins of Life]] by Sara Imari Walker & Paul C. W. Davies
82+
"""
83+
)
84+
85+
let renderer = SubtextAttributedStringRenderer()
86+
let a = renderer.render(subtext)
87+
let b = renderer.render(subtext.description)
88+
89+
var joined = AttributedString()
90+
for block in a {
91+
joined.append(block.renderedSubstring)
92+
if block != a.last {
93+
joined.append(AttributedString("\n"))
94+
}
95+
}
96+
97+
XCTAssertEqual(
98+
joined.characters.map { String($0) }.joined(separator: ""),
99+
b.characters.map { String($0) }.joined(separator: "")
100+
)
101+
}
102+
73103
func testPerformance() throws {
74104
let renderer = SubtextAttributedStringRenderer()
75105
let attributedString = NSMutableAttributedString(

0 commit comments

Comments
 (0)