Skip to content

Commit 656cf39

Browse files
authored
[Feature] Add isAutolink property to Link (swiftlang#110)
rdar://106656851
1 parent 4cbd46e commit 656cf39

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

Sources/Markdown/Inline Nodes/Inline Containers/Link.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -63,6 +63,16 @@ public extension Link {
6363
}
6464
}
6565

66+
var isAutolink: Bool {
67+
guard let destination,
68+
childCount == 1,
69+
let text = child(at: 0) as? Text,
70+
destination == text.string else {
71+
return false
72+
}
73+
return true
74+
}
75+
6676
// MARK: Visitation
6777

6878
func accept<V: MarkupVisitor>(_ visitor: inout V) -> V.Result {

Sources/Markdown/Walker/Walkers/MarkupFormatter.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -819,11 +819,8 @@ public struct MarkupFormatter: MarkupWalker {
819819
public mutating func visitLink(_ link: Link) {
820820
let savedState = state
821821
if formattingOptions.condenseAutolinks,
822-
let destination = link.destination,
823-
link.childCount == 1,
824-
let text = link.child(at: 0) as? Text,
825-
// Print autolink-style
826-
destination == text.string {
822+
link.isAutolink,
823+
let destination = link.destination {
827824
print("<\(destination)>", for: link)
828825
} else {
829826
func printRegularLink() {

Tests/MarkdownTests/Inline Nodes/LinkTests.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -34,4 +34,18 @@ class LinkTests: XCTestCase {
3434
"""
3535
XCTAssertEqual(expectedDump, link.debugDescription())
3636
}
37+
38+
func testAutoLink() {
39+
let children = [Text("example.com")]
40+
var link = Link(destination: "example.com", children)
41+
let expectedDump = """
42+
Link destination: "example.com"
43+
└─ Text "example.com"
44+
"""
45+
XCTAssertEqual(expectedDump, link.debugDescription())
46+
XCTAssertTrue(link.isAutolink)
47+
48+
link.destination = "test.example.com"
49+
XCTAssertFalse(link.isAutolink)
50+
}
3751
}

0 commit comments

Comments
 (0)