Skip to content

Commit 826f4d6

Browse files
committed
Navigator index multi-language render JSON support
Adds support for the navigator index to process render JSON that contains variant overrides by applying patches for the languages in which the page is available. rdar://83398083
1 parent 8da9448 commit 826f4d6

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

Sources/SwiftDocC/Indexing/Navigator/NavigatorIndex.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,22 @@ extension NavigatorIndex {
707707
multiCuratedUnvisited.remove(normalizedIdentifier)
708708
}
709709

710+
// Check if the render node has a variant for Objective-C
711+
let objCVariantTrait = renderNode.variantOverrides?.values.flatMap({ $0.traits }).first { trait in
712+
switch trait {
713+
case .interfaceLanguage(let language):
714+
return InterfaceLanguage.from(string: language) == .objc
715+
}
716+
}
717+
718+
// In case we have a variant for Objective-C, apply the variant and re-index the render node.
719+
if let variantToApply = objCVariantTrait {
720+
let encodedRenderNode = try renderNode.encodeToJSON()
721+
let transformedData = try RenderNodeVariantOverridesApplier().applyVariantOverrides(in: encodedRenderNode, for: [variantToApply])
722+
let variantRenderNode = try RenderNode.decode(fromJSON: transformedData)
723+
try index(renderNode: variantRenderNode)
724+
}
725+
710726
// Bump the nodes counter.
711727
counter += 1
712728
}

Tests/SwiftDocCTests/Indexing/NavigatorIndexTests.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,38 @@ Root
406406
assertEqualDumps(results.first ?? "", try testTree(named: "testNavigatorIndexGeneration"))
407407
}
408408

409+
func testNavigatorIndexGenerationVariantsPayload() throws {
410+
let jsonFile = Bundle.module.url(forResource: "Variant-render-node", withExtension: "json", subdirectory: "Test Resources")!
411+
let jsonData = try Data(contentsOf: jsonFile)
412+
413+
let targetURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString)
414+
let builder = NavigatorIndex.Builder(outputURL: targetURL, bundleIdentifier: testBundleIndetifier, sortRootChildrenByName: true, groupByLanguage: true)
415+
builder.setup()
416+
417+
let renderNode = try XCTUnwrap(RenderJSONDecoder.makeDecoder().decode(RenderNode.self, from: jsonData))
418+
try builder.index(renderNode: renderNode)
419+
420+
builder.finalize()
421+
422+
let navigatorIndex = builder.navigatorIndex!
423+
424+
assertUniqueIDs(node: navigatorIndex.navigatorTree.root)
425+
assertEqualDumps(navigatorIndex.navigatorTree.root.dumpTree(), """
426+
[Root]
427+
┣╸Objective-C
428+
┃ ┗╸My Article in Objective-C
429+
┃ ┣╸Task Group 1
430+
┃ ┣╸Task Group 2
431+
┃ ┗╸Task Group 3
432+
┗╸Swift
433+
┗╸My Article
434+
┣╸Task Group 1
435+
┣╸Task Group 2
436+
┗╸Task Group 3
437+
""")
438+
try FileManager.default.removeItem(at: targetURL)
439+
}
440+
409441
func testNavigatorIndexUsingPageTitleGeneration() throws {
410442
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
411443
let renderContext = RenderContext(documentationContext: context, bundle: bundle)
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
{
2+
"abstract": [],
3+
"documentVersion": 0,
4+
"identifier": {
5+
"interfaceLanguage": "swift",
6+
"url": "doc://org.swift.docc/documentation/mykit/my-article"
7+
},
8+
"kind": "article",
9+
"metadata": {
10+
"modules": [
11+
{
12+
"name": "mykit"
13+
}
14+
],
15+
"role": "article",
16+
"roleHeading": "Article",
17+
"title": "My Article"
18+
},
19+
"primaryContentSections": [],
20+
"references": {
21+
"doc://org.swift.docc/documentation/mykit": {
22+
"identifier": "doc://org.swift.docc/documentation/mykit",
23+
"kind": "symbol",
24+
"role": "collection",
25+
"title": "mykit",
26+
"type": "topic",
27+
"url": "/documentation/mykit"
28+
},
29+
"doc://org.swift.docc/documentation/mykit/my-article": {
30+
"abstract": [],
31+
"identifier": "doc://org.swift.docc/documentation/mykit/my-article",
32+
"kind": "article",
33+
"role": "article",
34+
"title": "My Article",
35+
"type": "topic",
36+
"url": "/documentation/mykit/my-article"
37+
},
38+
"doc://org.swift.docc/documentation/mykit/myclass": {
39+
"abstract": [],
40+
"fragments": [
41+
{
42+
"kind": "text",
43+
"text": "class "
44+
},
45+
{
46+
"kind": "identifier",
47+
"text": "myclass"
48+
}
49+
],
50+
"identifier": "doc://org.swift.docc/documentation/mykit/myclass",
51+
"kind": "symbol",
52+
"role": "symbol",
53+
"title": "myclass",
54+
"type": "topic",
55+
"url": "/documentation/mykit/myclass"
56+
}
57+
},
58+
"schemaVersion": {
59+
"major": 0,
60+
"minor": 1,
61+
"patch": 0
62+
},
63+
"sections": [],
64+
"seeAlsoSections": [],
65+
"topicSections": [
66+
{
67+
"anchor": "2959524",
68+
"identifiers": [
69+
"doc://org.swift.docc/documentation/mykit/myclass"
70+
],
71+
"kind": "taskGroup",
72+
"title": "Task Group 1"
73+
},
74+
{
75+
"anchor": "2958803",
76+
"identifiers": [
77+
"doc://org.swift.docc/documentation/mykit/myclass"
78+
],
79+
"kind": "taskGroup",
80+
"title": "Task Group 2"
81+
},
82+
{
83+
"anchor": "3016821",
84+
"identifiers": [
85+
"doc://org.swift.docc/documentation/mykit/myclass"
86+
],
87+
"kind": "taskGroup",
88+
"title": "Task Group 3"
89+
}
90+
],
91+
"variants": [
92+
{
93+
"paths": [
94+
"documentation/mykit/my-article"
95+
],
96+
"traits": [
97+
{
98+
"interfaceLanguage": "objc"
99+
}
100+
]
101+
},
102+
{
103+
"paths": [
104+
"documentation/mykit/my-article"
105+
],
106+
"traits": [
107+
{
108+
"interfaceLanguage": "swift"
109+
}
110+
]
111+
}
112+
],
113+
"variantOverrides": [
114+
{
115+
"patch": [
116+
{
117+
"op": "replace",
118+
"path": "/metadata/title",
119+
"value": "My Article in Objective-C"
120+
},
121+
{
122+
"op": "replace",
123+
"path": "/identifier/interfaceLanguage",
124+
"value": "objc"
125+
}
126+
],
127+
"traits": [
128+
{
129+
"interfaceLanguage": "objc"
130+
}
131+
]
132+
}
133+
]
134+
}

0 commit comments

Comments
 (0)