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

add og:title to the docs #1508

Merged
merged 5 commits into from
Apr 4, 2024
Merged

add og:title to the docs #1508

merged 5 commits into from
Apr 4, 2024

Conversation

czue
Copy link
Contributor

@czue czue commented Mar 26, 2024

This uses the same og:title as the page title.

The hooks are already in the base template so this is very straightforward.

image

Copy link
Sponsor Member

@thibaudcolas thibaudcolas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn’t we need an escape as well to protect from titles with double quotes? Considering the og:title value is an attribute while <title> is a tag.

@czue
Copy link
Contributor Author

czue commented Mar 26, 2024

Oh, good point. Will check and update.

Comment on lines 4 to 5
{% block title %}{{ doc.title|striptags|safe }} | {% trans "Django documentation" %}{% endblock %}

{% block og_title %}{{ doc.title|striptags|safe }} | {% trans "Django documentation" %}{% endblock %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could wrap with {% with %} to reduce the duplication here

Copy link
Contributor Author

@czue czue Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, though it doesn't seem to work. According to ChatGPT this is because "the scope of the variables in Django's {% with %} statement is limited to the block in which they are defined" (I didn't see this mentioned in the docs but it matches the behavior i'm seeing). So since the variables are used inside separate blocks it doesn't work. We could probably pass a variable in the view if you think it's worth looking into that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, given that we also now want to escape the one in the meta tag but not the one in the title, I don't think there's any easy way to avoid the duplication.

@czue
Copy link
Contributor Author

czue commented Mar 26, 2024

@thibaudcolas I added escaping and confirmed it does the right thing
image

Copy link
Member

@bmispelon bmispelon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put together a test for the new behavior (add it to docs/tests.py):

from django.test import RequestFactory
from django.template.loader import render_to_string

class TemplateTestCase(TestCase):
    def _assertOGTitleEqual(self, doc, expected):
        output = render_to_string(
            "docs/doc.html",
            {"doc": doc, "lang": "en", "version": "5.0"},
            request=RequestFactory().get("/")
        )

        self.assertInHTML(f'<meta property="og:title" content="{expected}" />', output)

    def test_opengraph_title(self):
        doc = Document.objects.create(
            release=DocumentRelease.objects.create(
                lang='en',
                release=Release.objects.create(version="5.0"),
            ),
        )
        doc.body = "test body"  # avoids trying to load the underlying physical file

        for title, expected in [
            ("test title", "test title"),
            ("test & title", "test &amp; title"),
            ("test <strong>title</strong>", "test title"),
        ]:
            doc.title = title
            with self.subTest(title=title):
                self._assertOGTitleEqual(doc, f"{expected} | Django documentation")

@@ -2,6 +2,7 @@
{% load i18n fundraising_extras docs %}

{% block title %}{{ doc.title|striptags|safe }} | {% trans "Django documentation" %}{% endblock %}
{% block og_title %}{{ doc.title|striptags|escape|safe }} | {% trans "Django documentation" %}{% endblock %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the |safe is needed here, since |escape marks its output as safe as far as I know

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call: 7545e3c

Also, based on the test even the |escape wasn't necessary (maybe because autoescape is on?). But I left it since it seemed more explicit. But let me know if you think worth removing

Copy link
Contributor Author

@czue czue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@beingbiplov thank you for the test code! made the suggested changes.

@@ -2,6 +2,7 @@
{% load i18n fundraising_extras docs %}

{% block title %}{{ doc.title|striptags|safe }} | {% trans "Django documentation" %}{% endblock %}
{% block og_title %}{{ doc.title|striptags|escape|safe }} | {% trans "Django documentation" %}{% endblock %}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call: 7545e3c

Also, based on the test even the |escape wasn't necessary (maybe because autoescape is on?). But I left it since it seemed more explicit. But let me know if you think worth removing

@bmispelon
Copy link
Member

Also, based on the test even the |escape wasn't necessary (maybe because autoescape is on?). But I left it since it seemed more explicit. But let me know if you think worth removing

Oh good point, I didn't think about autoescape. It doesn't make much difference in the end, but I think I'd personally prefer to remove the |escape if it's not actually doing anything. As long as we have a test, it should break and let us know in the future if we somehow turn off autoescape.

I don't seem to have the right permissions to allow the github actions to run on this PR. Can someone from @django/djangoproject-com-maintainters approve it?

@czue
Copy link
Contributor Author

czue commented Mar 27, 2024

@bmispelon good point on tests. I removed the redundant escape

Copy link
Member

@bmispelon bmispelon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks good to me, thanks again for taking the time ✨

for title, expected in [
("test title", "test title"),
("test & title", "test &amp; title"),
('test "title"', "test &quot;title&quot;"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good thinking on this one too 👍🏻

@czue
Copy link
Contributor Author

czue commented Apr 4, 2024

Just confirming there are no other steps/blockers on my side before this is merged? (First time contributing so don't know the process).

BTW I had a few other suggestions based on setting up my dev environment (#1507 and #1497). Not sure if there is a process for getting those looked at.

@knyghty
Copy link
Member

knyghty commented Apr 4, 2024

Just confirming there are no other steps/blockers on my side before this is merged? (First time contributing so don't know the process).

I don't think so, just a bit of patience, the website team are all volunteers and have other, more paid things to do.

@camilonova camilonova merged commit 6bbb8a8 into django:main Apr 4, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants