Fornax.Seo

Nuget version NuGet workflow status Test workflow status

A SEO meta tag generator for Fornax

Goals

Usage example

NOTE

The following requires fornax 0.15.1 or newer. Visit the wiki to learn how to use this package with earlier fornax versions.

  fornax new
  dotnet tool install paket
  dotnet paket init
  dotnet paket install

IMPORTANT

Collect metadata from a content item (e.g., a blog posting)

// generators/post.fsx
#load @"../.paket/load/net8.0/Fornax.Seo.fsx"
#load @"layout.fsx"

open Html
open Fornax.Seo

let generate' (ctx: SiteContents) (page: string) =
    let siteInfo = ctx.TryGetValue<Globalloader.SiteInfo>()
    let siteName = siteInfo |> Option.map (fun si -> si.title)

    let tagline =
        siteInfo
        |> Option.map (fun si -> si.description)
        |> Option.defaultValue ""

    let siteAuthor =
        ctx.TryGetValue<ContentCreator>()
        |> Option.defaultValue ContentCreator.Default

    let siteRoot =
        siteInfo
        |> Option.map (fun si -> si.baseUrl)
        |> Option.defaultValue ContentObject.Default.BaseUrl

    let post =
        ctx.TryGetValues<Postloader.Post>()
        |> Option.defaultValue Seq.empty
        |> Seq.find (fun p -> p.file = page)

    let postMeta: ContentObject =
        { Title = post.title
          BaseUrl = siteRoot
          Url = post.file.Replace(System.IO.Path.GetExtension post.file, ".html")
          Description = tagline
          Author = { siteAuthor with Name = defaultArg post.author siteAuthor.Name }
          SiteName = siteName
          Headline = Some post.summary
          ObjectType = Some "Blog"
          ContentType = Some "BlogPosting"
          OpenGraphType = Some "article"
          Locale = Some "en-us"
          Published = post.published
          Modified = post.modified
          Tags = Some post.tags
          Meta =
              Some [ ("Image", defaultArg post.image $"{siteRoot}/images/avatar.jpg")
                     ("Publisher", defaultArg siteName siteAuthor.Name) ] }

    ctx.Add(postMeta)
    // . . .

Render SEO metadata in your page layout

// generators/layout.fsx
#load @"../.paket/load/net8.0/Fornax.Seo.fsx"

open Html
open Fornax.Seo

// . . .

let layout (ctx: SiteContents) (active: string) (content: HtmlElement seq) =
    let siteAuthor =
        ctx.TryGetValue<ContentCreator>()
        |> Option.defaultValue ContentCreator.Default

    let seoData =
        ctx.TryGetValues<ContentObject>()
        |> Option.defaultValue Seq.empty

    let pageMeta =
        seoData
        |> Seq.tryFind (fun p -> p.Title.Contains(active))
        |> function
        | Some info -> info
        | _ -> { ContentObject.Default with Author = siteAuthor }

    html [] [
        head [] [
            meta [ CharSet "utf-8" ]
            meta [ Name "viewport"; Content "width=device-width, initial-scale=1" ]
            // . . .
            yield! seo pageMeta
        ]
        body [] [
            // . . .
            footer [] [ yield! socialMedia siteAuthor ]
        ]
    ]

    // . . .

Similar NuGet libraries (by framework)

.NET

ASP.NET

Umbraco

Contributing

A guide to building the project and making pull requests can be found here.

License

Distributed under the terms of the Mozilla Public License Version 2.0.