move opengraph head metadata stuff into its own template partial.

This commit is contained in:
fiatjaf
2023-10-30 22:51:13 -03:00
parent 4639c2dbcf
commit 1301f38443
6 changed files with 113 additions and 135 deletions

View File

@@ -21,6 +21,29 @@ const (
Other
)
var (
//go:embed templates/opengraph.html
tmplOpenGraph string
OpenGraphTemplate = tmpl.MustCompile(&OpenGraphPartial{})
)
//tmpl:bind head_common.html
type OpenGraphPartial struct {
IsTwitter bool
TitleizedContent string
Title string
TwitterTitle string
Proxy string
AuthorLong string
TextImageURL string
Video string
VideoType string
Image string
Description string
}
func (*OpenGraphPartial) TemplateText() string { return tmplOpenGraph }
var (
//go:embed templates/head_common.html
tmplHeadCommon string
@@ -33,6 +56,7 @@ type HeadCommonPartial struct {
TailwindDebugStuff template.HTML
NaddrNaked string
NeventNaked string
Oembed string
}
func (*HeadCommonPartial) TemplateText() string { return tmplHeadCommon }
@@ -185,32 +209,22 @@ var (
)
type NotePage struct {
OpenGraphPartial `tmpl:"opengraph"`
HeadCommonPartial `tmpl:"head_common"`
TopPartial `tmpl:"top"`
DetailsPartial `tmpl:"details"`
ClientsPartial `tmpl:"clients"`
FooterPartial `tmpl:"footer"`
AuthorLong string
Content template.HTML
CreatedAt string
Description string
Image string
Metadata nostr.ProfileMetadata
Npub string
NpubShort string
Oembed string
ParentLink template.HTML
Proxy string
SeenOn []string
IsTwitter bool
Subject string
TextImageURL string
Title string
TitleizedContent string
TwitterTitle string
Video string
VideoType string
}
func (*NotePage) TemplateText() string { return tmplNote }
@@ -253,31 +267,23 @@ var (
)
type FileMetadataPage struct {
OpenGraphPartial `tmpl:"opengraph"`
HeadCommonPartial `tmpl:"head_common"`
TopPartial `tmpl:"top"`
DetailsPartial `tmpl:"details"`
ClientsPartial `tmpl:"clients"`
FooterPartial `tmpl:"footer"`
AuthorLong string
Content template.HTML
CreatedAt string
Description string
Metadata nostr.ProfileMetadata
Npub string
NpubShort string
Oembed string
ParentLink template.HTML
Proxy string
SeenOn []string
Style Style
Subject string
TextImageURL string
Title string
TitleizedContent string
TwitterTitle string
Video string
VideoType string
// Specific Metadata
Url string

View File

@@ -253,8 +253,22 @@ func renderEvent(w http.ResponseWriter, r *http.Request) {
})
case Note:
err = NoteTemplate.Render(w, &NotePage{
OpenGraphPartial: OpenGraphPartial{
IsTwitter: style == StyleTwitter,
Proxy: "https://" + host + "/njump/proxy?src=",
Title: title,
TwitterTitle: twitterTitle,
TitleizedContent: titleizedContent,
TextImageURL: textImageURL,
Image: data.image,
Video: data.video,
VideoType: data.videoType,
Description: description,
AuthorLong: data.authorLong,
},
HeadCommonPartial: HeadCommonPartial{
IsProfile: false,
Oembed: oembed,
TailwindDebugStuff: tailwindDebugStuff,
NaddrNaked: data.naddrNaked,
NeventNaked: data.neventNaked,
@@ -264,25 +278,14 @@ func renderEvent(w http.ResponseWriter, r *http.Request) {
Clients: generateClientList(style, code, data.event),
},
AuthorLong: data.authorLong,
Content: template.HTML(data.content),
CreatedAt: data.createdAt,
Description: description,
Image: data.image,
Metadata: data.metadata,
Npub: data.npub,
NpubShort: data.npubShort,
Oembed: oembed,
ParentLink: data.parentLink,
Proxy: "https://" + host + "/njump/proxy?src=",
IsTwitter: style == StyleTwitter,
Subject: subject,
TextImageURL: textImageURL,
Title: title,
TitleizedContent: titleizedContent,
TwitterTitle: twitterTitle,
Video: data.video,
VideoType: data.videoType,
})
case FileMetadata:
thisImage := data.kind1063Metadata.image
@@ -290,6 +293,19 @@ func renderEvent(w http.ResponseWriter, r *http.Request) {
thisImage = data.image
}
err = FileMetadataTemplate.Render(w, &FileMetadataPage{
OpenGraphPartial: OpenGraphPartial{
IsTwitter: style == StyleTwitter,
Proxy: "https://" + host + "/njump/proxy?src=",
TitleizedContent: titleizedContent,
TwitterTitle: twitterTitle,
Title: title,
TextImageURL: textImageURL,
Video: data.video,
VideoType: data.videoType,
Image: thisImage,
Description: description,
AuthorLong: data.authorLong,
},
HeadCommonPartial: HeadCommonPartial{
IsProfile: false,
TailwindDebugStuff: tailwindDebugStuff,
@@ -301,20 +317,13 @@ func renderEvent(w http.ResponseWriter, r *http.Request) {
Clients: generateClientList(style, code, data.event),
},
AuthorLong: data.authorLong,
CreatedAt: data.createdAt,
Metadata: data.metadata,
Description: description,
Npub: data.npub,
NpubShort: data.npubShort,
Style: style,
Subject: subject,
TextImageURL: textImageURL,
Title: title,
TitleizedContent: titleizedContent,
TwitterTitle: twitterTitle,
Video: data.video,
VideoType: data.videoType,
Url: data.kind1063Metadata.url,
M: data.kind1063Metadata.m,
Aes256Gcm: data.kind1063Metadata.aes256gcm,

View File

@@ -2,50 +2,7 @@
<html class="theme--default text-lg font-light print:text-base sm:text-xl">
<meta charset="UTF-8" />
<head>
<title>{{.TitleizedContent}}</title>
<meta property="og:title" content="{{.Title}}" />
{{ if eq .Style "twitter" }}
<meta name="twitter:title" content="{{.TwitterTitle}}" />
{{ end }}
<meta property="og:site_name" content="{{.AuthorLong}}" />
{{ if not (eq "" .TextImageURL) }}
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@nostrprotocol" />
<meta property="og:image" content="{{.TextImageURL}}" />
<meta name="twitter:image" content="{{.TextImageURL}}" />
{{ else }}
<!---->
<meta property="twitter:card" content="summary" />
{{ if not (eq "" .Image) }}
<meta property="og:image" content="{{.Image}}" />
<meta name="twitter:image" content="{{.Proxy}}{{.Image}}" />
{{ end }} {{ if not (eq "" .Video) }}
<meta property="og:video" content="{{.Video}}" />
<meta property="og:video:secure_url" content="{{.Video}}" />
<meta property="og:video:type" content="video/{{.VideoType}}" />
{{ end }} {{ end }}
<!---->
{{ if not (eq "" .Description) }}
<meta property="og:description" content="{{.Description}}" />
<meta name="twitter:description" content="{{.Description}}" />
{{ end }}
<!---->
{{ if not (eq "" .Oembed) }}
<link
rel="alternate"
type="application/json+oembed"
href="{{.Oembed}}&format=json"
/>
<link
rel="alternate"
type="text/xml+oembed"
href="{{.Oembed}}&format=xml"
/>
{{ end }}
{{template "opengraph" .OpenGraphPartial}}
<!---->
{{template "head_common" .HeadCommonPartial}}
</head>
@@ -97,7 +54,7 @@
></div>
<article
class="prose dark:prose-invert sm:prose-a:text-justify prose-headings:font-light prose-blockquote:mx-0 prose-blockquote:my-8 prose-blockquote:pl-4 prose-blockquote:pr-0 prose-blockquote:py-2 prose-blockquote:border-l-05rem prose-blockquote:border-solid prose-blockquote:border-l-gray-100 dark:prose-blockquote:border-l-zinc-800 prose-p:m-0 prose-p:mb-2 prose-cite:text-sm prose-ul:m-0 prose-ul:p-0 prose-ul:pl-4 prose-ol:m-0 prose-ol:p-0 prose-ol:pl-4 prose-li:mb-2 prose mb-6 leading-5"
class="prose-cite:text-sm prose prose mb-6 leading-5 dark:prose-invert prose-headings:font-light prose-p:m-0 prose-p:mb-2 prose-blockquote:mx-0 prose-blockquote:my-8 prose-blockquote:border-l-05rem prose-blockquote:border-solid prose-blockquote:border-l-gray-100 prose-blockquote:py-2 prose-blockquote:pl-4 prose-blockquote:pr-0 prose-ol:m-0 prose-ol:p-0 prose-ol:pl-4 prose-ul:m-0 prose-ul:p-0 prose-ul:pl-4 prose-li:mb-2 dark:prose-blockquote:border-l-zinc-800 sm:prose-a:text-justify"
>
{{ if (not (eq "" .Subject))}}
<h1 class="text-2xl">{{.Subject}}</h1>
@@ -109,16 +66,25 @@
<!-- main content -->
{{ if (not (eq "" .Image))}}
<img src="{{ .Image }}" alt="{{ .Alt }}" />
{{ else if (eq "image" .MType)}}
<img src="{{ .Url }}" alt="{{ .Alt }}" />
{{ else if (eq "video" .MType)}}
<video controls width="100%%" class="max-h-[90vh] bg-neutral-300 dark:bg-zinc-700">
<source src="{{ .Url }}" alt="{{ .Alt }}"></video>
<img src="{{ .Image }}" alt="{{ .Alt }}" />
{{ else if (eq "image" .MType)}}
<img src="{{ .Url }}" alt="{{ .Alt }}" />
{{ else if (eq "video" .MType)}}
<video
controls
width="100%%"
class="max-h-[90vh] bg-neutral-300 dark:bg-zinc-700"
>
<source src="{{ .Url }}" alt="{{ .Alt }}" />
</video>
{{ end }}
<a href="{{ .Url }}" target="_new" class="block not-prose bg-strongpink mb-3 rounded-lg border-0 block basis-full px-4 text-[17px] font-normal text-white no-underline py-2 text-center font-light mx-auto w-2/6 text-center">Download file</a>
<a
href="{{ .Url }}"
target="_new"
class="not-prose mx-auto mb-3 block block w-2/6 basis-full rounded-lg border-0 bg-strongpink px-4 py-2 text-center text-center text-[17px] font-light font-normal text-white no-underline"
>Download file</a
>
</article>
{{template "details" .DetailsPartial}}

View File

@@ -1,5 +1,16 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
{{ if not (eq "" .Oembed) }}
<link
rel="alternate"
type="application/json+oembed"
href="{{.Oembed}}&format=json"
/>
<link rel="alternate" type="text/xml+oembed" href="{{.Oembed}}&format=xml" />
{{ end }}
<!---->
{{if .IsProfile}}
<link
rel="apple-touch-icon"

View File

@@ -2,50 +2,7 @@
<html class="theme--default text-lg font-light print:text-base sm:text-xl">
<meta charset="UTF-8" />
<head>
<title>{{.TitleizedContent}}</title>
<meta property="og:title" content="{{.Title}}" />
{{ if .IsTwitter }}
<meta name="twitter:title" content="{{.TwitterTitle}}" />
{{ end }}
<meta property="og:site_name" content="{{.AuthorLong}}" />
{{ if not (eq "" .TextImageURL) }}
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@nostrprotocol" />
<meta property="og:image" content="{{.TextImageURL}}" />
<meta name="twitter:image" content="{{.TextImageURL}}" />
{{ else }}
<!---->
<meta property="twitter:card" content="summary" />
{{ if not (eq "" .Image) }}
<meta property="og:image" content="{{.Image}}" />
<meta name="twitter:image" content="{{.Proxy}}{{.Image}}" />
{{ end }} {{ if not (eq "" .Video) }}
<meta property="og:video" content="{{.Video}}" />
<meta property="og:video:secure_url" content="{{.Video}}" />
<meta property="og:video:type" content="video/{{.VideoType}}" />
{{ end }} {{ end }}
<!---->
{{ if not (eq "" .Description) }}
<meta property="og:description" content="{{.Description}}" />
<meta name="twitter:description" content="{{.Description}}" />
{{ end }}
<!---->
{{ if not (eq "" .Oembed) }}
<link
rel="alternate"
type="application/json+oembed"
href="{{.Oembed}}&format=json"
/>
<link
rel="alternate"
type="text/xml+oembed"
href="{{.Oembed}}&format=xml"
/>
{{ end }}
{{template "opengraph" .OpenGraphPartial}}
<!---->
{{template "head_common" .HeadCommonPartial}}
</head>

29
templates/opengraph.html Normal file
View File

@@ -0,0 +1,29 @@
<title>{{.TitleizedContent}}</title>
<meta property="og:title" content="{{.Title}}" />
{{ if .IsTwitter }}
<meta name="twitter:title" content="{{.TwitterTitle}}" />
{{ end }}
<meta property="og:site_name" content="{{.AuthorLong}}" />
{{ if not (eq "" .TextImageURL) }}
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@nostrprotocol" />
<meta property="og:image" content="{{.TextImageURL}}" />
<meta name="twitter:image" content="{{.TextImageURL}}" />
{{ else }}
<!---->
<meta property="twitter:card" content="summary" />
{{ if not (eq "" .Image) }}
<meta property="og:image" content="{{.Image}}" />
<meta name="twitter:image" content="{{.Proxy}}{{.Image}}" />
{{ end }} {{ if not (eq "" .Video) }}
<meta property="og:video" content="{{.Video}}" />
<meta property="og:video:secure_url" content="{{.Video}}" />
<meta property="og:video:type" content="video/{{.VideoType}}" />
{{ end }} {{ end }}
<!---->
{{ if not (eq "" .Description) }}
<meta property="og:description" content="{{.Description}}" />
<meta name="twitter:description" content="{{.Description}}" />
{{ end }}