Added the organization identifier helper to the StructuredData type in the Infrastructure package target.
This commit is contained in:
@@ -23,6 +23,20 @@ public struct StructuredData: Equatable, Sendable {
|
|||||||
self.nodes = nodes
|
self.nodes = nodes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: Methods
|
||||||
|
|
||||||
|
/// The `@id` of the `Organization` node ``init(name:url:alternateName:description:areaServed:email:logo:profiles:)`` builds.
|
||||||
|
///
|
||||||
|
/// A page on another path that asserts a relationship to the organization — a `Person`'s `worksFor`, say — references this rather than
|
||||||
|
/// spelling the fragment a second time, so the two can never drift apart.
|
||||||
|
/// - Parameter url: the absolute URL the site is served at, as passed to that initializer.
|
||||||
|
/// - Returns: the organization node's `@id`.
|
||||||
|
public static func organizationID(
|
||||||
|
forSiteURL url: String
|
||||||
|
) -> String {
|
||||||
|
url + "#organization"
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: Computed
|
// MARK: Computed
|
||||||
|
|
||||||
/// The minified JSON-LD payload: the schema.org `@context`, and the ``nodes`` in a `@graph`.
|
/// The minified JSON-LD payload: the schema.org `@context`, and the ``nodes`` in a `@graph`.
|
||||||
@@ -62,7 +76,7 @@ public extension StructuredData {
|
|||||||
logo: String? = nil,
|
logo: String? = nil,
|
||||||
profiles: [String] = []
|
profiles: [String] = []
|
||||||
) {
|
) {
|
||||||
let id = url + "#organization"
|
let id = Self.organizationID(forSiteURL: url)
|
||||||
|
|
||||||
var organization: [Property] = [
|
var organization: [Property] = [
|
||||||
.init(.name, value: .string(name)),
|
.init(.name, value: .string(name)),
|
||||||
|
|||||||
@@ -46,6 +46,19 @@ struct StructuredDataTests {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func `derives the organization's identifier from the site URL`() {
|
||||||
|
let url = "https://site.example/"
|
||||||
|
let data = StructuredData(
|
||||||
|
name: "A Site",
|
||||||
|
url: url
|
||||||
|
)
|
||||||
|
|
||||||
|
// The helper is what another page references the organization by, so it has to name the node this actually builds.
|
||||||
|
#expect(StructuredData.organizationID(forSiteURL: url) == "https://site.example/#organization")
|
||||||
|
#expect(data.payload.contains(##""@id":"\##(StructuredData.organizationID(forSiteURL: url))""##))
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
func `omits the properties of the facts minimal data does not carry`() {
|
func `omits the properties of the facts minimal data does not carry`() {
|
||||||
let data = StructuredData(
|
let data = StructuredData(
|
||||||
|
|||||||
Reference in New Issue
Block a user