diff --git a/Sources/Markdown/Inline Nodes/Inline Containers/Link.swift b/Sources/Markdown/Inline Nodes/Inline Containers/Link.swift index 54ae3d8f..78a27f83 100644 --- a/Sources/Markdown/Inline Nodes/Inline Containers/Link.swift +++ b/Sources/Markdown/Inline Nodes/Inline Containers/Link.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift.org open source project - Copyright (c) 2021 Apple Inc. and the Swift project authors + Copyright (c) 2021-2023 Apple Inc. and the Swift project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -63,6 +63,16 @@ public extension Link { } } + var isAutolink: Bool { + guard let destination, + childCount == 1, + let text = child(at: 0) as? Text, + destination == text.string else { + return false + } + return true + } + // MARK: Visitation func accept(_ visitor: inout V) -> V.Result { diff --git a/Sources/Markdown/Walker/Walkers/MarkupFormatter.swift b/Sources/Markdown/Walker/Walkers/MarkupFormatter.swift index 37103454..bae1bdbf 100644 --- a/Sources/Markdown/Walker/Walkers/MarkupFormatter.swift +++ b/Sources/Markdown/Walker/Walkers/MarkupFormatter.swift @@ -819,11 +819,8 @@ public struct MarkupFormatter: MarkupWalker { public mutating func visitLink(_ link: Link) { let savedState = state if formattingOptions.condenseAutolinks, - let destination = link.destination, - link.childCount == 1, - let text = link.child(at: 0) as? Text, - // Print autolink-style - destination == text.string { + link.isAutolink, + let destination = link.destination { print("<\(destination)>", for: link) } else { func printRegularLink() { diff --git a/Tests/MarkdownTests/Inline Nodes/LinkTests.swift b/Tests/MarkdownTests/Inline Nodes/LinkTests.swift index 895b66af..aeeca9be 100644 --- a/Tests/MarkdownTests/Inline Nodes/LinkTests.swift +++ b/Tests/MarkdownTests/Inline Nodes/LinkTests.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift.org open source project - Copyright (c) 2021 Apple Inc. and the Swift project authors + Copyright (c) 2021-2023 Apple Inc. and the Swift project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -34,4 +34,18 @@ class LinkTests: XCTestCase { """ XCTAssertEqual(expectedDump, link.debugDescription()) } + + func testAutoLink() { + let children = [Text("example.com")] + var link = Link(destination: "example.com", children) + let expectedDump = """ + Link destination: "example.com" + └─ Text "example.com" + """ + XCTAssertEqual(expectedDump, link.debugDescription()) + XCTAssertTrue(link.isAutolink) + + link.destination = "test.example.com" + XCTAssertFalse(link.isAutolink) + } }