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
On particularly large projects defining the full page configuration is really cumbersome. However, without it, you have no control over the order.
I wonder if it would be worth allowing some sort of glob syntax, this would allow you to sort the categories but then not worry about the sub pages. So, in the case of the MkDocs config, it might look something like this:
This obviously only provides limited control, but I think sorting the categories is often more important than sorting the sub pages. At the moment this is just an idea, it might be that we don't even want to solve this directly in MkDocs, the linked example is something of an extreme case for us.
Open to any other ideas, the only others that I can think of are really ugly.
Reacted by Ed Morley, Kyle Barron, Jacek, Benjamin Rösner, devakker, Lucas Ramage, Francesco Maida, Artur Geraschenko, Shimizu, Yuko, Raymond Berger and 93 more
Reacted by ⁂ Tommi, alexzanderr, dario curreri and Pedro Brochado
changed the title [-]Alternative ways to define the page order if the navigation[/-][+]Alternative ways to define the page order in the navigation[/+]on Aug 15, 2015
I'd recommend an alternative approach: prepending a number to the file and folder names to indicate where they should be sorted. The numbers should then be excluded from the generation.
This way, a user can easily apply sorting where needed, without editing configuration files.
For example, a user could create three folders called
1. Zebras
3. Elephants
2. Ants
and the navigation would then be
Zebras
Ants
Elephants
This should extend to files as well.
The MkDocs config would probably need a tag to disable this behaviour in case a user wants to have the numbers. Also, this behaviour should obviously only apply to files and folders that don't already have a specific order set in the config.
About the implementation:
Implementation could be quite easy.
If in a given directory all entries match the regex ^\d+\. ?.* (starting with a number, a dot and an optional whitespace), the new sorting would be applied.
The existing alphabetical sorting would be replaced with a natural sorting algorithm such as natsort.
Then, from every entry, only regex (?:^\d+\. ?)(.*) (the rest of the name) would be extracted as the name.
Reacted by Peter Ivanics, Philippe Ivaldi, Josh Beard, Joram van den Boezem, cesargomezvela, Dimitris Tsimpidis, Hugo Bento, Sam Kazemian, Jacek, devakker and 26 more
Reacted by Phúc Lương, Francois Botha, Erik Johnson, guiferviz and B. Oskam
+1 to the original proposal. If it can be implemented cleanly with no weird side effects, I expect it would be a widely used feature.
However, I am very much opposed to the proposal to allow a sort order to be defined by prepending a number to the file names. If a user wants that amount of control, then they can use the config file and list every file (regardless of how long that list is).
One final comment regarding sorting a long list of files: When/if a Plugin API (#206) is introduced, it is very likely that a hook could be used to provide any type of user defined sorting that is desired. The whole "sort by a filename prefix, then remove the prefix" sorting thing sounds like a great candidate for a third party plugin.
"That amount of control" in the meaning of "being able to define the order" is for a user who needs this to work just a basic, needed function.
Sorting by numbers would make it as intuitive as sorting things in real life: by placing them directly in the order we want them to be.
Sorting by number prefixes on filenames is not necessarily easier. If I have 50 documents in a directory, each prefixed with a number in order and I want to insert a new document between numbers 5 and 6, I will have to edit every single filename for files 6 through 50 (45 filenames to edit without accidentally overwriting any existing files). Creating the list in the config would have been easier.
My point is that this is very much a special purpose edge case that will only be needed by a few users. Those few users should be able to get whatever sorting they want through a third party plugin. That way, the sorting fits each users exact needs and it not an almost good enough solution for most users.
@d0ugal's original proposal however, is something that will likely be useful to a large number of users and I think that makes sense as a default included feature.
Yes, you're right about that. However, this sorting would not be forced on the users. By default, files would be sorted as they are now. Only if the numbers are present, the natural sort would be applied. By this opt-in way, it would add functionality without interfering with how MkDocs works now.
Both of these proposals do not exclude each other. They do however address two different user bases, both of which are relevant to MkDocs:
the ones who just want to write their documentation as quickly as possible, maybe just have three to four files in a folder and thus prefer filename-based sorting (also, editing configuration files might not be an easy task for them)
and the ones who maintain large project documentations with a lot of files, thus needing and preferring the sorting by declaration in the config file.
As both user bases could be adressed by including both techniques, probably more users would be attracted to MkDocs.
@rathboma this was assigned the Post 1.0 milestone, so I don't expect any work to be done on this until after version 1.0 is released. We are currently working on version 0.16.
I just wanted to check in on this now that Mkdocs is 1.0. I assume that you still want this to be implemented with a Mkdocs plugin? I may look into the API and see how easy it would be to do this.
I have a use case that I'm not sure can be solved using the awesome-pages plugin.
I'd like to recursively include all markdown files within the docs directory (which can be achieved by just not including the nav field in the mkdocs.yml, or using the awesome-pages plugin), but I'd also like to have a menu item link to a specific page (e.g. - Doxygen: "doxygen/html/index.html")
My use case is using mkdocs with Doxygen. I'd like to have a menu item that takes a user directly to the Doxygen page (index.html), but since it's an html, the awesome-pages plugin has no way to link to it. The nav field can be used, but if I use the nav field, I lose the implicit recursive functionality.
Does anything exist that can allow a user to specify menu items AND recursively include markdown files? Has anyone else used Doxygen with Mkdocs working around this limitation?
I should mention, to work around it I just have a .md file that contains nothing but a link to the Doxygen index.html. This works fine, I just think it'd be nice to be able to link directly to the page from the menu, as well as include all markdown files recursively, instead of either/or.
@grandams if I have used this script, will it be help me to add any md file to automatically to mkdocs.yml. actually I am trying to create md files in docs folder, those should be automatically add to mkdocs.yml. is it possible with this script ?
Globbing and "hidden" feature should be implemented as builtins. I think one of the most important features of a documentation generator is the ability to organize files easily and flexibly, especially for big projects. Many writers prefer Markdown over RST may choose to use mkdocs, but lacking features similar to Sphinx's toctree directive is sometimes really frustrating.
The awesome-pages plugin can do some tricks, however it requires a .page file in each involved directory, which is not in line with the concept of managing everything in a single mkdocs.yml file.
BTW, the hidden feature means some files are not listed in the navigation while still get built into the site. For a big project with substantial files, we sometimes would rather use several index pages for navigation than have a long long list of nav entries, thus only index pages need show up in the nav list. This requires the globbing to load all files and the hidden feature to filter out whatever we don't want to see in the nav list.
@idnsunset Actually you can use globbing with awesome-pages in a single mkdocs.yml file (see README). If you need more help or believe you have a use case which is not currently supported, feel free to create an issue (or update the one you opened).
Reacted by Brennan Kinney, Steffen Schroeder, memeplex, Nathon Fowlie, Dom H and roark
Also got struck with this and thought BasePlugin.on_nav seemed like the right place to look. But generating new navigation elements remained a challenge..
defon_nav(self, nav, config, files):
importmkdocs.structure.navasnav_foriteminnav.items:
ifitem.title=="Release History":
children= []
forindex, (version, _) inenumerate(get_releases()):
url="releases/%s/"%versionlink=nav_.Link(version, url) # Is this right?link.is_page=True# Are these necessary?link.is_link=Truechildren.append(link)
item.children[:] =childrenreturnnav
The end result was navigation successfully appearing, but struck with oddities like the navigation in the footer not appearing, to navigate between "next" and "previous" pages. And for sections, like this one, not remaining open or indicating that it is currently opened.
Instead, I had greater success with on_config which let me process the mkdocs.yml file directly. Success!
defon_config(self, config):
# config is a dict, with a `"nav"` key similar to mkdocs.yml# One of the sections (in this case) is called "Release History"# Containing a list of sub-entries.section=next(
entryforentryinconfig["nav"]
if"Release History"inentry
)["Release History"]
# Start from scratchsection[:] = []
forversion, fnameinget_releases():
section.append({version: fname})
returnconfig
Hello, I got the same need, so I make plugin. Its' a simple plugin than check for dir in nav arbo and, if find it, expand it in nav config.
The change are made only during build / serve, nothing will be change in you mkdocs.yaml file.
Check the readme, it don't do more than reply to this thread (include directory in nav). If you need more, awesome-pages seem to can do many more think.
Just connecting some dots here: the awesome-pages plugin does a good job at controlling the navigation, without explicitly listing everything in nav or pages objects. It solves my issue to order some pages while leaving the navigation being automatically determined. The plugin system is truly powerful!
Thank you so much @ojacques!! (and to @lukasgeiter for creating the plugin!) This has solved precisely the issue I'm having - I want to autogenerate the pages but there are a couple of sub-sections where I want to specifically order them without having to rename the markdown files (as doing so is both a pain if adding extra pages and also it causes the URLs to include the numbering). I'm running Material for MkDocs as my theme so added it to the customer Docker image and activated it in plugins, now working perfectly, thanks for flagging this as an option 😃
Activity
[-]Alternative ways to define the page order if the navigation[/-][+]Alternative ways to define the page order in the navigation[/+]trel commentedon Aug 15, 2015
+1
This, alongside a default sort order (alphabetical) should be enough to provide an initial set of controls without a full user-specified navigation.
d0ugal commentedon Aug 15, 2015
Alphabetical sorting has been added in git master. Not sure if you are referencing that or not :)
connorlanigan commentedon Sep 24, 2015
I'd recommend an alternative approach: prepending a number to the file and folder names to indicate where they should be sorted. The numbers should then be excluded from the generation.
This way, a user can easily apply sorting where needed, without editing configuration files.
For example, a user could create three folders called
1. Zebras3. Elephants2. Antsand the navigation would then be
This should extend to files as well.
The MkDocs config would probably need a tag to disable this behaviour in case a user wants to have the numbers. Also, this behaviour should obviously only apply to files and folders that don't already have a specific order set in the config.
About the implementation:
Implementation could be quite easy.
If in a given directory all entries match the regex
^\d+\. ?.*(starting with a number, a dot and an optional whitespace), the new sorting would be applied.The existing alphabetical sorting would be replaced with a natural sorting algorithm such as natsort.
Then, from every entry, only regex
(?:^\d+\. ?)(.*)(the rest of the name) would be extracted as the name.waylan commentedon Sep 24, 2015
+1 to the original proposal. If it can be implemented cleanly with no weird side effects, I expect it would be a widely used feature.
However, I am very much opposed to the proposal to allow a sort order to be defined by prepending a number to the file names. If a user wants that amount of control, then they can use the config file and list every file (regardless of how long that list is).
waylan commentedon Sep 24, 2015
One final comment regarding sorting a long list of files: When/if a Plugin API (#206) is introduced, it is very likely that a hook could be used to provide any type of user defined sorting that is desired. The whole "sort by a filename prefix, then remove the prefix" sorting thing sounds like a great candidate for a third party plugin.
connorlanigan commentedon Sep 24, 2015
No, the users will probably care about that.
"That amount of control" in the meaning of "being able to define the order" is for a user who needs this to work just a basic, needed function.
Sorting by numbers would make it as intuitive as sorting things in real life: by placing them directly in the order we want them to be.
waylan commentedon Sep 24, 2015
Sorting by number prefixes on filenames is not necessarily easier. If I have 50 documents in a directory, each prefixed with a number in order and I want to insert a new document between numbers 5 and 6, I will have to edit every single filename for files 6 through 50 (45 filenames to edit without accidentally overwriting any existing files). Creating the list in the config would have been easier.
My point is that this is very much a special purpose edge case that will only be needed by a few users. Those few users should be able to get whatever sorting they want through a third party plugin. That way, the sorting fits each users exact needs and it not an almost good enough solution for most users.
@d0ugal's original proposal however, is something that will likely be useful to a large number of users and I think that makes sense as a default included feature.
connorlanigan commentedon Sep 24, 2015
Yes, you're right about that. However, this sorting would not be forced on the users. By default, files would be sorted as they are now. Only if the numbers are present, the natural sort would be applied. By this opt-in way, it would add functionality without interfering with how MkDocs works now.
Both of these proposals do not exclude each other. They do however address two different user bases, both of which are relevant to MkDocs:
As both user bases could be adressed by including both techniques, probably more users would be attracted to MkDocs.
waylan commentedon Sep 24, 2015
Right. And I'm saying that the way users "opt-in" is by using a plugin which implements the sorting function.
rathboma commentedon Oct 10, 2016
Did anything ever come of this? I'm building some docs now and directory ordering would be very welcome.
waylan commentedon Oct 10, 2016
@rathboma this was assigned the Post 1.0 milestone, so I don't expect any work to be done on this until after version 1.0 is released. We are currently working on version 0.16.
kylebarron commentedon Aug 14, 2018
I just wanted to check in on this now that Mkdocs is 1.0. I assume that you still want this to be implemented with a Mkdocs plugin? I may look into the API and see how easy it would be to do this.
12 remaining items
adam-stamand commentedon Jan 3, 2020
I have a use case that I'm not sure can be solved using the awesome-pages plugin.
I'd like to recursively include all markdown files within the docs directory (which can be achieved by just not including the
navfield in the mkdocs.yml, or using the awesome-pages plugin), but I'd also like to have a menu item link to a specific page (e.g.- Doxygen: "doxygen/html/index.html")My use case is using mkdocs with Doxygen. I'd like to have a menu item that takes a user directly to the Doxygen page (index.html), but since it's an html, the awesome-pages plugin has no way to link to it. The
navfield can be used, but if I use thenavfield, I lose the implicit recursive functionality.Does anything exist that can allow a user to specify menu items AND recursively include markdown files? Has anyone else used Doxygen with Mkdocs working around this limitation?
adam-stamand commentedon Jan 3, 2020
I should mention, to work around it I just have a .md file that contains nothing but a link to the Doxygen index.html. This works fine, I just think it'd be nice to be able to link directly to the page from the menu, as well as include all markdown files recursively, instead of either/or.
lukasgeiter commentedon Jan 4, 2020
@adam-stamand A workaround for this would be to use the
metaextension to redirect to the URL from your markdown page: https://squidfunk.github.io/mkdocs-material/extensions/metadata/#redirecting-to-another-pagebweston92 commentedon Apr 23, 2020
Did work get started on this?
grandams commentedon May 9, 2020
I've build a python script that generates the nav part walking down the doc sub-folders, reading md files and extracting the first level 1 title
https://gist.github.com/grandams/4f3ef0c9cdce2d048b8bea391e91b953
feel free to improve it f you want
hpatil47 commentedon May 19, 2020
@grandams if I have used this script, will it be help me to add any md file to automatically to mkdocs.yml. actually I am trying to create md files in docs folder, those should be automatically add to mkdocs.yml. is it possible with this script ?
idnsunset commentedon Jan 9, 2021
Globbing and "hidden" feature should be implemented as builtins. I think one of the most important features of a documentation generator is the ability to organize files easily and flexibly, especially for big projects. Many writers prefer Markdown over RST may choose to use mkdocs, but lacking features similar to Sphinx's
toctreedirective is sometimes really frustrating.The awesome-pages plugin can do some tricks, however it requires a
.pagefile in each involved directory, which is not in line with the concept of managing everything in a singlemkdocs.ymlfile.BTW, the
hiddenfeature means some files are not listed in the navigation while still get built into the site. For a big project with substantial files, we sometimes would rather use several index pages for navigation than have a long long list of nav entries, thus only index pages need show up in the nav list. This requires the globbing to load all files and the hidden feature to filter out whatever we don't want to see in the nav list.lukasgeiter commentedon Jan 9, 2021
@idnsunset Actually you can use globbing with awesome-pages in a single
mkdocs.ymlfile (see README). If you need more help or believe you have a use case which is not currently supported, feel free to create an issue (or update the one you opened).mottosso commentedon Jul 27, 2021
Also got struck with this and thought
BasePlugin.on_navseemed like the right place to look. But generating new navigation elements remained a challenge..The end result was navigation successfully appearing, but struck with oddities like the navigation in the footer not appearing, to navigate between "next" and "previous" pages. And for sections, like this one, not remaining open or indicating that it is currently opened.
Instead, I had greater success with
on_configwhich let me process themkdocs.ymlfile directly. Success!mysiki commentedon Sep 8, 2021
Hello, I got the same need, so I make plugin. Its' a simple plugin than check for dir in nav arbo and, if find it, expand it in nav config.
The change are made only during build / serve, nothing will be change in you mkdocs.yaml file.
Check the readme, it don't do more than reply to this thread (include directory in nav). If you need more, awesome-pages seem to can do many more think.
My plugin : https://github.com/mysiki/mkdocs_include_dir_to_nav/
Issue / PR is open ;)
EDIflyer commentedon Jul 17, 2022
Thank you so much @ojacques!! (and to @lukasgeiter for creating the plugin!) This has solved precisely the issue I'm having - I want to autogenerate the pages but there are a couple of sub-sections where I want to specifically order them without having to rename the markdown files (as doing so is both a pain if adding extra pages and also it causes the URLs to include the numbering). I'm running Material for MkDocs as my theme so added it to the customer Docker image and activated it in plugins, now working perfectly, thanks for flagging this as an option 😃
baarbaracrr commentedon Feb 16, 2024
Hello!
I ran into the same problem and found the following plugin to include in mkdocs: mkdocs-include-dir-to-nav 1.2.0
pip install mkdocs-include-dir-to-navI hope this has helped you!
oprypin commentedon Feb 16, 2024
pawamoy commentedon Apr 14, 2024
As of today there are multiple solutions for this, implemented as third-party plugins, just have a look at the navigation section of the catalog: https://github.com/mkdocs/catalog/?tab=readme-ov-file#-navigation--page-building. We think it shows how powerful and flexible the plugin system is, and therefore we'll close this as out of scope for MkDocs core 😊 Feel free to comment further!