diff --git a/README.md b/README.md index 67feab2..e1e4214 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,41 @@ This allows you to write both `.md` and `.rst` files inside of the same project. For all links in commonmark that aren't explicit URLs, they are treated as cross references with the [`:any:`](http://www.sphinx-doc.org/en/stable/markup/inline.html#role-any) role. This allows referencing a lot of things including files, labels, and even objects in the loaded domain. +#### Linking to headings in other files + +For linking to headings in other files you can use the [`autosectionlabel`][] sphinx feature, e.g. + +```python +# conf.py + +extensions = [ + # Auto-generate section labels. + 'sphinx.ext.autosectionlabel', +] + +# Prefix document path to section labels, otherwise autogenerated labels would look like 'heading' +# rather than 'path/to/file:heading' +autosectionlabel_prefix_document = True +``` + +You would use it like: + +```markdown + + +# Title + +## My Subtitle +``` + +```markdown + + +[My Subtitle][] + +[My Subtitle]: +``` + ### AutoStructify AutoStructify makes it possible to write your documentation in Markdown, and automatically convert this @@ -118,3 +153,4 @@ and is now maintained in the Read the Docs (rtfd) GitHub organization. [dc]: http://docutils.sourceforge.net/docs/ref/doctree.html [sphinx-issue]: https://bitbucket.org/birkenfeld/sphinx/issue/825/markdown-capable-sphinx [so-question]: http://stackoverflow.com/questions/2471804/using-sphinx-with-markdown-instead-of-rst +[`autosectionlabel`]: https://www.sphinx-doc.org/en/master/usage/extensions/autosectionlabel.html diff --git a/recommonmark/parser.py b/recommonmark/parser.py index 857aded..89b2b6c 100644 --- a/recommonmark/parser.py +++ b/recommonmark/parser.py @@ -11,9 +11,9 @@ from commonmark import Parser from warnings import warn if sys.version_info < (3, 0): - from urlparse import urlparse + from urlparse import urlparse, unquote else: - from urllib.parse import urlparse + from urllib.parse import urlparse, unquote __all__ = ['CommonMarkParser'] @@ -152,7 +152,7 @@ class CommonMarkParser(parsers.Parser): url_check = urlparse(destination) if not url_check.scheme and not url_check.fragment: wrap_node = addnodes.pending_xref( - reftarget=destination, + reftarget=unquote(destination), reftype='any', refdomain=None, # Added to enable cross-linking refexplicit=True, diff --git a/tests/sphinx_xref/conf.py b/tests/sphinx_xref/conf.py index 6b0140e..443a026 100644 --- a/tests/sphinx_xref/conf.py +++ b/tests/sphinx_xref/conf.py @@ -3,6 +3,9 @@ from recommonmark.parser import CommonMarkParser +extensions = 'sphinx.ext.autosectionlabel'] +autosectionlabel_prefix_document = True + templates_path = ['_templates'] source_suffix = '.md' source_parsers = { '.md': CommonMarkParser } diff --git a/tests/sphinx_xref/index.md b/tests/sphinx_xref/index.md index a41ebfa..ae1cf55 100644 --- a/tests/sphinx_xref/index.md +++ b/tests/sphinx_xref/index.md @@ -3,3 +3,7 @@ Header A paragraph [link](link) and [absolute link](/link). An [external link](http://www.google.com). +A [ref with spaces][]. + + +[ref with spaces]: diff --git a/tests/sphinx_xref/link.md b/tests/sphinx_xref/link.md index a3fbc9c..e2beb4d 100644 --- a/tests/sphinx_xref/link.md +++ b/tests/sphinx_xref/link.md @@ -2,3 +2,7 @@ link ==== The link file. + +## Section 1 + +Autosectionlabel will generate a link for this heading at `link:Section 1`.