Support relative URLs in OpenGraph properties (#52)

This commit is contained in:
2025-08-01 22:16:19 -04:00
parent 8b190a6c23
commit cba1bbfa28
9 changed files with 172 additions and 140 deletions
+6
View File
@@ -388,6 +388,12 @@ type WebLog = {
member this.AbsoluteUrl(permalink: Permalink) =
$"{this.UrlBase}/{permalink}"
/// <summary>Convert a string URL to an absolute URL for this web log if required</summary>
/// <param name="url">The URL which may be translated to an absolute one</param>
/// <returns>The given URL if it was already absolute, or a corresponding absolute URL if not</returns>
member this.UrlToAbsolute(url: string) =
if url.StartsWith "http" then url else this.AbsoluteUrl(Permalink url)
/// <summary>Generate a relative URL for the given link</summary>
/// <param name="permalink">The permalink for which a relative URL should be generated</param>
/// <returns>A relative URL for the given link</returns>
+24 -13
View File
@@ -418,9 +418,12 @@ type OpenGraphAudio = {
|> dict
/// <summary>The <c>meta</c> properties for this image</summary>
member this.Properties = seq {
yield "og:audio", this.Url
if this.Url.StartsWith "https:" then yield "og:audio:secure_url", this.Url
/// <param name="urlTransform">A function to convert relative URLs to absolute URLs</param>
/// <returns>A sequence of key/value pairs for this OpenGraph audio file</returns>
member this.ToProperties(urlTransform: string -> string) = seq {
let url = urlTransform this.Url
yield "og:audio", url
if url.StartsWith "https:" then yield "og:audio:secure_url", url
match this.Type with
| Some typ -> yield "og:audio:type", typ
| None ->
@@ -472,9 +475,12 @@ type OpenGraphImage = {
|> dict
/// <summary>The <c>meta</c> properties for this image</summary>
member this.Properties = seq {
yield "og:image", this.Url
if this.Url.StartsWith "https:" then yield "og:image:secure_url", this.Url
/// <param name="urlTransform">A function to convert relative URLs to absolute URLs</param>
/// <returns>A sequence of key/value pairs for this OpenGraph image file</returns>
member this.ToProperties(urlTransform: string -> string) = seq {
let url = urlTransform this.Url
yield "og:image", url
if url.StartsWith "https:" then yield "og:image:secure_url", url
match this.Type with
| Some typ -> yield "og:image:type", typ
| None ->
@@ -520,9 +526,12 @@ type OpenGraphVideo = {
|> dict
/// <summary>The <c>meta</c> properties for this video</summary>
member this.Properties = seq {
yield "og:video", this.Url
if this.Url.StartsWith "https:" then yield "og:video:secure_url", this.Url
/// <param name="urlTransform">A function to convert relative URLs to absolute URLs</param>
/// <returns>A sequence of key/value pairs for this OpenGraph video file</returns>
member this.ToProperties(urlTransform: string -> string) = seq {
let url = urlTransform this.Url
yield "og:video", url
if url.StartsWith "https:" then yield "og:video:secure_url", url
match this.Type with
| Some typ -> yield "og:video:type", typ
| None ->
@@ -633,17 +642,19 @@ type OpenGraphProperties = {
Other = None }
/// <summary>The <c>meta</c> properties for this page or post</summary>
member this.Properties = seq {
/// <param name="urlTransform">A function to convert relative URLs to absolute URLs</param>
/// <returns>A sequence of key/value pairs for this set of OpenGraph properties</returns>
member this.ToProperties urlTransform = seq {
yield "og:type", string this.Type
yield! this.Image.Properties
yield! this.Image.ToProperties urlTransform
match this.Description with Some desc -> yield "og:description", desc | None -> ()
match this.Determiner with Some det -> yield "og:determiner", det | None -> ()
match this.Locale with Some loc -> yield "og:locale", loc | None -> ()
match this.LocaleAlternate with
| Some alt -> yield! alt |> List.map (fun it -> "og:locale:alternate", it)
| None -> ()
match this.Audio with Some audio -> yield! audio.Properties | None -> ()
match this.Video with Some video -> yield! video.Properties | None -> ()
match this.Audio with Some audio -> yield! audio.ToProperties urlTransform | None -> ()
match this.Video with Some video -> yield! video.ToProperties urlTransform | None -> ()
match this.Other with Some oth -> yield! oth |> List.map (fun it -> it.Name, it.Value) | None -> ()
}
+8 -13
View File
@@ -504,21 +504,18 @@ type EditCommonModel() =
post.OpenGraph |> Option.iter this.PopulateOpenGraph
/// <summary>Convert the properties of the model into a set of OpenGraph properties</summary>
/// <param name="webLog">The current web log</param>
member this.ToOpenGraph(webLog: WebLog) =
member this.ToOpenGraph() =
if this.AssignOpenGraph then
let toAbsolute (url: string) =
if url.StartsWith "http" then url else webLog.AbsoluteUrl (Permalink url)
let audio =
match this.OpenGraphAudioUrl.Trim() with
| "" -> None
| url -> Some { OpenGraphAudio.Url = toAbsolute url; Type = noneIfBlank this.OpenGraphAudioType }
| url -> Some { OpenGraphAudio.Url = url; Type = noneIfBlank this.OpenGraphAudioType }
let video =
match this.OpenGraphVideoUrl.Trim() with
| "" -> None
| url ->
Some {
OpenGraphVideo.Url = toAbsolute url
OpenGraphVideo.Url = url
Type = noneIfBlank this.OpenGraphVideoType
Width = noneIfBlank this.OpenGraphVideoWidth |> Option.map int
Height = noneIfBlank this.OpenGraphVideoHeight |> Option.map int
@@ -526,7 +523,7 @@ type EditCommonModel() =
Some {
Type = if this.OpenGraphType = "" then Article else OpenGraphType.Parse this.OpenGraphType
Image = {
Url = toAbsolute this.OpenGraphImageUrl
Url = this.OpenGraphImageUrl
Type = noneIfBlank this.OpenGraphImageType
Width = noneIfBlank this.OpenGraphImageWidth |> Option.map int
Height = noneIfBlank this.OpenGraphImageHeight |> Option.map int
@@ -749,10 +746,9 @@ type EditPageModel() =
/// <summary>Update a page with values from this model</summary>
/// <param name="page">The page to be updated</param>
/// <param name="webLog">The web log to which this page belongs</param>
/// <param name="now">The <c>Instant</c> to use for this particular update</param>
/// <returns>The page, updated with the values from this model</returns>
member this.UpdatePage (page: Page) webLog now =
member this.UpdatePage (page: Page) now =
let revision = { AsOf = now; Text = MarkupText.Parse $"{this.Source}: {this.Text}" }
// Detect a permalink change, and add the prior one to the prior list
match string page.Permalink with
@@ -768,7 +764,7 @@ type EditPageModel() =
IsInPageList = this.IsShownInPageList
Template = match this.Template with "" -> None | tmpl -> Some tmpl
Text = revision.Text.AsHtml()
OpenGraph = this.ToOpenGraph webLog
OpenGraph = this.ToOpenGraph()
Metadata = Seq.zip this.MetaNames this.MetaValues
|> Seq.filter (fun it -> fst it > "")
|> Seq.map (fun it -> { Name = fst it; Value = snd it })
@@ -908,10 +904,9 @@ type EditPostModel() =
/// <summary>Update a post with values from the submitted form</summary>
/// <param name="post">The post which should be updated</param>
/// <param name="webLog">The web log to which this post belongs</param>
/// <param name="now">The <c>Instant</c> to use for this particular update</param>
/// <returns>The post, updated with the values from this model</returns>
member this.UpdatePost (post: Post) (webLog: WebLog) now =
member this.UpdatePost (post: Post) now =
let revision = { AsOf = now; Text = MarkupText.Parse $"{this.Source}: {this.Text}" }
// Detect a permalink change, and add the prior one to the prior list
match string post.Permalink with
@@ -935,7 +930,7 @@ type EditPostModel() =
Template = match this.Template.Trim() with "" -> None | tmpl -> Some tmpl
CategoryIds = this.CategoryIds |> Array.map CategoryId |> List.ofArray
Status = if this.DoPublish then Published else post.Status
OpenGraph = this.ToOpenGraph webLog
OpenGraph = this.ToOpenGraph()
Metadata = Seq.zip this.MetaNames this.MetaValues
|> Seq.filter (fun it -> fst it > "")
|> Seq.map (fun it -> { Name = fst it; Value = snd it })