From 7762ab38887e5977400480c4d1b6eddec1c939ee Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Thu, 2 Nov 2017 15:55:46 +0000 Subject: [PATCH] Fix hard error on cross-linking markdown document Sphinx 1.6.5 with tip recommonmark hard errors when a markdown document contains a cross-reference to another local markdown document as it unconditionally access the `refdomain` of a `pending_xref`. This was fixed [elsewhere](https://github.com/ignatenkobrain/sphinxcontrib-issuetracker/pull/13/files#diff-61beda6e59ec3020dbf0c2826dbb64c4R161) by setting it to `None`. This patch uses the same approach which no longer causes the Sphinx build to fail on cross-linking a markdown document however the generated link is not correctly rendered producing broken links in the generated html. Addressing the broken links, the `mdnode.destination` is stripped of its file extension if that extension is supported by `CommonMarkParser` however this will only work for markdown documents, not ReStructuedText or any other extended file type parsers. Fixes #89. --- recommonmark/parser.py | 17 ++++++++++++++--- tests/test_basic.py | 10 +++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/recommonmark/parser.py b/recommonmark/parser.py index e54fdf4..0c4924b 100644 --- a/recommonmark/parser.py +++ b/recommonmark/parser.py @@ -1,6 +1,7 @@ """Docutils CommonMark parser""" import sys +from os.path import splitext from docutils import parsers, nodes from sphinx import addnodes @@ -126,7 +127,16 @@ class CommonMarkParser(parsers.Parser): 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. @@ -136,11 +146,12 @@ class CommonMarkParser(parsers.Parser): 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 ) diff --git a/tests/test_basic.py b/tests/test_basic.py index 0e703dc..1111c5d 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -183,7 +183,7 @@ class TestParsing(unittest.TestCase): - + link @@ -198,7 +198,7 @@ class TestParsing(unittest.TestCase): - + link @@ -274,21 +274,21 @@ class TestParsing(unittest.TestCase): - + List item 1 - + List item 2 - + List item 3