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

Twitter og:description doubly escaped by Hugo 0.125.x #12418

Closed
Tracked by #1907
chalin opened this issue Apr 23, 2024 · 7 comments · Fixed by #12425 or google/docsy#1950
Closed
Tracked by #1907

Twitter og:description doubly escaped by Hugo 0.125.x #12418

chalin opened this issue Apr 23, 2024 · 7 comments · Fixed by #12425 or google/docsy#1950

Comments

@chalin
Copy link

chalin commented Apr 23, 2024

If you build this small repro (test-site-2024-04-23.zip) using Hugo 0.124.1 and 0.125.2, you'll see the following difference in the content of <meta property="og:description"> for http://localhost:1313/docs/page:

0.124.1: <meta property="og:description" content="Test line with a single quote: can&rsquo;t." />
0.125.2: <meta property="og:description" content="Test line with a single quote: can&amp;rsquo;t.">

Notice that 0.125.2 doubly escapes the content, so that:

  • can&rsquo;t becomes
  • can&amp;rsquo;t

This seems wrong to me. WDYT?


Below I include a bit more header context.

Hugo 0.124.1:

<meta name="description" content="Test line with a single quote: can&amp;rsquo;t.">
<meta property="og:title" content="Some page" />
<meta property="og:description" content="Test line with a single quote: can&rsquo;t." />
...
<meta name="twitter:description" content="Test line with a single quote: can&rsquo;t."/>

Hugo 0.125.2:

<meta name="description" content="Test line with a single quote: can&amp;rsquo;t.">
<meta property="og:url" content="http://localhost:1313/docs/page/">
  <meta property="og:site_name" content="My New Hugo Site">
  <meta property="og:title" content="Some page">
  <meta property="og:description" content="Test line with a single quote: can&amp;rsquo;t.">
  ...
<meta name="twitter:description" content="Test line with a single quote: can&rsquo;t.">
@jmooring
Copy link
Member

I'll look at this again. Here's the challenge in the embedded opengraph template:

{{- with or .Description .Summary site.Params.description | plainify }}
  <meta property="og:description" content="{{ . }}">
{{- end }}

The input can be plain text, markdown, rendered markdown, HTML, or anything else. If rendered markdown, it might be wrapped in a p element or it might not, depending on how the summary was defined.

@jmooring jmooring self-assigned this Apr 23, 2024
@bep bep removed the NeedsTriage label Apr 23, 2024
@bep bep modified the milestones: v0.125.3, v0.125.4 Apr 23, 2024
@jmooring
Copy link
Member

Going back through issues, PRs, and forum topics, the best that I have come up with for consistent rendering regardless of how the summary is defined (front matter, with summary divider, without summary divider) is:

{{- with or .Description .Summary site.Params.description | .RenderString | plainify | htmlUnescape }}
  <meta property="og:description" content="{{ . }}">
{{- end }}

This handles the following cases the same way:

+++
title = 'Post 1'
summary = "Test <em>line</em> with a **single** quote: can't."
+++
+++
title = 'Post 1'
+++

Test <em>line</em> with a **single** quote: can't.
+++
title = 'Post 1'
+++

Test <em>line</em> with a **single** quote: can't.
<!--more-->

All three produce:

<meta property="og:description" content="Test line with a single quote: can’t.">

If this is acceptable:

  • Do the same thing for og:site_name and og:title
  • Make similar changes to the embedded schema and twitter_cards templates

@chalin
Copy link
Author

chalin commented Apr 23, 2024

Sounds good, thanks @jmooring. The OTel website has descriptions like the following (which I'm hoping to curate at some point to remove the images, but this is what we have now):

description: >-
  <img width="35" class="img-initial" src="/img/logos/32x32/Golang_SDK.svg"
  alt="Go"> A language-specific implementation of OpenTelemetry in Go.

And this (markdown):

description: >-
  _OTel in Practice_ is a _series_ of talks initiated by some members of the End
  User SIG.

Will the solution you propose handle such descriptions as before (or better :)?

/cc @svrnm @theletterf

@jmooring
Copy link
Member

@chalin

With the proposed changes, the first is rendered to:

<meta property="og:description" content=" A language-specific implementation of OpenTelemetry in Go.">

The second is rendered to:

<meta property="og:description" content="OTel in Practice is a series of talks initiated by some members of the End User SIG.">

jmooring added a commit to jmooring/hugo that referenced this issue Apr 24, 2024
- Do not escape HTML entities
- Use consistent formatting for title and description with opengraph,
  schema, and twitter_cards
- Improve readability of twitter_cards

Closes gohugoio#12418
@chalin
Copy link
Author

chalin commented Apr 24, 2024

Thanks @jmooring, that LGTM.

jmooring added a commit to jmooring/hugo that referenced this issue Apr 24, 2024
@jmooring
Copy link
Member

@chalin

In its current implementation, PR #12425 does this:

the first is rendered to:

<meta property="og:description" content=" A language-specific implementation of OpenTelemetry in Go.">

The second is rendered to:

<meta property="og:description" content="_OTel in Practice_ is a _series_ of talks initiated by some members of the End User SIG.">

In your second example, note that the markdown is not rendered to HTML due to performance concerns related to using .RenderString on every og:description on every page.

If you want to render the markdown and don't care about a performance hit, use this in the override template...

{{- with or .Description .Summary site.Params.description | .RenderString | plainify | htmlUnescape | chomp }}

I have no idea how much this would affect a 10,000 page site.

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants