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

docs: add opengraph metadata tags #860

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/860.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `OpenGraph <https://ogp.me/>`_ tags to all pages.
1 change: 1 addition & 0 deletions docs/_templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{{- metatags }}
<title>{{ title|striptags|e }}{{ titlesuffix }}</title>
{%- block extrahead %} {% endblock %}
<link rel="stylesheet" href="{{ pathto('_static/icons.css', 1)|e }}" type="text/css" />
Expand Down
11 changes: 11 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"sphinxcontrib.towncrier.ext",
"hoverxref.extension",
"notfound.extension",
"sphinxext.opengraph",
"redirects",
"fulltoc",
"exception_hierarchy",
Expand Down Expand Up @@ -276,6 +277,16 @@ def linkcode_resolve(domain: str, info: Dict[str, Any]) -> Optional[str]:
html_use_opensearch = html_baseurl.rstrip("/")


# ogp_site_url = "" # automatically set on readthedocs
ogp_site_name = "disnake documentation"
ogp_image = "https://disnake.dev/assets/disnake-logo-transparent.png"
ogp_image_alt = "disnake icon"
ogp_custom_meta_tags = [
'<meta property="og:image:width" content="64" />',
'<meta property="og:image:height" content="64" />',
]


# -- Options for HTML output ----------------------------------------------

html_experimental_html5_writer = True
Expand Down
20 changes: 19 additions & 1 deletion docs/extensions/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import inspect
from typing import TYPE_CHECKING, Any

from docutils import nodes
from sphinx.environment.adapters.indexentries import IndexEntries
from sphinxext.opengraph.descriptionparser import DescriptionParser

if TYPE_CHECKING:
from _types import SphinxExtensionMeta
from docutils import nodes
from sphinx.application import Sphinx
from sphinx.config import Config
from sphinx.writers.html5 import HTML5Translator
Expand Down Expand Up @@ -57,6 +58,22 @@ def patch_genindex(*args: Any) -> None:
IndexEntries.create_index = new_create_index # type: ignore


def patch_opengraph(*args: Any) -> None:
# Patch sphinxext-opengraph's description parser to only take the first section
# into account, resulting in cleaner descriptions.
orig = DescriptionParser.dispatch_visit

def patched_dispatch_visit(self, node: nodes.Element) -> None:
if isinstance(node, nodes.section):
if getattr(self, "section_found", False):
# stop when encountering nested section
raise nodes.StopTraversal
self.section_found = True
return orig(self, node)

DescriptionParser.dispatch_visit = patched_dispatch_visit


def disable_mathjax(app: Sphinx, config: Config) -> None:
# prevent installation of mathjax script, which gets installed due to
# https://github.com/readthedocs/sphinx-hoverxref/commit/7c4655092c482bd414b1816bdb4f393da117062a
Expand All @@ -71,6 +88,7 @@ def disable_mathjax(app: Sphinx, config: Config) -> None:

def setup(app: Sphinx) -> SphinxExtensionMeta:
app.connect("config-inited", patch_genindex)
app.connect("config-inited", patch_opengraph)
app.connect("config-inited", disable_mathjax)
app.connect("builder-inited", set_translator)

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ docs = [
"sphinxcontrib-towncrier==0.3.2a0",
"towncrier==23.6.0",
"sphinx-notfound-page==0.8.3",
"sphinxext-opengraph==0.7.5"
]
discord = ["discord-disnake"]

Expand Down