Skip to content

Commit 6e35675

Browse files
authored
Relaxing requirement of anchorSection for headings (#403)
1 parent 6ab3ec1 commit 6e35675

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

Sources/SwiftDocC/Model/DocumentationNode.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ public struct DocumentationNode {
112112

113113
for discussion in discussionSections {
114114
for child in discussion.content {
115-
// For any H2/H3 sections found in the topic's discussion
115+
// For any non-H1 Heading sections found in the topic's discussion
116116
// create an `AnchorSection` and add it to `anchorSections`
117117
// so we can index all anchors found in the bundle for link resolution.
118-
if let heading = child as? Heading, heading.level > 1, heading.level < 4 {
118+
if let heading = child as? Heading, heading.level > 1 {
119119
anchorSections.append(
120120
AnchorSection(reference: reference.withFragment(heading.plainText), title: heading.plainText)
121121
)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2022 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import Foundation
12+
import Markdown
13+
@testable import SwiftDocC
14+
import XCTest
15+
16+
class DocumentationNodeTests: XCTestCase {
17+
func testH4AndUpAnchorSections() throws {
18+
let articleSource = """
19+
# Title
20+
21+
## Heading2
22+
23+
### Heading3
24+
25+
#### Heading4
26+
27+
##### Heading5
28+
29+
###### Heading6
30+
"""
31+
32+
let article = Article(markup: Document(parsing: articleSource, options: []), metadata: nil, redirects: nil, options: [:])
33+
let node = try DocumentationNode(
34+
reference: ResolvedTopicReference(bundleIdentifier: "org.swift.docc", path: "/blah", sourceLanguage: .swift),
35+
article: article
36+
)
37+
XCTAssertEqual(node.anchorSections.count, 5)
38+
for (index, anchorSection) in node.anchorSections.enumerated() {
39+
let expectedTitle = "Heading\(index + 2)"
40+
XCTAssertEqual(anchorSection.title, expectedTitle)
41+
XCTAssertEqual(anchorSection.reference, node.reference.withFragment(expectedTitle))
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)