Skip to content

Commit e6a6d7c

Browse files
committed
Update Swift node type names in accordance with the cmark changes.
Update some tests that broke in the new version.
1 parent 9407b68 commit e6a6d7c

8 files changed

+109
-108
lines changed

Sources/CommonMark/CMarkInterop.swift

+14-14
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,10 @@ fileprivate func makeNode(from cNode: OpaquePointer) -> MarkdownNode {
4444
children: makeNodes(fromChildrenOf: cNode) as! [InlineContent],
4545
sourceRange: sourceRange)
4646
case CMARK_NODE_HEADING:
47-
node = HeaderNode(
48-
level: HeaderNode.Level(rawValue: numericCast(cmark_node_get_heading_level(cNode)))!,
47+
node = HeadingNode(
48+
level: HeadingNode.Level(rawValue: numericCast(cmark_node_get_heading_level(cNode)))!,
4949
children: makeNodes(fromChildrenOf: cNode) as! [InlineContent],
5050
sourceRange: sourceRange)
51-
case CMARK_NODE_THEMATIC_BREAK:
52-
node = HorizontalRuleNode(sourceRange: sourceRange)
5351
case CMARK_NODE_HTML_BLOCK:
5452
node = HTMLBlockNode(
5553
literalContent: String(cString: cmark_node_get_literal(cNode)),
@@ -106,6 +104,8 @@ fileprivate func makeNode(from cNode: OpaquePointer) -> MarkdownNode {
106104
node = TextNode(
107105
literalContent: String(cString: cmark_node_get_literal(cNode)),
108106
sourceRange: sourceRange)
107+
case CMARK_NODE_THEMATIC_BREAK:
108+
node = ThematicBreakNode(sourceRange: sourceRange)
109109
default:
110110
fatalError("Unexpected node type \(type) encountered")
111111
}
@@ -180,8 +180,7 @@ extension PrimitiveNode: CMarkNodeConvertible {
180180
case .codeBlock(let node): return node.makeCNode()
181181
case .document(let node): return node.makeCNode()
182182
case .emphasis(let node): return node.makeCNode()
183-
case .header(let node): return node.makeCNode()
184-
case .horizontalRule(let node): return node.makeCNode()
183+
case .heading(let node): return node.makeCNode()
185184
case .htmlBlock(let node): return node.makeCNode()
186185
case .image(let node): return node.makeCNode()
187186
case .inlineCode(let node): return node.makeCNode()
@@ -194,6 +193,7 @@ extension PrimitiveNode: CMarkNodeConvertible {
194193
case .softBreak(let node): return node.makeCNode()
195194
case .strong(let node): return node.makeCNode()
196195
case .text(let node): return node.makeCNode()
196+
case .thematicBreak(let node): return node.makeCNode()
197197
}
198198
}
199199
}
@@ -239,7 +239,7 @@ extension HTMLBlockNode: CMarkNodeConvertible {
239239
}
240240
}
241241

242-
extension HeaderNode: CMarkNodeConvertible {
242+
extension HeadingNode: CMarkNodeConvertible {
243243

244244
func makeCNode() -> OpaquePointer {
245245
let cNode = cmark_node_new(CMARK_NODE_HEADING)!
@@ -251,13 +251,6 @@ extension HeaderNode: CMarkNodeConvertible {
251251
}
252252
}
253253

254-
extension HorizontalRuleNode: CMarkNodeConvertible {
255-
256-
func makeCNode() -> OpaquePointer {
257-
return cmark_node_new(CMARK_NODE_THEMATIC_BREAK)!
258-
}
259-
}
260-
261254
extension ImageNode: CMarkNodeConvertible {
262255

263256
func makeCNode() -> OpaquePointer {
@@ -388,3 +381,10 @@ extension TextNode: CMarkNodeConvertible {
388381
return cNode
389382
}
390383
}
384+
385+
extension ThematicBreakNode: CMarkNodeConvertible {
386+
387+
func makeCNode() -> OpaquePointer {
388+
return cmark_node_new(CMARK_NODE_THEMATIC_BREAK)!
389+
}
390+
}

Sources/CommonMark/HeaderNode.swift renamed to Sources/CommonMark/HeadingNode.swift

+14-14
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
/// A block element that represents a section header.
14-
public struct HeaderNode: BlockContent {
13+
/// A block element that represents a section heading.
14+
public struct HeadingNode: BlockContent {
1515

16-
/// The level of a header, which describes its position in the hierarchy of a document and the
17-
/// size at which the header is rendered.
16+
/// The level of a heading, which describes its position in the hierarchy of a document and the
17+
/// size at which the heading is rendered.
1818
public enum Level: Int {
1919
case h1 = 1
2020
case h2 = 2
@@ -24,20 +24,20 @@ public struct HeaderNode: BlockContent {
2424
case h6 = 6
2525
}
2626

27-
/// The level of the header.
27+
/// The level of the heading.
2828
public let level: Level
2929

3030
/// The children of the receiver.
3131
public let children: [InlineContent]
3232

3333
public let sourceRange: Range<SourceLocation>?
3434

35-
public var primitiveRepresentation: PrimitiveNode { return .header(self) }
35+
public var primitiveRepresentation: PrimitiveNode { return .heading(self) }
3636

37-
/// Creates a new header node.
37+
/// Creates a new heading node.
3838
///
3939
/// - Parameters:
40-
/// - level: The level of the header. If omitted, `.h1` is used.
40+
/// - level: The level of the heading. If omitted, `.h1` is used.
4141
/// - children: Inline content nodes that are children of the new node.
4242
/// - sourceRange: The source range from which the node was parsed, if known.
4343
public init(
@@ -55,25 +55,25 @@ public struct HeaderNode: BlockContent {
5555
///
5656
/// - Parameter level: The new level.
5757
/// - Returns: The new node.
58-
public func replacingLevel(_ level: Level) -> HeaderNode {
59-
return HeaderNode(level: level, children: children, sourceRange: sourceRange)
58+
public func replacingLevel(_ level: Level) -> HeadingNode {
59+
return HeadingNode(level: level, children: children, sourceRange: sourceRange)
6060
}
6161

6262
/// Returns a new node equivalent to the receiver, but whose children have been replaced with the
6363
/// given list of nodes.
6464
///
6565
/// - Parameter children: The new list of children.
6666
/// - Returns: The new node.
67-
public func replacingChildren(_ children: [InlineContent]) -> HeaderNode {
68-
return HeaderNode(level: level, children: children, sourceRange: sourceRange)
67+
public func replacingChildren(_ children: [InlineContent]) -> HeadingNode {
68+
return HeadingNode(level: level, children: children, sourceRange: sourceRange)
6969
}
7070

7171
/// Returns a new node equivalent to the receiver, but whose source range has been replaced with
7272
/// the given value.
7373
///
7474
/// - Parameter sourceRange: The new source range.
7575
/// - Returns: The new node.
76-
public func replacingSourceRange(_ sourceRange: Range<SourceLocation>?) -> HeaderNode {
77-
return HeaderNode(children: children, sourceRange: sourceRange)
76+
public func replacingSourceRange(_ sourceRange: Range<SourceLocation>?) -> HeadingNode {
77+
return HeadingNode(children: children, sourceRange: sourceRange)
7878
}
7979
}

Sources/CommonMark/MarkdownRewriter.swift

+14-14
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ open class MarkdownRewriter {
3636
case let castNode as CodeBlockNode: return visit(castNode)
3737
case let castNode as EmphasisNode: return visit(castNode)
3838
case let castNode as HTMLBlockNode: return visit(castNode)
39-
case let castNode as HeaderNode: return visit(castNode)
40-
case let castNode as HorizontalRuleNode: return visit(castNode)
39+
case let castNode as HeadingNode: return visit(castNode)
4140
case let castNode as ImageNode: return visit(castNode)
4241
case let castNode as InlineCodeNode: return visit(castNode)
4342
case let castNode as InlineHTMLNode: return visit(castNode)
@@ -50,6 +49,7 @@ open class MarkdownRewriter {
5049
case let castNode as SoftBreakNode: return visit(castNode)
5150
case let castNode as StrongNode: return visit(castNode)
5251
case let castNode as TextNode: return visit(castNode)
52+
case let castNode as ThematicBreakNode: return visit(castNode)
5353
case let castNode as BlockContent: return visit(extension: castNode)
5454
case let castNode as InlineContent: return visit(extension: castNode)
5555
default:
@@ -121,28 +121,18 @@ open class MarkdownRewriter {
121121
return node
122122
}
123123

124-
/// Called when a `HeaderNode` is visited, replacing it in the AST with the returned node.
124+
/// Called when a `HeadingNode` is visited, replacing it in the AST with the returned node.
125125
///
126126
/// The base class implementation of this method automatically visits the children of the node and
127127
/// returns a node whose children have been replaced by the results of that visitation. If you
128128
/// override it, you must call `super.visit(node)` if you wish to visit the children as well.
129129
///
130130
/// - Parameter node: The node being visited.
131131
/// - Returns: The node that should replace the given node in the AST.
132-
open func visit(_ node: HeaderNode) -> BlockContent {
132+
open func visit(_ node: HeadingNode) -> BlockContent {
133133
return node.replacingChildren(visitAll(node.children))
134134
}
135135

136-
/// Called when a `HorizontalRuleNode` is visited, replacing it in the AST with the returned node.
137-
///
138-
/// The base class implementation of this method simply returns the same node.
139-
///
140-
/// - Parameter node: The node being visited.
141-
/// - Returns: The node that should replace the given node in the AST.
142-
open func visit(_ node: HorizontalRuleNode) -> BlockContent {
143-
return node
144-
}
145-
146136
/// Called when a `ImageNode` is visited, replacing it in the AST with the returned node.
147137
///
148138
/// The base class implementation of this method automatically visits the children of the node and
@@ -277,6 +267,16 @@ open class MarkdownRewriter {
277267
return node
278268
}
279269

270+
/// Called when a `ThematicBreakNode` is visited, replacing it in the AST with the returned node.
271+
///
272+
/// The base class implementation of this method simply returns the same node.
273+
///
274+
/// - Parameter node: The node being visited.
275+
/// - Returns: The node that should replace the given node in the AST.
276+
open func visit(_ node: ThematicBreakNode) -> BlockContent {
277+
return node
278+
}
279+
280280
/// Visits a custom block extension node, replacing it in the AST with the returned node.
281281
///
282282
/// If you have defined custom extension nodes and use them in your AST, then this method will be

Sources/CommonMark/MarkdownVisitor.swift

+11-11
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ open class MarkdownVisitor {
3333
case let castNode as CodeBlockNode: visit(castNode)
3434
case let castNode as EmphasisNode: visit(castNode)
3535
case let castNode as HTMLBlockNode: visit(castNode)
36-
case let castNode as HeaderNode: visit(castNode)
37-
case let castNode as HorizontalRuleNode: visit(castNode)
36+
case let castNode as HeadingNode: visit(castNode)
3837
case let castNode as ImageNode: visit(castNode)
3938
case let castNode as InlineCodeNode: visit(castNode)
4039
case let castNode as InlineHTMLNode: visit(castNode)
@@ -47,6 +46,7 @@ open class MarkdownVisitor {
4746
case let castNode as SoftBreakNode: visit(castNode)
4847
case let castNode as StrongNode: visit(castNode)
4948
case let castNode as TextNode: visit(castNode)
49+
case let castNode as ThematicBreakNode: visit(castNode)
5050
default: visit(extension: node)
5151
}
5252
afterVisit(node)
@@ -104,23 +104,16 @@ open class MarkdownVisitor {
104104
/// - Parameter node: The node being visited.
105105
open func visit(_ node: HTMLBlockNode) {}
106106

107-
/// Called when a `HeaderNode` is visited.
107+
/// Called when a `HeadingNode` is visited.
108108
///
109109
/// The base class implementation of this method automatically visits the children of the node. If
110110
/// you override it, you must call `super.visit(node)` if you wish to visit the children as well.
111111
///
112112
/// - Parameter node: The node being visited.
113-
open func visit(_ node: HeaderNode) {
113+
open func visit(_ node: HeadingNode) {
114114
visitAll(node.children)
115115
}
116116

117-
/// Called when a `HorizontalRuleNode` is visited.
118-
///
119-
/// The base class implementation of this method does nothing.
120-
///
121-
/// - Parameter node: The node being visited.
122-
open func visit(_ node: HorizontalRuleNode) {}
123-
124117
/// Called when a `ImageNode` is visited.
125118
///
126119
/// The base class implementation of this method automatically visits the children of the node. If
@@ -226,6 +219,13 @@ open class MarkdownVisitor {
226219
/// - Parameter node: The node being visited.
227220
open func visit(_ node: TextNode) {}
228221

222+
/// Called when a `ThematicBreakNode` is visited.
223+
///
224+
/// The base class implementation of this method does nothing.
225+
///
226+
/// - Parameter node: The node being visited.
227+
open func visit(_ node: ThematicBreakNode) {}
228+
229229
/// Visits a custom extension node.
230230
///
231231
/// If you have defined custom extension nodes and use them in your AST, then this method will be

Sources/CommonMark/PrimitiveNode.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ public enum PrimitiveNode {
3131

3232
case emphasis(EmphasisNode)
3333

34-
case header(HeaderNode)
35-
36-
case horizontalRule(HorizontalRuleNode)
34+
case heading(HeadingNode)
3735

3836
case htmlBlock(HTMLBlockNode)
3937

@@ -58,4 +56,6 @@ public enum PrimitiveNode {
5856
case strong(StrongNode)
5957

6058
case text(TextNode)
59+
60+
case thematicBreak(ThematicBreakNode)
6161
}

Sources/CommonMark/HorizontalRuleNode.swift renamed to Sources/CommonMark/ThematicBreakNode.swift

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
/// A block element that represents a horizontal rule.
14-
public struct HorizontalRuleNode: BlockContent {
13+
/// A block element that represents a thematic break between large ranges of Markdown content (which
14+
/// is typically rendered as a horizontal rule).
15+
public struct ThematicBreakNode: BlockContent {
1516

1617
public let sourceRange: Range<SourceLocation>?
1718

18-
public var primitiveRepresentation: PrimitiveNode { return .horizontalRule(self) }
19+
public var primitiveRepresentation: PrimitiveNode { return .thematicBreak(self) }
1920

20-
/// Creates a new horizontal rule node.
21+
/// Creates a new thematic break node.
2122
///
2223
/// - Parameter sourceRange: The source range from which the node was parsed, if known.
2324
public init(sourceRange: Range<SourceLocation>? = nil) {
@@ -29,7 +30,7 @@ public struct HorizontalRuleNode: BlockContent {
2930
///
3031
/// - Parameter sourceRange: The new source range.
3132
/// - Returns: The new node.
32-
public func replacingSourceRange(_ sourceRange: Range<SourceLocation>?) -> HorizontalRuleNode {
33-
return HorizontalRuleNode(sourceRange: sourceRange)
33+
public func replacingSourceRange(_ sourceRange: Range<SourceLocation>?) -> ThematicBreakNode {
34+
return ThematicBreakNode(sourceRange: sourceRange)
3435
}
3536
}

Tests/CommonMarkTests/MarkdownDocumentTest.swift

+17-17
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,15 @@ final class MarkdownDocumentTest: XCTestCase {
5050
""")
5151
}
5252

53-
func testInitByParsing_header() {
53+
func testInitByParsing_heading() {
5454
let document = MarkdownDocument(byParsing: "# Foo")
5555

56-
let header = document.children[0] as! HeaderNode
57-
XCTAssertEqual(header.level, .h1)
58-
let text = header.children[0] as! TextNode
56+
let heading = document.children[0] as! HeadingNode
57+
XCTAssertEqual(heading.level, .h1)
58+
let text = heading.children[0] as! TextNode
5959
XCTAssertEqual(text.literalContent, "Foo")
6060
}
6161

62-
func testInitByParsing_horizontalRule() {
63-
let document = MarkdownDocument(byParsing: """
64-
foo
65-
66-
---
67-
68-
bar
69-
""")
70-
71-
let rule = document.children[1] as? HorizontalRuleNode
72-
XCTAssertNotNil(rule)
73-
}
74-
7562
func testInitByParsing_image() {
7663
let document = MarkdownDocument(byParsing: "![foo](http://bar \"title\")")
7764

@@ -173,6 +160,19 @@ final class MarkdownDocumentTest: XCTestCase {
173160
let text = paragraph.children[0] as! TextNode
174161
XCTAssertEqual(text.literalContent, "basic")
175162
}
163+
164+
func testInitByParsing_thematicBreak() {
165+
let document = MarkdownDocument(byParsing: """
166+
foo
167+
168+
---
169+
170+
bar
171+
""")
172+
173+
let rule = document.children[1] as? ThematicBreakNode
174+
XCTAssertNotNil(rule)
175+
}
176176
}
177177

178178
#if !os(macOS)

0 commit comments

Comments
 (0)