-
Notifications
You must be signed in to change notification settings - Fork 51
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
Provide a way to opt-in for a whole subdirectory, instead of file by file? #204
Comments
Noncommittal: one way to do that, might be to create an That would be the place in the code: if self.config['render_by_default']:
# opt-out: force of a page NOT to be interpreted,
opt_out = ignore_macros == True or render_macros == False
if opt_out:
return markdown
else:
# opt-in: force a page to be interpreted
opt_in = render_macros == True or ignore_macros == False
if not opt_in:
return markdown |
Creating a file in the directory is very good. The other approach is to have a setting in the mkdocs yaml file. There are pros and cons to each approach. I like the self-containment of having a file in the directory, but I know that many prefer to separate settings from docs, so prefer to have all configurations in folders outside the |
something like:
|
We could indeed, do like that. We already have list parameters, in the config file e.g. ('modules', PluginType(list,
default=[])), We could assume that the default directory is the docs directory; though that would change the convention for We also assume that this will be recursive, i.e.: for each page processed, make a check whether that page is part of that directory or its subdirectories. |
For the implementation, we could use a function inspired by this (suggested by ChatGPT): import os
def is_file_in_directory(file_path, directory):
# Get the absolute path of the directory
directory = os.path.abspath(directory)
# Check if the absolute path of the file is a subpath of the directory
return os.path.abspath(file_path).startswith(directory) or: from pathlib import Path
def is_file_in_directory(file_path, directory):
# Resolve the paths to eliminate any relative parts (like '..')
directory = Path(directory).resolve()
file_path = Path(file_path).resolve()
# Check if the file path is a descendant of the directory
return directory in file_path.parents |
I don't think we need to, in the example I used below, I had
For this feature, that makes sense. If you are including a directory, then you are including everything under that directory. Get ready for requests to add a flag for (nonrecursive), or add an option to exclude directory though, it will come sooner or later :) |
I would think the pattern matching syntax, as in .gitignore, but reversed 🤔 For that, we might use the Python PathSpec library. |
- this is a mechanism that forces rendering of files for the case when `render_by_default` is set to `false`. - it uses a git-like syntax to define files to include
The last commit implements this requirement of opt-in of files by directory, filename, etc when the parameter There is a new parameter called The syntax follows more or less the .gitignore pattern matching: this allows more than one instruction, with examples provided in this page. Comments (starting with a Here is an example: plugins:
- search
- macros:
# do not render the pages by default
# requires an opt-in
render_by_default: false
# render that path:
force_render_paths: |
# this directory will be rendered:
rendered/
# this pattern of files will be rendered:
render_*.md Notes
|
@mhussein Could I ask you to test this commit, and see if it could work for you? |
@fralau I tried it and it seems to be working fine for me. Thanks a lot for the quick turnaround. (1.) I think that if a file has btw, I am getting an error with the new version, I think it is unrelated, but there seems to be a conflict with the blogging plugin
Note: this file has |
Thanks a lot. Your argument about local overriding the global makes sense. I will have a look at it. It's good to know that the negative syntax (with |
Oh, I forgot to specify (but I think you worked it out) that the path is relative to the document directory ( That is how the paths of document pages are referenced by MkDocs, so it was simpler for me to stick to that convention. It is different than for other parameters, but it makes sense; I will just have to make sure that the documentation is clear. |
Yes, had to put all possible variations together first tbh, then started eliminating one by one till I figured it out :) It makes sense though to be consistent in the mkdocs file. |
I updated with cfa03e7, to give the last word to the I also updated the |
@fralau Thanks a lot for implementing this. Do you have a plan of when this can be available in a release? |
Discussed in #203
Originally posted by mhussein January 25, 2024
I have few directories in a very large project that uses macros while the rest doesn't. The project is set as opt-in with
Is there a way to avoid setting
render_macros: true
on every single file in those directories that use macros? Is there a way to tell the plugin to run on all files under certain folder(s)?The text was updated successfully, but these errors were encountered: