Skip to content

Auto-populate navigation sub-sections #714

Closed as not planned
Closed as not planned

Description

@d0ugal

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:

pages:
- Home: index.md
- User Guide: user-guide/*
- About: about/*

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.

Activity

  1. added this to the Post 1.0 milestone on Aug 15, 2015
  2. 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
  3. trel commented on Aug 15, 2015

    @trel
    Contributor

    +1

    This, alongside a default sort order (alphabetical) should be enough to provide an initial set of controls without a full user-specified navigation.

  4. d0ugal commented on Aug 15, 2015

    @d0ugal
    ContributorAuthor

    Alphabetical sorting has been added in git master. Not sure if you are referencing that or not :)

  5. connorlanigan commented on Sep 24, 2015

    @connorlanigan

    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.

  6. waylan commented on Sep 24, 2015

    @waylan
    Contributor

    +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).

  7. waylan commented on Sep 24, 2015

    @waylan
    Contributor

    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.

  8. connorlanigan commented on Sep 24, 2015

    @connorlanigan

    @waylan (regardless of how long that list is)

    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.

  9. waylan commented on Sep 24, 2015

    @waylan
    Contributor

    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.

  10. connorlanigan commented on Sep 24, 2015

    @connorlanigan

    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.

  11. waylan commented on Sep 24, 2015

    @waylan
    Contributor

    By this opt-in way, it would add functionality without interfering with how MkDocs works now.

    Right. And I'm saying that the way users "opt-in" is by using a plugin which implements the sorting function.

  12. rathboma commented on Oct 10, 2016

    @rathboma

    Did anything ever come of this? I'm building some docs now and directory ordering would be very welcome.

  13. waylan commented on Oct 10, 2016

    @waylan
    Contributor

    @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.

  14. kylebarron commented on Aug 14, 2018

    @kylebarron

    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.

  15. 12 remaining items

  16. adam-stamand commented on Jan 3, 2020

    @adam-stamand

    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?

  17. adam-stamand commented on Jan 3, 2020

    @adam-stamand

    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.

  18. lukasgeiter commented on Jan 4, 2020

    @lukasgeiter
    Contributor
  19. bweston92 commented on Apr 23, 2020

    @bweston92

    Did work get started on this?

  20. grandams commented on May 9, 2020

    @grandams

    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

    1. put the script at the same place than the mkdocs.yml
    2. configure the script (line 10) with the root folder for the doc
    3. comment last line of the script if you just want a print, either way, it automatically update the "nav" part of the mkdocs.yml file
    4. run it with python3 (not supported by python2x)

    feel free to improve it f you want

  21. hpatil47 commented on May 19, 2020

    @hpatil47

    @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 ?

  22. idnsunset commented on Jan 9, 2021

    @idnsunset

    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.

  23. lukasgeiter commented on Jan 9, 2021

    @lukasgeiter
    Contributor

    @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).

  24. mottosso commented on Jul 27, 2021

    @mottosso

    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..

        def on_nav(self, nav, config, files):
            import mkdocs.structure.nav as nav_
    
            for item in nav.items:
                if item.title == "Release History":
                    children = []
    
                    for index, (version, _) in enumerate(get_releases()):
                        url = "releases/%s/" % version
                        link = nav_.Link(version, url)  # Is this right?
                        link.is_page = True  # Are these necessary?
                        link.is_link = True
    
                        children.append(link)
    
                    item.children[:] = children
    
            return nav

    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!

        def on_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(
                entry for entry in config["nav"]
                if "Release History" in entry
            )["Release History"]
    
            # Start from scratch
            section[:] = []
    
            for version, fname in get_releases():
                section.append({version: fname})
    
            return config
  25. mysiki commented on Sep 8, 2021

    @mysiki

    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 ;)

  26. EDIflyer commented on Jul 17, 2022

    @EDIflyer

    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 😃

  27. baarbaracrr commented on Feb 16, 2024

    @baarbaracrr

    Hello!

    I ran into the same problem and found the following plugin to include in mkdocs: mkdocs-include-dir-to-nav 1.2.0

    1. Installs with: pip install mkdocs-include-dir-to-nav
    2. Include it in the plugins section:
    plugins:
      - search
      - include_dir_to_nav
    
    1. And then in nav you can directly include the folder address.

    I hope this has helped you!

  28. oprypin commented on Feb 16, 2024

    @oprypin
  29. pawamoy commented on Apr 14, 2024

    @pawamoy
    Contributor

    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!

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions