From df6944ac5b2329e78ae467755203cc62f22da876 Mon Sep 17 00:00:00 2001 From: Kyle Date: Mon, 13 Mar 2023 20:08:47 +0800 Subject: [PATCH] [Feature] Add isAutolink property to Link --- .../Inline Nodes/Inline Containers/Link.swift | 12 +++++++++++- .../Walker/Walkers/MarkupFormatter.swift | 7 ++----- Tests/MarkdownTests/Inline Nodes/LinkTests.swift | 16 +++++++++++++++- 3 files changed, 28 insertions(+), 7 deletions(-) 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) + } }