Skip to content

Commit c166a85

Browse files
set reference roles with PageKind directive (#474)
rdar://104980714
1 parent 7fe1b5e commit c166a85

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Sources/SwiftDocC/Model/Rendering/DocumentationContentRenderer.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,20 @@ public class DocumentationContentRenderer {
127127

128128
/// Returns a metadata role for an article, depending if it's a collection, technology, or a free form article.
129129
func roleForArticle(_ article: Article, nodeKind: DocumentationNode.Kind) -> RenderMetadata.Role {
130+
// If the article has a `@PageKind` directive, use the kind from there
131+
// before checking anything else.
132+
if let pageKind = article.metadata?.pageKind {
133+
return pageKind.kind.renderRole
134+
}
135+
130136
// We create generated nodes with a semantic Article because they
131137
// can have doc extensions and the only way to tell them apart from
132138
// api collections or other articles is by their node kind.
133139
switch nodeKind {
134140
case .collectionGroup: return role(for: nodeKind)
135141
default: break
136142
}
137-
143+
138144
if article.topics?.taskGroups.isEmpty == false {
139145
// The documentation includes a "Topics" section, it's a collection or a group
140146
let isTechnologyRoot = article.metadata?.technologyRoot != nil

Tests/SwiftDocCTests/Rendering/PageKindTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,27 @@ class PageKindTests: XCTestCase {
7474
XCTAssertEqual(renderNode.metadata.roleHeading, "Article")
7575
}
7676

77+
func testPageKindReference() throws {
78+
let (bundle, context) = try testBundleAndContext(named: "SampleBundle")
79+
let reference = ResolvedTopicReference(
80+
bundleIdentifier: bundle.identifier,
81+
path: "/documentation/SomeSample",
82+
sourceLanguage: .swift
83+
)
84+
let article = try XCTUnwrap(context.entity(with: reference).semantic as? Article)
85+
var translator = RenderNodeTranslator(
86+
context: context,
87+
bundle: bundle,
88+
identifier: reference,
89+
source: nil
90+
)
91+
let renderNode = try XCTUnwrap(translator.visitArticle(article) as? RenderNode)
92+
93+
let sampleReference = try XCTUnwrap(renderNode.references["doc://org.swift.docc.sample/documentation/SampleBundle/MyLocalSample"] as? TopicRenderReference)
94+
95+
XCTAssertEqual(sampleReference.role, RenderMetadata.Role.sampleCode.rawValue)
96+
}
97+
7798
func testValidMetadataWithOnlyPageKind() throws {
7899
let source = """
79100
@Metadata {

0 commit comments

Comments
 (0)