Skip to content
This repository has been archived by the owner on Mar 25, 2022. It is now read-only.

Bugfix: External links to 'supported' files lose their file extension #181

Merged
merged 2 commits into from
May 19, 2020
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
21 changes: 11 additions & 10 deletions recommonmark/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,19 @@ def visit_link(self, mdnode):
# Check destination is supported for cross-linking and remove extension
destination = mdnode.destination
_, ext = splitext(destination)

# Check if the destination starts with a url scheme, since internal and
# external links need to be handled differently.
url_check = urlparse(destination)
known_url_schemes = self.config.get('known_url_schemes')
if known_url_schemes:
scheme_known = url_check.scheme in known_url_schemes
else:
scheme_known = bool(url_check.scheme)

# TODO check for other supported extensions, such as those specified in
# the Sphinx conf.py file but how to access this information?
# TODO this should probably only remove the extension for local paths,
# i.e. not uri's starting with http or other external prefix.
if ext.replace('.', '') in self.supported:
if not scheme_known and ext.replace('.', '') in self.supported:
destination = destination.replace(ext, '')
ref_node['refuri'] = destination
# TODO okay, so this is acutally not always the right line number, but
Expand All @@ -162,16 +170,9 @@ def visit_link(self, mdnode):
ref_node['title'] = mdnode.title
next_node = ref_node

url_check = urlparse(destination)
# If there's not a url scheme (e.g. 'https' for 'https:...' links),
# or there is a scheme but it's not in the list of known_url_schemes,
# then assume it's a cross-reference and pass it to Sphinx as an `:any:` ref.
known_url_schemes = self.config.get('known_url_schemes')
if known_url_schemes:
scheme_known = url_check.scheme in known_url_schemes
else:
scheme_known = bool(url_check.scheme)

if not url_check.fragment and not scheme_known:
wrap_node = addnodes.pending_xref(
reftarget=unquote(destination),
Expand Down
2 changes: 2 additions & 0 deletions tests/sphinx_generic/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ This is a [pending ref](index)

[example]: http://example.com/foobar "Example"

External link to Markdown file: [Readme](https://github.com/readthedocs/recommonmark/blob/master/README.md).

Foo

----
Expand Down
6 changes: 6 additions & 0 deletions tests/test_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ def test_links(self):
'</a>'),
output
)
self.assertIn(
('External link to Markdown file: '
'<a class="reference external" '
'href="https://github.com/readthedocs/recommonmark/blob/master/README.md">Readme</a>.'),
output
)

def test_image(self):
output = self.read_file('index.html')
Expand Down