fix: update title/description

This commit is contained in:
Kieran 2024-01-15 11:40:29 +00:00
parent 66842b4c7b
commit 52b308967f
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941

View File

@ -200,6 +200,43 @@ public class OpenGraphController : Controller
}
doc.Head?.AppendChild(tag);
var isOgTitle = ex.Attributes.Any(a => a is {Key: "property", Value: "og:title"});
if (isOgTitle && doc.Head != default)
{
var titleTag = doc.Head.QuerySelector("title");
var ogTitle = ex.Attributes.FirstOrDefault(a => a.Key is "content");
if (!string.IsNullOrEmpty(ogTitle.Value))
{
if (titleTag == default)
{
var newTitle = doc.CreateElement("title");
doc.Head.AppendChild(newTitle);
titleTag = newTitle;
}
titleTag.TextContent = ogTitle.Value;
}
}
var isOgDesc = ex.Attributes.Any(a => a is {Key: "property", Value: "og:description"});
if (isOgDesc && doc.Head != default)
{
var ogDesc = ex.Attributes.FirstOrDefault(a => a.Key is "content");
var descriptionTag = doc.Head?.QuerySelector("meta[name='description']");
if (!string.IsNullOrEmpty(ogDesc.Value))
{
if (descriptionTag == default)
{
var newDesc = doc.CreateElement("meta");
newDesc.SetAttribute("name", "description");
doc.Head?.AppendChild(newDesc);
descriptionTag = newDesc;
}
descriptionTag.SetAttribute("content", ogDesc.Value);
}
}
}
return doc;