Skip to content

Commit a8e145d

Browse files
committed
Always set role even if automatic role headings are disabled
Fixes a bug where disabling automatic role headings with directive `@AutomaticTitleHeading` would also result in the node's role being lost. We always want to set the node's role as part of the node's metadata, regardless of any title settings. Some unit tests were added to ensure that the interaction between `AutomaticTitleHeading`, `.metadata.roleHeading` and `.metadata.role` is all working as expected. Additional notes: ----------------- We still want the role heading to be set if it has been overridden with `@TitleHeading`, regardless of the value of `@AutomaticTitleHeading`. Only automatic title headings should be disabled, not manually curated ones. The same applies for `@PageKind`, which manually overrides the role and role heading. Fixes rdar://128609308.
1 parent 79bb1d0 commit a8e145d

File tree

2 files changed

+100
-4
lines changed

2 files changed

+100
-4
lines changed

Sources/SwiftDocC/Model/Rendering/RenderNodeTranslator.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -729,11 +729,10 @@ public struct RenderNodeTranslator: SemanticVisitor {
729729

730730
node.topicSectionsStyle = topicsSectionStyle(for: documentationNode)
731731

732+
let role = DocumentationContentRenderer.roleForArticle(article, nodeKind: documentationNode.kind)
733+
node.metadata.role = role.rawValue
734+
732735
if shouldCreateAutomaticRoleHeading(for: documentationNode) {
733-
734-
let role = DocumentationContentRenderer.roleForArticle(article, nodeKind: documentationNode.kind)
735-
node.metadata.role = role.rawValue
736-
737736
switch role {
738737
case .article:
739738
// If there are no links to other nodes from the article,

Tests/SwiftDocCTests/Rendering/RenderNodeTranslatorTests.swift

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,103 @@ class RenderNodeTranslatorTests: XCTestCase {
13621362
renderNode = try renderNodeArticleFromReferencePath(referencePath: "/documentation/unit-test/SampleCode")
13631363
XCTAssertEqual(renderNode.metadata.roleHeading, "Sample Code")
13641364
}
1365+
1366+
func testExpectedRoleHeadingWhenAutomaticRoleHeadingIsDisabled() throws {
1367+
let exampleDocumentation = Folder(
1368+
name: "unit-test.docc",
1369+
content: [
1370+
TextFile(name: "APICollection.md", utf8Content: """
1371+
# API Collection
1372+
@Options {
1373+
@AutomaticTitleHeading(disabled)
1374+
}
1375+
My API Collection Abstract.
1376+
## Topics
1377+
- ``Symbol``
1378+
- <doc:article2>
1379+
- <doc:article3>
1380+
"""),
1381+
TextFile(name: "Article.md", utf8Content: """
1382+
# Article
1383+
@Options {
1384+
@AutomaticTitleHeading(disabled)
1385+
}
1386+
My Article Abstract.
1387+
## Overview
1388+
An overview.
1389+
"""),
1390+
TextFile(name: "CustomRole.md", utf8Content: """
1391+
# Article 4
1392+
@Options {
1393+
@AutomaticTitleHeading(disabled)
1394+
}
1395+
@Metadata {
1396+
@TitleHeading("Custom Role")
1397+
}
1398+
My Article Abstract.
1399+
## Overview
1400+
An overview.
1401+
"""),
1402+
TextFile(name: "SampleCode.md", utf8Content: """
1403+
# Sample Code
1404+
@Options {
1405+
@AutomaticTitleHeading(disabled)
1406+
}
1407+
@Metadata {
1408+
@PageKind(sampleCode)
1409+
}
1410+
## Topics
1411+
- <doc:article>
1412+
"""),
1413+
JSONFile(
1414+
name: "unit-test.symbols.json",
1415+
content: makeSymbolGraph(
1416+
moduleName: "unit-test",
1417+
symbols: [SymbolGraph.Symbol(
1418+
identifier: .init(precise: "symbol-id", interfaceLanguage: "swift"),
1419+
names: .init(title: "Symbol", navigator: nil, subHeading: nil, prose: nil),
1420+
pathComponents: ["Symbol"],
1421+
docComment: nil,
1422+
accessLevel: .public,
1423+
kind: .init(parsedIdentifier: .class, displayName: "Kind Display Name"),
1424+
mixins: [:]
1425+
)]
1426+
)
1427+
),
1428+
]
1429+
)
1430+
let tempURL = try createTempFolder(content: [exampleDocumentation])
1431+
let (_, bundle, context) = try loadBundle(from: tempURL)
1432+
1433+
func renderNodeArticleFromReferencePath(
1434+
referencePath: String
1435+
) throws -> RenderNode {
1436+
let reference = ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: referencePath, sourceLanguage: .swift)
1437+
let symbol = try XCTUnwrap(context.entity(with: reference).semantic as? Article)
1438+
var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: reference)
1439+
return try XCTUnwrap(translator.visitArticle(symbol) as? RenderNode)
1440+
}
1441+
1442+
// Assert that API collections disabling automatic title headings don't get any value assigned as the eyebrow title,
1443+
// but that the node's role itself is unaffected.
1444+
var renderNode = try renderNodeArticleFromReferencePath(referencePath: "/documentation/unit-test/APICollection")
1445+
XCTAssertEqual(renderNode.metadata.roleHeading, nil)
1446+
XCTAssertEqual(renderNode.metadata.role, RenderMetadata.Role.collectionGroup.rawValue)
1447+
// Assert that articles disabling automatic title headings don't get any value assigned as the eyebrow title,
1448+
// but that the node's role itself is unaffected.
1449+
renderNode = try renderNodeArticleFromReferencePath(referencePath: "/documentation/unit-test/Article")
1450+
XCTAssertEqual(renderNode.metadata.roleHeading, nil)
1451+
XCTAssertEqual(renderNode.metadata.role, RenderMetadata.Role.article.rawValue)
1452+
// Assert that articles that have a custom title heading have the eyebrow title assigned properly,
1453+
// even when automatic title headings are disabled.
1454+
renderNode = try renderNodeArticleFromReferencePath(referencePath: "/documentation/unit-test/CustomRole")
1455+
XCTAssertEqual(renderNode.metadata.roleHeading, "Custom Role")
1456+
XCTAssertEqual(renderNode.metadata.role, RenderMetadata.Role.article.rawValue)
1457+
// Assert that articles that have a custom page kind have the eyebrow title assigned properly,
1458+
// even when automatic title headings are disabled.
1459+
renderNode = try renderNodeArticleFromReferencePath(referencePath: "/documentation/unit-test/SampleCode")
1460+
XCTAssertEqual(renderNode.metadata.roleHeading, "Sample Code")
1461+
}
13651462

13661463
func testEncodesOverloadsInRenderNode() throws {
13671464
enableFeatureFlag(\.isExperimentalOverloadedSymbolPresentationEnabled)

0 commit comments

Comments
 (0)