Skip to content

Commit 2c83cf7

Browse files
committed
[Feature] Add isAutolink property to Link
1 parent 4cbd46e commit 2c83cf7

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

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

Lines changed: 23 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
@@ -23,6 +23,8 @@ public struct Link: InlineMarkup, InlineContainer {
2323
init(_ data: _MarkupData) {
2424
self._data = data
2525
}
26+
27+
var _isAutolink: Bool?
2628
}
2729

2830
// MARK: - Public API
@@ -62,6 +64,26 @@ public extension Link {
6264
}
6365
}
6466
}
67+
68+
// If we make it a pure computed variable, when updating destination
69+
// an orginal autolink Link will return false
70+
var isAutolink: Bool {
71+
get {
72+
if let isAutolink = _isAutolink {
73+
return isAutolink
74+
}
75+
guard let destination,
76+
childCount == 1,
77+
let text = child(at: 0) as? Text,
78+
destination == text.string else {
79+
return false
80+
}
81+
return true
82+
}
83+
set {
84+
_isAutolink = newValue
85+
}
86+
}
6587

6688
// MARK: Visitation
6789

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: 18 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,21 @@ 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+
51+
link.isAutolink = true
52+
XCTAssertTrue(link.isAutolink)
53+
}
3754
}

0 commit comments

Comments
 (0)