-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of #25.
- Loading branch information
Showing
4 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
Sources/Slipstream/W3C/Elements/DocumentMetadata/Canonical.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Foundation | ||
|
||
import SwiftSoup | ||
|
||
/// A view that indicates that a URL is the preferred URL for the current document. | ||
/// | ||
/// ```swift | ||
/// struct MySiteMetadata: View { | ||
/// var body: some View { | ||
/// Head { | ||
/// Canonical(URL(string: "https://mysite.com")) | ||
/// } | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// - SeeAlso: W3C [rel="canonical"](https://html.spec.whatwg.org/multipage/links.html#link-type-canonical) specification. | ||
@available(iOS 17.0, macOS 14.0, *) | ||
public struct Canonical: View { | ||
/// Creates a Canonical view. | ||
/// | ||
/// - Parameters: | ||
/// - url: The preferred URL for this document. | ||
public init(_ url: URL?) { | ||
self.url = url | ||
} | ||
|
||
@_documentation(visibility: private) | ||
public func render(_ container: Element, environment: EnvironmentValues) throws { | ||
guard let url else { | ||
return | ||
} | ||
let element = try container.appendElement("link") | ||
try element.attr("rel", "canonical") | ||
try element.attr("href", url.absoluteString) | ||
} | ||
|
||
private let url: URL? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Foundation | ||
import Testing | ||
|
||
import Slipstream | ||
|
||
struct CanonicalTests { | ||
@Test func nilURL() throws { | ||
try #expect(renderHTML(Canonical(nil)) == "") | ||
} | ||
|
||
@Test func validURL() throws { | ||
try #expect(renderHTML(Canonical(URL(string: "https://mysite.com"))) == #"<link rel="canonical" href="https://mysite.com" />"#) | ||
} | ||
} |