Skip to content

Add isAutolink property to Link #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Sources/Markdown/Inline Nodes/Inline Containers/Link.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<V: MarkupVisitor>(_ visitor: inout V) -> V.Result {
Expand Down
7 changes: 2 additions & 5 deletions Sources/Markdown/Walker/Walkers/MarkupFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
16 changes: 15 additions & 1 deletion Tests/MarkdownTests/Inline Nodes/LinkTests.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
}