You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
Reacted by Simon Jakobi, Martin Zurowietz, Andy Shapiro, Yaroslav Halchenko, Lee Robinson, Brian L. Chen, Michael Reichenbach, Shen Yang, Oleg Nenashev, ajay and 5 more
@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?
@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.
From the same page, use the id (slug) only as the anchor: [link](#a-header).
From a different page, use the path and anchor: [link](/path/to/page.html#a-header)
From a different domain, use the full URL and anchor: [link](http://example.com/path/to/page.html#a-header)
Reacted by Jeremy Kao, Jeremie Abiteboul, Jendrik Seipp, Beatriz Martinez Gonzalez, Wellington Miranda, Paul Cartee, Evan Sneath, George Markhulia, Jacek Laskowski, Jeroen and 2 more
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?
@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.
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.
@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.
Reacted by sesong11, Otto, Tobias Kaupat, Thomas Fel, Oleh Prypin, Artem Rys, Jacek Laskowski and hello_wac
Reacted by sesong11, Jeremy Kao, Davor Kus, Murad Biashimov and hello_wac
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.
@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).
@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.
Activity
waylan commentedon Oct 22, 2015
That feature is enabled by the permalink option of the TOC extension to Markdown. You can see it enabled in the config file:
brianjking commentedon Oct 25, 2015
@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?
waylan commentedon Oct 28, 2015
@brianjking if you have the
permalinkoption of thetocextension enabled, then every header will be assigned anid. Simply use thatidto link to it. This is basic HTML linking techniques. Nothing unique to MkDocs or Markdown.For example, the following header:
Will produce the following HTML:
Note that the
idis assigned the slug:a-header.[link](#a-header).[link](/path/to/page.html#a-header)[link](http://example.com/path/to/page.html#a-header)brianjking commentedon Oct 29, 2015
Thanks!
noel-yap commentedon Feb 3, 2017
I'd like to be able to do this from
mkdocs.ymlbutmkdocsadds a/to the end of the link so that something like- Item: 'ANOTHER_PAGE#anchor'creates a link forANOTHER_PAGE#anchor/and the anchor doesn't work. Any advice?waylan commentedon Feb 4, 2017
@noel-yap are you listing
- Item: 'ANOTHER_PAGE#anchor'as an item in yourpagesconfig in themkdocs.ymlfile? If so, that won't work. You can only point nav items at pages, not subsections of a page.noel-yap commentedon Feb 4, 2017
sesong11 commentedon Aug 24, 2018
Is it possible to change auto generated link?
e.g
## My Headergenerated#my-headerhow can I change this auto generated to
#my-customer-link-to-header.waylan commentedon Aug 24, 2018
@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.
jvleminc commentedon Aug 21, 2022
By any chance are there any updates on this?
Try adding toc permalinks
pereorga commentedon May 19, 2023
@jvleminc I created a follow-up issue (#3223), as this one was closed a long time ago.
petre-renware commentedon Jul 23, 2023
another solution that works is to link directly to html. The "background" is: when
mkdocsparse 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 ahrefattribute. So if you specify in nav something likeentry name = 'your_page.html#desired_section'will happen what you want... Please take a look at generated URL. Have a wonderful day.pereorga commentedon Jul 23, 2023
@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).
petre-renware commentedon Jul 23, 2023
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
mermaiddiagrams) that have simpleindex.htmlinside 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 usemkdocsfor UI (for simplicity of using instead of HTML and with more Jinja variables) and Flask to render at run time themkdocsstatic pages. In this casemkdocsis run only when change tge interface and "fixed, static, top" navigation and all dynamic part is solved at run time fromwsgiserver.