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

Convert relative paths to absolute in og:image. #48

Merged
merged 1 commit into from
Nov 20, 2021
Merged
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
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