Repos / s4g / a54716c9f7
commit a54716c9f7db746026cfd337c38a81f61cd8c845
Author: Nhân <hi@imnhan.com>
Date:   Mon Aug 21 22:57:32 2023 +0700

    add OpenGraph/Twitter meta tags

diff --git a/README.md b/README.md
index 414befd..572e277 100644
--- a/README.md
+++ b/README.md
@@ -49,8 +49,6 @@ # - starts a local HTTP server for preview, also livereloads on changes
 # TODOs
 
 - When cleaning up outdated files from manifest, delete empty dirs too.
-- Open Graph, Twitter tags (make use of Thumb metadata field too)
 - Checked internal links (link to other article, to other article's asset)
 - Warn when linking to redirected content
-- Home page: align multi-line article title
 - Minify/prettify HTML (optional?)
diff --git a/docs/_s4g/theme/base.tmpl b/docs/_s4g/theme/base.tmpl
index 3dd855a..f27d1ad 100644
--- a/docs/_s4g/theme/base.tmpl
+++ b/docs/_s4g/theme/base.tmpl
@@ -7,6 +7,25 @@
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <link rel="alternate" type="application/atom+xml" title="Atom feed" href="{{.Feed}}">
   <link rel="stylesheet" href="{{.ThemePath}}/base.css">
+
+  <meta property="og:title" content="{{.Post.Title}}" />
+  <meta name="twitter:title" content="{{.Post.Title}}" />
+  <meta name="twitter:card" content="summary" />
+  {{- if .Post.Description -}}
+    <meta property="og:description" content="{{.Post.Description}}" />
+    <meta name="twitter:description" content="{{.Post.Description}}" />
+  {{- end -}}
+  {{- if .Post.Thumb -}}
+    <meta property="og:image" content="{{.Post.Thumb}}" />
+    <meta name="twitter:image" content="{{.Post.Thumb}}" />
+  {{- else if .Site.DefaultThumb -}}
+    <meta property="og:image" content="{{.Site.Root}}{{.Site.DefaultThumb}}" />
+    <meta name="twitter:image" content="{{.Site.Root}}{{.Site.DefaultThumb}}" />
+  {{- end }}
+  {{- if .Site.AuthorTwitter -}}
+    <meta name="twitter:site" content="{{.Site.AuthorTwitter}}" />
+  {{- end }}
+
   {{- template "head" .}}
 </head>
 
diff --git a/docs/about/index.html b/docs/about/index.html
index 46ec36f..d761c95 100644
--- a/docs/about/index.html
+++ b/docs/about/index.html
@@ -7,6 +7,10 @@
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <link rel="alternate" type="application/atom+xml" title="Atom feed" href="/s4g/feed.xml">
   <link rel="stylesheet" href="/s4g/_s4g/theme/base.css">
+
+  <meta property="og:title" content="About" />
+  <meta name="twitter:title" content="About" />
+  <meta name="twitter:card" content="summary" />
 </head>
 
 <body>
diff --git a/docs/index.html b/docs/index.html
index 2e20956..519a861 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -7,6 +7,10 @@
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <link rel="alternate" type="application/atom+xml" title="Atom feed" href="/s4g/feed.xml">
   <link rel="stylesheet" href="/s4g/_s4g/theme/base.css">
+
+  <meta property="og:title" content="Home" />
+  <meta name="twitter:title" content="Home" />
+  <meta name="twitter:card" content="summary" />
 </head>
 
 <body>
diff --git a/docs/mfws.html b/docs/mfws.html
index a9cd041..3bb6415 100644
--- a/docs/mfws.html
+++ b/docs/mfws.html
@@ -7,6 +7,10 @@
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <link rel="alternate" type="application/atom+xml" title="Atom feed" href="/s4g/feed.xml">
   <link rel="stylesheet" href="/s4g/_s4g/theme/base.css">
+
+  <meta property="og:title" content="This is a motherfucking website." />
+  <meta name="twitter:title" content="This is a motherfucking website." />
+  <meta name="twitter:card" content="summary" />
 </head>
 
 <body>
diff --git a/docs/scale/index.html b/docs/scale/index.html
index 183fa76..acb415e 100644
--- a/docs/scale/index.html
+++ b/docs/scale/index.html
@@ -7,6 +7,10 @@
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <link rel="alternate" type="application/atom+xml" title="Atom feed" href="/s4g/feed.xml">
   <link rel="stylesheet" href="/s4g/_s4g/theme/base.css">
+
+  <meta property="og:title" content="I&#39;m Going To Scale My Foot Up Your Ass" />
+  <meta name="twitter:title" content="I&#39;m Going To Scale My Foot Up Your Ass" />
+  <meta name="twitter:card" content="summary" />
 </head>
 
 <body>
diff --git a/metadata.go b/metadata.go
index 1756f77..bf9e52e 100644
--- a/metadata.go
+++ b/metadata.go
@@ -16,23 +16,27 @@
 )
 
 type SiteMetadata struct {
-	Address     string
-	Name        string
-	Tagline     string
-	Root        string
-	ShowFooter  bool
-	NavbarLinks []string
-	AuthorName  string
-	AuthorURI   string
-	AuthorEmail string
+	Address       string
+	Name          string
+	Tagline       string
+	Root          string
+	ShowFooter    bool
+	NavbarLinks   []string
+	DefaultThumb  string
+	AuthorName    string
+	AuthorURI     string
+	AuthorEmail   string
+	AuthorTwitter string
 }
 
 type ArticleMetadata struct {
-	Title      string
-	IsDraft    bool
-	PostedAt   time.Time
-	Templates  []string
-	ShowInFeed bool
+	Title       string
+	Description string
+	IsDraft     bool
+	PostedAt    time.Time
+	Templates   []string
+	ShowInFeed  bool
+	Thumb       string
 }
 
 func NewSiteMetadata() SiteMetadata {
@@ -61,6 +65,15 @@ func ReadSiteMetadata(fsys writablefs.FS) (*SiteMetadata, error) {
 		sm.Root = fmt.Sprintf("/%s/", trimmed)
 	}
 
+	// make sure AuthorTwitter starts with an "@"
+	if len(sm.AuthorTwitter) > 0 && sm.AuthorTwitter[0] != '@' {
+		sm.AuthorTwitter = "@" + sm.AuthorTwitter
+	}
+
+	// trim leading "/" because DefaultThumb will be prepended with web root
+	// path when used.
+	sm.DefaultThumb = strings.TrimPrefix(sm.DefaultThumb, "/")
+
 	return &sm, nil
 }
 
diff --git a/theme/base.tmpl b/theme/base.tmpl
index 3dd855a..f27d1ad 100644
--- a/theme/base.tmpl
+++ b/theme/base.tmpl
@@ -7,6 +7,25 @@
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <link rel="alternate" type="application/atom+xml" title="Atom feed" href="{{.Feed}}">
   <link rel="stylesheet" href="{{.ThemePath}}/base.css">
+
+  <meta property="og:title" content="{{.Post.Title}}" />
+  <meta name="twitter:title" content="{{.Post.Title}}" />
+  <meta name="twitter:card" content="summary" />
+  {{- if .Post.Description -}}
+    <meta property="og:description" content="{{.Post.Description}}" />
+    <meta name="twitter:description" content="{{.Post.Description}}" />
+  {{- end -}}
+  {{- if .Post.Thumb -}}
+    <meta property="og:image" content="{{.Post.Thumb}}" />
+    <meta name="twitter:image" content="{{.Post.Thumb}}" />
+  {{- else if .Site.DefaultThumb -}}
+    <meta property="og:image" content="{{.Site.Root}}{{.Site.DefaultThumb}}" />
+    <meta name="twitter:image" content="{{.Site.Root}}{{.Site.DefaultThumb}}" />
+  {{- end }}
+  {{- if .Site.AuthorTwitter -}}
+    <meta name="twitter:site" content="{{.Site.AuthorTwitter}}" />
+  {{- end }}
+
   {{- template "head" .}}
 </head>