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

Handle relative image URLs #134

Open
wants to merge 1 commit into
base: develop
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
12 changes: 12 additions & 0 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from textwrap import fill
import re
import six
from urllib.parse import urlparse


convert_heading_re = re.compile(r'convert_h(\d+)')
Expand Down Expand Up @@ -86,6 +87,7 @@ class DefaultOptions:
sup_symbol = ''
wrap = False
wrap_width = 80
base_url = None

class Options(DefaultOptions):
pass
Expand Down Expand Up @@ -295,6 +297,16 @@ def convert_img(self, el, text, convert_as_inline):
src = el.attrs.get('src', None) or ''
title = el.attrs.get('title', None) or ''
title_part = ' "%s"' % title.replace('"', r'\"') if title else ''

if not src.startswith(('http://', 'https://', 'data:')):
if self.options['base_url']:
if src.startswith('/'):
parsed_url = urlparse(self.options['base_url'])
base_path = f"{parsed_url.scheme}://{parsed_url.netloc}"
src = f"{base_path}{src}"
else:
src = f"{self.options['base_url'].rstrip('/')}/{src.lstrip('/')}"

if (convert_as_inline
and el.parent.name not in self.options['keep_inline_images_in']):
return alt
Expand Down
9 changes: 9 additions & 0 deletions tests/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,12 @@ def callback(el):
assert md('<pre class="python">test\n foo\nbar</pre>', code_language_callback=callback) == '\n```python\ntest\n foo\nbar\n```\n'
assert md('<pre class="javascript"><code>test\n foo\nbar</code></pre>', code_language_callback=callback) == '\n```javascript\ntest\n foo\nbar\n```\n'
assert md('<pre class="javascript"><code class="javascript">test\n foo\nbar</code></pre>', code_language_callback=callback) == '\n```javascript\ntest\n foo\nbar\n```\n'

def test_img():
assert md('<img src="/path/to/img.jpg" alt="Alt text" title="Optional title" />') == '![Alt text](/path/to/img.jpg "Optional title")'
assert md('<img src="/path/to/img.jpg" alt="Alt text" />') == '![Alt text](/path/to/img.jpg)'
assert md('<img src="/path/to/img.jpg" alt="Alt text" />', base_url='https://example.com') == '![Alt text](https://example.com/path/to/img.jpg)'
assert md('<img src="/path/to/img.jpg" alt="Alt text" />', base_url='https://example.com/otherpath') == '![Alt text](https://example.com/path/to/img.jpg)'
assert md('<img src="/path/to/img.jpg" alt="Alt text" />', base_url='https://example.com/otherpath/') == '![Alt text](https://example.com/path/to/img.jpg)'
assert md('<img src="path/to/img.jpg" alt="Alt text" />', base_url='https://example.com/anypage') == '![Alt text](https://example.com/anypage/path/to/img.jpg)'
assert md('<img src="path/to/img.jpg" alt="Alt text" />', base_url='https://example.com/anypage/') == '![Alt text](https://example.com/anypage/path/to/img.jpg)'