Skip to content

Commit

Permalink
Merge pull request #48 from pd95/feature/Allow-Enhancing-Standard-Head
Browse files Browse the repository at this point in the history
Allow enhancing standard header elements
  • Loading branch information
twostraws authored May 13, 2024
2 parents 3ce9855 + 822762c commit c6c1bb1
Showing 1 changed file with 41 additions and 30 deletions.
71 changes: 41 additions & 30 deletions Sources/Ignite/Elements/Head.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,47 +30,58 @@ public struct Head: HTMLRootElement {
/// - page: The `Page` you want to create headers for.
/// - context: The active `PublishingContext`, which includes
/// information about the site being rendered and more.
public init(for page: Page, in context: PublishingContext) {
self.init {
MetaTag.utf8
MetaTag.flexibleViewport
/// - additionalItems: Additional items to enhance the set of standard headers.
public init(for page: Page, in context: PublishingContext, @HeadElementBuilder additionalItems: () -> [HeadElement] = {[]}) {
items = Head.standardHeaders(for: page, in: context)

if page.description.isEmpty == false {
MetaTag(name: "description", content: page.description)
}
items += MetaTag.socialSharingTags(for: page, context: context)
items += additionalItems()
}

if context.site.author.isEmpty == false {
MetaTag(name: "author", content: context.site.author)
}
/// Renders this element using publishing context passed in.
/// - Parameter context: The current publishing context.
/// - Returns: The HTML for this element.
public func render(context: PublishingContext) -> String {
"<head>\(items.render(context: context))</head>"
}

MetaTag.generator
/// A static function, returning the standard set of headers used for a `Page` instance.
///
/// This function can be used when defining a custom header based on the standard set of headers.
/// - Parameters:
/// - page: The `Page` you want to create headers for.
/// - context: The active `PublishingContext`, which includes
@HeadElementBuilder
public static func standardHeaders(for page: Page, in context: PublishingContext) -> [HeadElement] {
MetaTag.utf8
MetaTag.flexibleViewport

Title(page.title)
if page.description.isEmpty == false {
MetaTag(name: "description", content: page.description)
}

MetaLink.standardCSS
if context.site.author.isEmpty == false {
MetaTag(name: "author", content: context.site.author)
}

if context.site.syntaxHighlighters.isEmpty == false {
MetaLink.syntaxHighlightingCSS
}
MetaTag.generator

if context.site.builtInIconsEnabled {
MetaLink.iconCSS
}
Title(page.title)

MetaLink(href: page.url, rel: "canonical")
MetaLink.standardCSS

if let favicon = context.site.favicon {
MetaLink(href: favicon, rel: .icon)
}
if context.site.syntaxHighlighters.isEmpty == false {
MetaLink.syntaxHighlightingCSS
}

items += MetaTag.socialSharingTags(for: page, context: context)
}
if context.site.builtInIconsEnabled {
MetaLink.iconCSS
}

/// Renders this element using publishing context passed in.
/// - Parameter context: The current publishing context.
/// - Returns: The HTML for this element.
public func render(context: PublishingContext) -> String {
"<head>\(items.render(context: context))</head>"
MetaLink(href: page.url, rel: "canonical")

if let favicon = context.site.favicon {
MetaLink(href: favicon, rel: .icon)
}
}
}

0 comments on commit c6c1bb1

Please sign in to comment.