Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement twitter cards #124

Closed
appieschot opened this issue Apr 29, 2022 · 6 comments
Closed

Implement twitter cards #124

appieschot opened this issue Apr 29, 2022 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@appieschot
Copy link
Member

Article issue

The template currently does not render the header image when posting a link to twitter. We should update our template to provide the required OpenGraph or Twitter info to render images propperly.

We can use https://cards-dev.twitter.com/validator to validate our current template.

@appieschot appieschot self-assigned this Apr 29, 2022
@appieschot appieschot added the enhancement New feature or request label Apr 29, 2022
@appieschot
Copy link
Member Author

I'll pick this one up, for future reference:

Remove the following snippet to fall back to custom implementation

{{ template "_internal/opengraph.html" . }}
{{ template "_internal/twitter_cards.html" . }}

Rewrite to use correct image (currently using the svg logo)

<!-- Open Graph image and Twitter Card metadata -->
{{ $image_path := .Params.image | default site.Params.image }}
{{ $image_path_local := printf "static/%s" $image_path }}
{{ $image_ext := trim (path.Ext $image_path | lower) "." }}
{{ if fileExists $image_path_local }}
<meta property="og:image" content="{{ $image_path | absURL }}" />
<!-- If not SVG, read image aspect ratio and define Twitter Card and Open Graph width and height -->
{{ if ne $image_ext "svg" }}
{{ with (imageConfig $image_path_local) }}
{{ if (and (gt .Width 144) (gt .Height 144)) }}
<meta name="twitter:image" content="{{ $image_path | absURL }}" />
<meta name="twitter:card"
content="summary{{ if (and (gt .Width 300) (gt .Height 157) (not (eq .Width .Height))) }}_large_image{{ end }}">
{{ end }}
<meta property="og:image:width" content="{{ .Width }}">
<meta property="og:image:height" content="{{ .Height }}">
{{ end }}
{{ end }}
<meta property="og:image:type"
content="image/{{ if eq $image_ext `svg` }}svg+xml{{ else }}{{ replaceRE `^jpg$` `jpeg` $image_ext }}{{ end }}">
{{ end }}
<meta name="twitter:title" content="{{ with .Title }}{{ . }}{{ else }}{{ site.Title }}{{ end }}" />
<meta name="twitter:description"
content="{{ with .Description }}{{ . }}{{ else }}{{ if .IsPage }}{{ .Summary }}{{ else }}{{ with site.Params.description }}{{ . }}{{ end }}{{ end }}{{ end }}" />
{{ with site.Social.twitter }}
<meta name="twitter:site" content="@{{ . }}" />{{ end }}
{{ range site.Authors }}
{{ with .twitter }}
<meta name="twitter:creator" content="@{{ . }}" />{{ end }}
{{ end }}

@LuiseFreese
Copy link
Collaborator

hey @appieschot can you please only change the files in the blog/layouts folder and not in the blogs/themes/reader-hugo/layouts folder? This way we don't override the theme. This was @hugoabernier s suggestion :-)

@appieschot
Copy link
Member Author

Will look into it, we should be able to do I think 🦾

@appieschot
Copy link
Member Author

@hugoabernier do you want me to revert #127 and implement a custom file in the /layouts folder as well? That way we could keep the reader-hugo as is I guess?

@LuiseFreese
Copy link
Collaborator

that would be perfect!

appieschot pushed a commit to appieschot/pnp-blog that referenced this issue May 6, 2022
appieschot added a commit that referenced this issue May 6, 2022
Implemented correct twitter image. Solves #124
appieschot pushed a commit to appieschot/pnp-blog that referenced this issue May 6, 2022
appieschot added a commit that referenced this issue May 6, 2022
Implemented correct twitter image. Solves #124
@appieschot
Copy link
Member Author

Solved with last PR, cards should be visible now.

appieschot added a commit to appieschot/pnp-blog that referenced this issue May 11, 2022
appieschot added a commit to appieschot/pnp-blog that referenced this issue May 11, 2022
Implemented correct twitter image. Solves pnp#124
appieschot added a commit to appieschot/pnp-blog that referenced this issue May 11, 2022
appieschot added a commit to appieschot/pnp-blog that referenced this issue May 11, 2022
Implemented correct twitter image. Solves pnp#124
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants