Skip to content

Commit 79bb1d0

Browse files
authored
Limit the number of items in automatic See Also sections (#1043)
* Limit the number of items in automatic See Also sections rdar://134376209 * Make See Also limit externally configurable
1 parent 37df0ee commit 79bb1d0

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

Sources/SwiftDocC/Infrastructure/Topic Graph/AutomaticCuration.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import Foundation
1212
import Markdown
1313
import SymbolKit
1414

15+
16+
private let automaticSeeAlsoLimit: Int = {
17+
ProcessInfo.processInfo.environment["DOCC_AUTOMATIC_SEE_ALSO_LIMIT"].flatMap { Int($0) } ?? 15
18+
}()
19+
1520
/// A set of functions that add automatic symbol curation to a topic graph.
1621
public struct AutomaticCuration {
1722
/// A value type to store an automatically curated task group and its sorting index.
@@ -167,7 +172,8 @@ public struct AutomaticCuration {
167172
}
168173

169174
func filterReferences(_ references: [ResolvedTopicReference]) -> [ResolvedTopicReference] {
170-
references
175+
Array(
176+
references
171177
// Don't include the current node.
172178
.filter { $0 != node.reference }
173179

@@ -177,6 +183,9 @@ public struct AutomaticCuration {
177183
variantsTraits.contains(where: { $0.interfaceLanguage == language.id})
178184
})
179185
}
186+
// Don't create too long See Also sections
187+
.prefix(automaticSeeAlsoLimit)
188+
)
180189
}
181190

182191
// Look up the render context first

Tests/SwiftDocCTests/Model/SemaToRenderNodeMultiLanguageTests.swift

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import Foundation
1212
@testable import SwiftDocC
1313
import SymbolKit
14+
import SwiftDocCTestUtilities
1415
import XCTest
1516

1617
class SemaToRenderNodeMixedLanguageTests: XCTestCase {
@@ -969,7 +970,48 @@ class SemaToRenderNodeMixedLanguageTests: XCTestCase {
969970
XCTAssertTrue(objCTopicIDs.contains("doc://org.swift.MixedLanguageFramework/documentation/MixedLanguageFramework/_MixedLanguageFrameworkVersionNumber"))
970971
XCTAssertFalse(objCTopicIDs.contains("doc://org.swift.MixedLanguageFramework/documentation/MixedLanguageFramework/SwiftOnlyStruct"))
971972
}
972-
973+
974+
func testAutomaticSeeAlsoSectionElementLimit() throws {
975+
let fileSystem = try TestFileSystem(folders: [
976+
Folder(name: "unit-test.docc", content: [
977+
JSONFile(name: "ModuleName.symbols.json", content: makeSymbolGraph(moduleName: "ModuleName", symbols: (1...50).map {
978+
makeSymbol(id: "symbol-id-\($0)", kind: .class, pathComponents: ["SymbolName\($0)"])
979+
})),
980+
981+
TextFile(name: "ModuleName.md", utf8Content: """
982+
# ``ModuleName``
983+
984+
A topic section with many elements
985+
986+
## Topics
987+
988+
### Many symbols
989+
990+
\((1...50).map { "- ``SymbolName\($0)``" }.joined(separator: "\n"))
991+
"""),
992+
])
993+
])
994+
995+
let workspace = DocumentationWorkspace()
996+
let context = try DocumentationContext(dataProvider: workspace)
997+
try workspace.registerProvider(fileSystem)
998+
999+
XCTAssert(context.problems.isEmpty, "Unexpected problems: \(context.problems.map(\.diagnostic.summary))")
1000+
let bundle = try XCTUnwrap(context.registeredBundles.first)
1001+
1002+
let converter = DocumentationNodeConverter(bundle: bundle, context: context)
1003+
1004+
let moduleReference = try XCTUnwrap(context.soleRootModuleReference)
1005+
let moduleNode = try converter.convert(context.entity(with: moduleReference))
1006+
XCTAssertEqual(moduleNode.topicSections.first?.identifiers.count, 50, "The module curates 50 symbols")
1007+
1008+
for number in 1...50 {
1009+
let symbolReference = moduleReference.appendingPath("SymbolName\(number)")
1010+
let symbolNode = try converter.convert(context.entity(with: symbolReference))
1011+
XCTAssertEqual(symbolNode.seeAlsoSections.first?.identifiers.count, 15, "The limit is applies to the large See Also section")
1012+
}
1013+
}
1014+
9731015
func renderNodeApplyingObjectiveCVariantOverrides(to renderNode: RenderNode) throws -> RenderNode {
9741016
return try renderNodeApplying(variant: "occ", to: renderNode)
9751017
}

0 commit comments

Comments
 (0)