]> danix's work - danix.xyz-2.git/commitdiff
feat: add Open Graph and Twitter Card meta tags for social sharing
authorDanilo M. <redacted>
Sat, 18 Apr 2026 18:22:10 +0000 (20:22 +0200)
committerDanilo M. <redacted>
Sat, 18 Apr 2026 18:22:10 +0000 (20:22 +0200)
Implement comprehensive OG and Twitter Card support:
- og:title, og:description, og:image, og:type (website/article)
- og:article:published_time and og:article:author for article pages
- twitter:card with summary_large_image, twitter:title, twitter:description, twitter:image
- Per-page description from excerpt field with fallback to site description
- Article-specific tags only rendered for pages with dates
- Default dark thumbnail fallback when article has no image
- Empty twitterHandle param (optional fill-in for users)

Extracted head meta into new partial for maintainability.

Co-Authored-By: Claude Haiku 4.5 <redacted>
hugo.toml
themes/danix-xyz-hacker/layouts/_default/baseof.html
themes/danix-xyz-hacker/layouts/partials/head-meta.html [new file with mode: 0644]

index c20b6175441ac2b79cf4b17d3d4a585a1de354b2..ad31c860491b2339cbaf40a05f6fbe1fa03ef909 100644 (file)
--- a/hugo.toml
+++ b/hugo.toml
@@ -109,6 +109,7 @@ enableRobotsTXT = true
   siteName = "danix.xyz"
   author = "Danilo Macrì"
   email = "danix@danix.xyz"
+  twitterHandle = ""
 
   # Theme options
   readingTime = true
index 9370157cf5c8cd9fc61b16052af55b3a03f777df..1f5dc97295620d35a41fe5413c281037a5add209 100644 (file)
@@ -3,11 +3,7 @@
 <head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
-  <meta name="description" content="{{ .Site.Params.siteDescription }}">
-  <meta property="og:locale" content="{{ .Site.Language.Params.locale }}">
-  <meta property="og:type" content="website">
-  <meta property="og:url" content="{{ .Permalink }}">
-  <meta property="og:site_name" content="{{ .Site.Title }}">
+  {{ partial "head-meta.html" . }}
   <title>{{ .Title }}{{ if ne .Title .Site.Title }} — {{ .Site.Title }}{{ end }}</title>
 
   <!-- Favicon -->
diff --git a/themes/danix-xyz-hacker/layouts/partials/head-meta.html b/themes/danix-xyz-hacker/layouts/partials/head-meta.html
new file mode 100644 (file)
index 0000000..cd4e88c
--- /dev/null
@@ -0,0 +1,38 @@
+{{/* description: per-page excerpt wins over site-wide description */}}
+{{ $description := .Site.Language.Params.siteDescription }}
+{{ with .Params.excerpt }}{{ $description = . }}{{ end }}
+
+{{/* og:type: "article" only for single content pages that have a date */}}
+{{ $ogType := "website" }}
+{{ if and (eq .Kind "page") .Date }}{{ $ogType = "article" }}{{ end }}
+
+{{/* og:image: page image wins; fall back to default dark thumbnail */}}
+{{ $ogImage := printf "%s%s" .Site.BaseURL "images/default_thumbnail_dark.png" }}
+{{ with .Params.image }}{{ $ogImage = printf "%s%s" $.Site.BaseURL (strings.TrimLeft "/" .) }}{{ end }}
+
+{{/* author: page-level param wins; fall back to site param */}}
+{{ $author := .Site.Params.author }}
+{{ with .Params.author }}{{ $author = . }}{{ end }}
+
+<meta name="description" content="{{ $description }}">
+
+<meta property="og:title" content="{{ .Title }}">
+<meta property="og:description" content="{{ $description }}">
+<meta property="og:type" content="{{ $ogType }}">
+<meta property="og:url" content="{{ .Permalink }}">
+<meta property="og:site_name" content="{{ .Site.Title }}">
+<meta property="og:locale" content="{{ .Site.Language.Params.locale }}">
+<meta property="og:image" content="{{ $ogImage }}">
+
+{{ if eq $ogType "article" }}
+<meta property="article:published_time" content="{{ .Date.Format "2006-01-02T15:04:05Z07:00" }}">
+<meta property="article:author" content="{{ $author }}">
+{{ end }}
+
+<meta name="twitter:card" content="summary_large_image">
+<meta name="twitter:title" content="{{ .Title }}">
+<meta name="twitter:description" content="{{ $description }}">
+<meta name="twitter:image" content="{{ $ogImage }}">
+{{ with .Site.Params.twitterHandle }}
+<meta name="twitter:site" content="@{{ . }}">
+{{ end }}