Skip to content

Commit

Permalink
Add Canonical. (#185)
Browse files Browse the repository at this point in the history
Part of #25.
  • Loading branch information
jverkoey authored Sep 14, 2024
1 parent 4dc0a98 commit dcc9ba9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ provided below is an organized table of W3C HTML tags and their equivalent Slips
[`<base>`](https://html.spec.whatwg.org/multipage/semantics.html#the-base-element) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
[`<link rel="alternate">`](https://html.spec.whatwg.org/multipage/links.html#rel-alternate) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
[`<link rel="alternate">`](https://html.spec.whatwg.org/multipage/links.html#rel-alternate) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
[`<link rel="canonical">`](https://html.spec.whatwg.org/multipage/links.html#link-type-canonical) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
[`<link rel="canonical">`](https://html.spec.whatwg.org/multipage/links.html#link-type-canonical) | ``Canonical``
[`<link rel="author">`](https://html.spec.whatwg.org/multipage/links.html#link-type-author) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
[`<link rel="bookmark">`](https://html.spec.whatwg.org/multipage/links.html#link-type-bookmark) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
[`<link rel="dns-prefetch">`](https://html.spec.whatwg.org/multipage/links.html#link-type-dns-prefetch) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
Expand Down
1 change: 1 addition & 0 deletions Sources/Slipstream/Documentation.docc/W3C/W3CViews.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The complete W3C HTML elements standard can be found [here](https://html.spec.wh

- ``Head``
- ``Title``
- ``Canonical``
- ``Icon``
- ``Preload``
- ``Preconnect``
Expand Down
39 changes: 39 additions & 0 deletions Sources/Slipstream/W3C/Elements/DocumentMetadata/Canonical.swift
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?
}
14 changes: 14 additions & 0 deletions Tests/SlipstreamTests/W3C/CanonicalTests.swift
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" />"#)
}
}

0 comments on commit dcc9ba9

Please sign in to comment.