Skip to content

Commit

Permalink
Convert relative paths to absolute in og:image. (#48)
Browse files Browse the repository at this point in the history
Always using absolute/full URLs for og:image as long as
`config["ogp_site_url"]` is set.

Fixes #43
  • Loading branch information
Robpol86 authored Nov 20, 2021
1 parent e20f753 commit 54ea5eb
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 4 deletions.
7 changes: 5 additions & 2 deletions sphinxext/opengraph/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from typing import Any, Dict
from urllib.parse import urljoin, urlparse, urlunparse
from pathlib import Path, PosixPath
from pathlib import Path

import docutils.nodes as nodes
from sphinx.application import Sphinx
from sphinx.util import logger

from .descriptionparser import get_description
from .titleparser import get_title
Expand Down Expand Up @@ -111,6 +110,10 @@ def get_tags(
ogp_image_alt = first_image.get("alt", None)

if image_url:
image_url_parsed = urlparse(image_url)
if not image_url_parsed.scheme:
# Relative image path detected. Make absolute.
image_url = urljoin(config["ogp_site_url"], image_url_parsed.path)
tags += make_tag("og:image", image_url)

# Add image alt text (either provided by config or from site_name)
Expand Down
16 changes: 14 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ def content(app):
yield app


def _meta_tags(content):
c = (content.outdir / "index.html").read_text()
def _meta_tags(content, subdir=None):
if subdir is None:
c = (content.outdir / "index.html").read_text()
else:
c = (content.outdir / subdir / "index.html").read_text()
return BeautifulSoup(c, "html.parser").find_all("meta")


Expand All @@ -42,5 +45,14 @@ def og_meta_tags(content):
]


@pytest.fixture()
def og_meta_tags_sub(content):
return [
tag
for tag in _meta_tags(content, "sub")
if tag.get("property", "").startswith("og:")
]


def pytest_configure(config):
config.addinivalue_line("markers", "sphinx")
10 changes: 10 additions & 0 deletions tests/roots/test-image-rel-paths/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extensions = ["sphinxext.opengraph"]

master_doc = "index"
exclude_patterns = ["_build"]

html_theme = "basic"

ogp_site_name = "Example's Docs!"
ogp_site_url = "http://example.org/"
ogp_use_first_image = True
Binary file added tests/roots/test-image-rel-paths/img/sample.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions tests/roots/test-image-rel-paths/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.. image:: img/sample.jpg
:alt: Test image alt text
6 changes: 6 additions & 0 deletions tests/roots/test-image-rel-paths/sub/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
========
Sub Page
========

.. image:: ../img/sample.jpg
:alt: Test image alt text
12 changes: 12 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ def test_image(og_meta_tags):
assert get_tag_content(og_meta_tags, "image") == "http://example.org/image.png"


@pytest.mark.sphinx("html", testroot="image-rel-paths")
def test_image_rel_paths(og_meta_tags, og_meta_tags_sub):
assert (
get_tag_content(og_meta_tags, "image")
== "http://example.org/_images/sample.jpg"
)
assert (
get_tag_content(og_meta_tags_sub, "image")
== "http://example.org/_images/sample.jpg"
)


@pytest.mark.sphinx("html", testroot="image")
def test_image_alt(og_meta_tags):
assert get_tag_content(og_meta_tags, "image:alt") == "Example's Docs!"
Expand Down

0 comments on commit 54ea5eb

Please sign in to comment.