Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to hide a single file from nav? #102

Open
madjxatw opened this issue Mar 3, 2024 · 12 comments
Open

Is it possible to hide a single file from nav? #102

madjxatw opened this issue Mar 3, 2024 · 12 comments

Comments

@madjxatw
Copy link

madjxatw commented Mar 3, 2024

I see that hide: true could hide a directory from the navigation, but is there any simple way to hide a single file? The regex syntax might be used to filter out a file with a specific name, but I couldn't find an example. Please advice, thanks.

@lukasgeiter
Copy link
Owner

Not really. The regex might be able to do the trick, but it's not what it was designed for. Can you describe your use-case for hiding a single file?

@madjxatw
Copy link
Author

madjxatw commented Mar 4, 2024

I have some extra documents as supplementary readings, they do not and should not belong to the docs of my project. I only want to reference them from a Markdown file using the Markdown link syntax [](), so I need to make them part of the site but not directly navigable.

I might hide more files and expect a mechanism that works in the similar way as .gitignore where I could simply prefix a symbol (e.g. !) to ignore a file. I am not sure if this feature could be implemented by a plugin, maybe it is not possible due to the limitation of mkdocs itself.

@VladimirFokow
Copy link
Contributor

VladimirFokow commented Mar 4, 2024

@madjxatw
Here's an example that I used to put the 'Home' tab first, and then everything else (except for the top-level index.md file):

nav:
  - ... | home/**  # first in the nav order is the 'Home' tab
  - ... | regex=^(?!index\.md$).*  # then, all paths that are not exactly "index.md" of the root

But for your use-case, why can't you put all the files you want to hide into a directory and then just hide this directory?

@madjxatw
Copy link
Author

madjxatw commented Mar 4, 2024

@VladimirFokow, it sounds a good workaround to have a dedicated directory if there is no other better way, thanks!

@madjxatw
Copy link
Author

madjxatw commented Mar 4, 2024

@lukasgeiter , another use case is when blog pattern is enabled, a tags.md file is placed in the docs to make tags in each blog post clickable. See https://squidfunk.github.io/mkdocs-material/plugins/tags/?h=tags#config.tags_file. I want to include everything excluding the tags file.

@VladimirFokow
Copy link
Contributor

VladimirFokow commented Mar 4, 2024

@madjxatw , as far as I understand, you specify

plugins:
  - tags:
      tags_file: tags.md

only once - in mkdocs.yml file.
So there is only one tags_file path that exists - only one page where you see all your tags.

If you want to exclude this page from nav - also put it into that hidden folder?
(just remember to update its path in mkdocs.yml)

@madjxatw
Copy link
Author

madjxatw commented Mar 4, 2024

@VladimirFokow , I am currently using dedicated directories as you suggested for what to be hidden in nav. My site has a complex directory structure, so I create a "hidden" folder in each subdirectory (section) as needed for better organization and managment. Really appreciate your suggestion and awesome-pages plugin!

BTW, I still kind of expect that MkDocs itself offers a feature similar to gitignore in the future instead of .pages files being scattered around. 😛

@lukasgeiter
Copy link
Owner

Thanks for helping out @VladimirFokow!

Indeed, at the moment a hidden folder is the easiest solution. I've been playing with the thought of adding exclude patterns before, so it's good to know that there's a need for that 👍

@PlasmaHH
Copy link

Just came here for the same reason. I like the suggestion earlier with the special char like ! or so.

My use case is that I have some external links pointing to something inside my docs where I want to show a special "not here anymore look elsehwere" page, but I don't want that page to be visible at all in the navigation.

Since I do not have control over those external links I cannot use the directory trick described.

@kamilkrzyskow
Copy link
Contributor

@PlasmaHH
You can omit one file from the nav tree in .pages file and then use the mkdocs-redirects plugin to redirect the old missing unavailable path to the new page that contains special message.

nav:
    - Home:
        - ... | index.md
        # The not_here_anymore.md file is not included
    - dir_section

Just extract a specific file from the ...

If you're looking for a more automated approach perhaps you could modify our hook from here that uses GitPython and the renamed diff change to automatically create redirections in the plugin above:

@PlasmaHH
Copy link

@PlasmaHH You can omit one file from the nav tree in .pages file and then use the mkdocs-redirects plugin to redirect the old missing unavailable path to the new page that contains special message.

nav:
    - Home:
        - ... | index.md
        # The not_here_anymore.md file is not included
    - dir_section

Just extract a specific file from the ...

If you're looking for a more automated approach perhaps you could modify our hook from here that uses GitPython and the renamed diff change to automatically create redirections in the plugin above:

The redirect thing is really not an issue for me, there is nothing to redirect to. I am unsure at what your .pages file suggestion is hinting at, as it seems to just display the index.md file and nothing else. Or is the suggestion to hide one file to specify all the others manually?

To make it clearer, this is similar to how it looks like:

docs/
└── topic
    ├── .pages
    ├── deleted.md
    ├── entry1.md
    ├── entry2.md
    ├── entry3.md
    ├── entry4.md
    └── index.md

And in the end result I would like to have a simple entry in that .pages file that causes all the entry#.md and index.md to still be in the navbar, but just deleted.md should not be visible there.

@kamilkrzyskow
Copy link
Contributor

kamilkrzyskow commented Jun 27, 2024

@PlasmaHH
As per the docs:

You can use regex in the pattern matching, and regex allows for excluding values via the look ahead.

... | regex=^(?!deleted\.md)[\w\d]+\.md$
  • ^ and $ (in global scope) mean start and end respectively
  • (?!deleted\.md) excludes values that have deleted.md inside
  • [\w\d]+ matches any a-zA-z0-9 characters, so it doesn't match -_ , or other values, needs to be adjusted here if you are using those in your file names.

Here is a working example using the example above:


Though, if the file should be truly deleted then you should delete it, as this approach only hides it from nav, the file can be still accessed via the URL, and is added inside the sitemap.xml for search indexing ✌️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants