Skip to content

Anchor Linking with Heading Sections #744

Closed

Description

@brianjking

Hello,

I'm trying to setup MkDocs so that headers will automatically (or manually) receive anchor based link support for navigation purposes using the ReadTheDocs theme.

I see it working on the MkDocs website at http://www.mkdocs.org/#preview-your-site-as-you-work, however, I'm having trouble making this work on my site.

Am I missing something?

Activity

  1. waylan commented on Oct 22, 2015

    @waylan
    Contributor

    That feature is enabled by the permalink option of the TOC extension to Markdown. You can see it enabled in the config file:

    markdown_extensions:
        - toc:
            permalink: 
  2. brianjking commented on Oct 25, 2015

    @brianjking
    ContributorAuthor

    @waylan -- Thanks. How about when I'm trying to link from say a single word of a file down to a section of the same file? Alternatively, when I'm attempting to link from a word of a file to a section of another file?

  3. waylan commented on Oct 28, 2015

    @waylan
    Contributor

    @brianjking if you have the permalink option of the toc extension enabled, then every header will be assigned an id. Simply use that id to link to it. This is basic HTML linking techniques. Nothing unique to MkDocs or Markdown.

    For example, the following header:

    # A Header
    

    Will produce the following HTML:

    <h1 id="a-header">A Header</h1>
    

    Note that the id is assigned the slug: a-header.

    1. From the same page, use the id (slug) only as the anchor: [link](#a-header).
    2. From a different page, use the path and anchor: [link](/path/to/page.html#a-header)
    3. From a different domain, use the full URL and anchor: [link](http://example.com/path/to/page.html#a-header)
  4. brianjking commented on Oct 29, 2015

    @brianjking
    ContributorAuthor

    Thanks!

  5. noel-yap commented on Feb 3, 2017

    @noel-yap

    I'd like to be able to do this from mkdocs.yml but mkdocs adds a / to the end of the link so that something like - Item: 'ANOTHER_PAGE#anchor' creates a link for ANOTHER_PAGE#anchor/ and the anchor doesn't work. Any advice?

  6. waylan commented on Feb 4, 2017

    @waylan
    Contributor

    @noel-yap are you listing - Item: 'ANOTHER_PAGE#anchor' as an item in your pages config in the mkdocs.yml file? If so, that won't work. You can only point nav items at pages, not subsections of a page.

  7. noel-yap commented on Feb 4, 2017

    @noel-yap
  8. sesong11 commented on Aug 24, 2018

    @sesong11

    Is it possible to change auto generated link?
    e.g ## My Header generated #my-header
    how can I change this auto generated to #my-customer-link-to-header.

  9. waylan commented on Aug 24, 2018

    @waylan
    Contributor

    @sesong11 if you enable the attr-list extension, that will allow you to define custom ids for your headers. Those ids will then be used rather than the auto-generated ones.

  10. jvleminc commented on Aug 21, 2022

    @jvleminc

    Can/should mkdocs be changed so this would work? All that really needs to
    happen is for it not to append / to the nav link.

    By any chance are there any updates on this?

  11. added a commit that references this issue on Apr 7, 2023
  12. pereorga commented on May 19, 2023

    @pereorga

    @jvleminc I created a follow-up issue (#3223), as this one was closed a long time ago.

  13. petre-renware commented on Jul 23, 2023

    @petre-renware

    another solution that works is to link directly to html. The "background" is: when mkdocs parse yaml file to get html pages where to go, first is looking if specified link finish with a markdown known extension (ie, like .md). If NOT take string as is and put it in a href attribute. So if you specify in nav something like entry name = 'your_page.html#desired_section' will happen what you want... Please take a look at generated URL. Have a wonderful day.

  14. pereorga commented on Jul 23, 2023

    @pereorga

    @petre-renware this workaround is problematic on relative paths appearing in multiple places, or on anything that requires MkDocs altering the URL (such as when using the use_directory_urls setting).

  15. petre-renware commented on Jul 23, 2023

    @petre-renware

    @petre-renware this workaround is problematic on relative paths appearing in multiple places, or on anything that requires MkDocs altering the URL (such as when using the use_directory_urls setting).

    yes, you have right, but it solves the problem for the moment and gives you time to find a better solution... Another workaround that is possible is to make directories (with html extension ! - take a look at pdf maker but that with js included to render mermaid diagrams) that have simple index.html inside that redirects to what you want. But this workaround will be also hard to maintain for large data and link volumes... I'm supposind that what are doing exceed the level of a static portal... and should be a dynamic one with an active application server running some code... What I did in such cases was to use mkdocs for UI (for simplicity of using instead of HTML and with more Jinja variables) and Flask to render at run time the mkdocs static pages. In this case mkdocs is run only when change tge interface and "fixed, static, top" navigation and all dynamic part is solved at run time from wsgi server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions