question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MyST does not link to anchors within non-auto-generated markdown or HTML files

See original GitHub issue

Describe the bug

context When I do this:


Works: [b](c.html)
Does not work: [b](c.html#foo)

expectation I expected two links.

bug But instead only one link is created.

$ sphinx-build docs out

...
/private/tmp/mysttest/docs/index.md:3: WARNING: 'myst' reference target not found: c.html#foo
...

problem This is a problem for people who need to link to anchors inside of HTML files.

Reproduce the bug

 CCI-39 ▶ tmp ❯ mysttest ▶ 12 ▶ % ▶ cat docs/index.md                 
# AAAAA

Works: [b](c.html)

Does not work: [b2](c.html#foo)
 CCI-39 ▶ tmp ❯ mysttest ▶ 12 ▶ % ▶ cat docs/conf.py                  
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = [
    "sphinx.ext.autodoc",
    "sphinx.ext.viewcode",
    "sphinx.ext.autosectionlabel",
    "myst_parser"  # ,
    #    "recommonmark"
]
 CCI-39 ▶ tmp ❯ mysttest ▶ 12 ▶ % ▶ cat docs/c.html                   
<a id="foo">Foo!</a>
 CCI-39 ▶ tmp ❯ mysttest ▶ 12 ▶ % ▶ rm -rf out ; sphinx-build docs out
Running Sphinx v4.5.0
making output directory... done
myst v0.17.1: MdParserConfig(commonmark_only=False, gfm_only=False, enable_extensions=[], linkify_fuzzy_links=True, dmath_allow_labels=True, dmath_allow_space=True, dmath_allow_digits=True, dmath_double_inline=False, update_mathjax=True, mathjax_classes='tex2jax_process|mathjax_process|math|output_area', disable_syntax=[], all_links_external=False, url_schemes=['http', 'https', 'mailto', 'ftp'], ref_domains=None, highlight_code_blocks=True, number_code_blocks=[], title_to_header=False, heading_anchors=None, heading_slug_func=None, html_meta=[], footnote_transition=True, substitutions=[], sub_delimiters=['{', '}'], words_per_minute=200)
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 1 source files that are out of date
updating environment: [new config] 1 added, 0 changed, 0 removed
reading sources... [100%] index                                                                                      
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] index                                                                                       
/private/tmp/mysttest/docs/index.md:5: WARNING: 'myst' reference target not found: c.html#foo
generating indices... genindex done
writing additional pages... search done
copying downloadable files... [100%] c.html                                                                          
copying static files... done
copying extra files... done
dumping search index in English (code: en)... done
dumping object inventory... done
build succeeded, 1 warning.

The HTML pages are in out.

List your environment

sphinx-build --version            
sphinx-build 4.5.0

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:8
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
choldgrafcommented, Jun 13, 2022

I believe that the anchors linking only works for header anchors that are auto-generated. If you link to a header that wasn’t auto-generated then MyST won’t find it. At least that was the behavior that @nthiery and I ran into when we tried to reproduce this (he mentioned the same problem)

Perhaps it would be possible for this functionality to check whether a # is present in a markdown link, and if so, then somehow:

  • Remove it from the link before references are resolved
  • Store it somehow
  • At build time, re-insert the # link

Not sure how feasible that is though

1reaction
chrisjsewellcommented, Aug 23, 2022

I think there is some confusion is some of the later comments, conflating the use of .html and .md extensions: myst_heading_anchors only work for .md extensions, i.e. [text](page.md#something) will link to a heading # something (or # Something, etc) on page.md, providing at least myst_heading_anchors=1

For:

Works: [b](c.html)
Does not work: [b](c.html#foo)

linking to built html documents is not recommended, since it in essence goes against the output format agnostic nature of sphinx, e.g. it would not work if you tried to build a LaTeX PDF etc

myst-parser provides a number of ways to make the links output format agnostic: https://myst-parser.readthedocs.io/en/latest/syntax/syntax.html#markdown-links-and-referencing

In this case, one should ideally be linking to the actual Markdown documents, i.e. using the .md format:

[b](c.md)
[b](c.md#foo)

If you specifically only want markdown style links to output “external hrefs”, then you can use:

myst_all_links_external = True

this will simply output every text link as e.g. text`, without any “smart” referencing

Read more comments on GitHub >

github_iconTop Results From Across the Web

Myst handles HTML anchors in headings in an unexpected way
Describe the bug context We have documents which have a structure very similar to the C++ Core Guidelines and use HTML anchors in...
Read more >
Syntax Extensions - MyST-Parser
Auto-generated header anchors​​ The MyST Parser can automatically generate label “slugs” for header anchors so that you can reference them from markdown links. ......
Read more >
Anchors within the same page not working? - Stack Overflow
It's important. Anchors will not work on all pages, have tag <base> in head (but root page), if href of anchor is page-relative...
Read more >
Anchors in Markdown documents not working
When clicking andhor links in markdown files from the source code viewer, it simply refreshes the page and does not go to the...
Read more >
Using MyST and trying to link from an .rst file to an .md file
I have been using MyST to combine markdown files with Sphinx files, ... that rst tab in the example mentioned above, but the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found