Add sitemap.

This commit is contained in:
Dillon Kearns 2020-01-07 17:10:00 -08:00
parent 40e89ccec2
commit 43f494a84d
3 changed files with 82 additions and 0 deletions

View File

@ -20,6 +20,7 @@ import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Exploration as D
import MarkdownRenderer
import Metadata exposing (Metadata)
import MySitemap
import Pages exposing (images, pages)
import Pages.Directory as Directory exposing (Directory)
import Pages.Document
@ -81,6 +82,7 @@ generateFiles :
}
generateFiles siteMetadata =
[ Feed.fileToGenerate { siteTagline = siteTagline, siteUrl = canonicalSiteUrl } siteMetadata
, MySitemap.build { siteUrl = canonicalSiteUrl } siteMetadata
]

View File

@ -0,0 +1,37 @@
module MySitemap exposing (..)
import Dict
import Metadata exposing (Metadata(..))
import Pages
import Pages.PagePath as PagePath exposing (PagePath)
import RssFeed
import Sitemap
import Time
import Xml
import Xml.Encode exposing (..)
build :
{ siteUrl : String
}
->
List
{ path : PagePath Pages.PathKey
, frontmatter : Metadata
}
->
{ path : List String
, content : String
}
build config siteMetadata =
{ path = [ "sitemap.xml" ]
, content =
Sitemap.build config
(siteMetadata
|> List.map
(\page ->
page.path
|> PagePath.toString
)
)
}

View File

@ -0,0 +1,43 @@
module Sitemap exposing (build)
import Date exposing (Date)
import Dict
import Imf.DateTime
import Time
import Xml
import Xml.Encode exposing (..)
build :
{ siteUrl : String
}
-> List String
-> String
build { siteUrl } urls =
object
[ ( "urlset"
, Dict.fromList
[ ( "xmlns", string "http://www.sitemaps.org/schemas/sitemap/0.9" )
]
, urls
|> List.map (urlXml siteUrl)
|> list
)
]
|> encode 0
urlXml siteUrl url =
object
[ ( "url"
, Dict.empty
, list
[ keyValue "loc" <| string (siteUrl ++ url)
]
)
]
keyValue : String -> Xml.Value -> Xml.Value
keyValue key value =
object [ ( key, Dict.empty, value ) ]