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

Commit

Permalink
Merge pull request #91 from kbenzie/kbenzie/fix_md_links
Browse files Browse the repository at this point in the history
Fix hard error on cross-linking markdown document
  • Loading branch information
ericholscher committed Apr 27, 2018
2 parents 3238b02 + 7762ab3 commit 450909b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
17 changes: 14 additions & 3 deletions recommonmark/parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Docutils CommonMark parser"""

import sys
from os.path import splitext

from docutils import parsers, nodes
from sphinx import addnodes
Expand Down Expand Up @@ -126,7 +127,16 @@ def visit_code(self, mdnode):

def visit_link(self, mdnode):
ref_node = nodes.reference()
ref_node['refuri'] = mdnode.destination
# Check destination is supported for cross-linking and remove extension
destination = mdnode.destination
_, ext = splitext(destination)
# 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:
destination = destination.replace(ext, '')
ref_node['refuri'] = destination
# TODO okay, so this is acutally not always the right line number, but
# these mdnodes won't have sourcepos on them for whatever reason. This
# is better than 0 though.
Expand All @@ -136,11 +146,12 @@ def visit_link(self, mdnode):
ref_node['title'] = mdnode.title
next_node = ref_node

url_check = urlparse(mdnode.destination)
url_check = urlparse(destination)
if not url_check.scheme and not url_check.fragment:
wrap_node = addnodes.pending_xref(
reftarget=mdnode.destination,
reftarget=destination,
reftype='any',
refdomain=None, # Added to enable cross-linking
refexplicit=True,
refwarn=True
)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def test_links(self):
<?xml version="1.0" ?>
<document source="&lt;string&gt;">
<paragraph>
<pending_xref refexplicit="True" reftarget="/foo" reftype="any" refwarn="True">
<pending_xref refdomain="None" refexplicit="True" reftarget="/foo" reftype="any" refwarn="True">
<reference refuri="/foo">link</reference>
</pending_xref>
</paragraph>
Expand All @@ -198,7 +198,7 @@ def test_links(self):
<?xml version="1.0" ?>
<document source="&lt;string&gt;">
<paragraph>
<pending_xref refexplicit="True" reftarget="foo" reftype="any" refwarn="True">
<pending_xref refdomain="None" refexplicit="True" reftarget="foo" reftype="any" refwarn="True">
<reference refuri="foo">link</reference>
</pending_xref>
</paragraph>
Expand Down Expand Up @@ -274,21 +274,21 @@ def test_bullet_list(self):
<bullet_list>
<list_item>
<paragraph>
<pending_xref refexplicit="True" reftarget="/1" reftype="any" refwarn="True">
<pending_xref refdomain="None" refexplicit="True" reftarget="/1" reftype="any" refwarn="True">
<reference refuri="/1">List item 1</reference>
</pending_xref>
</paragraph>
</list_item>
<list_item>
<paragraph>
<pending_xref refexplicit="True" reftarget="/2" reftype="any" refwarn="True">
<pending_xref refdomain="None" refexplicit="True" reftarget="/2" reftype="any" refwarn="True">
<reference refuri="/2">List item 2</reference>
</pending_xref>
</paragraph>
</list_item>
<list_item>
<paragraph>
<pending_xref refexplicit="True" reftarget="/3" reftype="any" refwarn="True">
<pending_xref refdomain="None" refexplicit="True" reftarget="/3" reftype="any" refwarn="True">
<reference refuri="/3">List item 3</reference>
</pending_xref>
</paragraph>
Expand Down

0 comments on commit 450909b

Please sign in to comment.