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
Add generated_by
attribute to File
class
#3344
Conversation
Plugins like `mkdocs-gen-files` and the `mkdocs-material` blog plugin create new files that are added to the site dynamically. By adding a `generated_by` attribute to the `File` class, other plugins can improve interopt.
Add `generated_by` attribute to `File` class
I'm not sure where I stand on this decision, but I think for this to be more useful, normally it shouldn't be shown as "generated" by mkdocs, because these are based on real files. Or regardless of that, the default should just be |
I would love to see this in MkDocs, as it would provide a unified way of signaling that a file was generated. I second that the default should be set to |
As a plugin developer while I find the idea seducing I fail at getting a clear use case but that's maybe my own bias. I don't want to sound like putting down the idea, I just need help in understanding what missing feature that would bring to us plugin devs.
Can you explain your use case maybe @timvink?
That sounds like a convenience but not a concrete feature to me, why would that be that helpful to you?
I would assign the plugin name since we can then get access to it via the As for the default to |
The blog plugin must make use of
It would promote a standardized way for plugins to distinguish generated files.
How about multi instance capable plugins? For Material for MkDocs, those include: blog, optimize, privacy, social, tags. |
What that plugin actually wants to determine is whether a document is backed by a real file. Which I think is probably already possible to do in another way - by checking for discrepancies in paths or something like that - and this way can be made official and easier to use. |
@squidfunk thanks, I understand better how this can be a real feature. I have the feeling that if mkdocs itself was responsible for populating this field automatically it would be even better (that's how I imagined it in the first place). Imagine if this
Since my initial idea was that mkdocs itself would populate this field and knowing that newer mkdocs do prefix plugins with their name like |
From my experience writing plugins, I'm not sure how that should work reliably.
The current solution is far from ideal, as it does not support multi-instance as far as I know. There's no way to know which instance generated the file if you only have the name of the plugin, AFAIK. |
You must be right but in case it can help thinking about it I allowed myself a little POC in #3345 : food for thoughts (or not). |
@ultrabug: another more verbose idea might be to provide a subclass of file that is called |
To expand on my comment:
I mean that the plugin can just replace this code and then the attribute doesn't need to be set: if getattr(file, "generated_by"): # relies on plugins setting the value if Path(file.abs_src_path).is_relative_to(config.docs_dir): Although maybe this is not fully reliable with multi-instance plugins such as "monorepo" "multirepo" "static-i18n" |
@timvink indeed mentions some edge cases that would probably not be well supported by this approach of checking the file absolute path against the config file or docs dir path. |
Great input! I didn't see that |
Great discussion! Indeed the core issue for me is determining whether a File is backed by a real file. Trying to determine that relative to a docs or config directory got me into tricky edge cases trying to have compatibility with for example monorepo. I really like the proposal by @ultrabug of having The |
Instead of a generated_by attribute, perhaps it´s more future-proof to just add a generic |
That'd defeat the purpose of providing a unified way of saying "this file is generated". However, I agree that, in general, adding |
That´s true. |
I would refrain in using the
|
Of course naming could be discussed, metadata is perhaps not the best fit. |
Coming back here after 2 months to check if there's any progress. With MkDocs being adopted by more users and more plugins appearing that generate files, we need a standard way to signal that a file is generated. What are the current plans by the maintainers? I'm asking, since this issue/PR is not scheduled for the 1.6.0 milestone. |
Let's approve this Now I am working on a big change to improve the approach to generating files - obvious for plugins and easy enough even for hooks; no more messing with temporary directories. The addition of the |
Oh I didn't expect this actually. Do you think it would make sense to sometimes get ids such as |
I'm not sure what the best value is that should go into |
I have completed work on my pull request which also adds a convenient way to create a generated file for a Files collection. Most notable commits: Description: |
Plugins like
mkdocs-gen-files
and themkdocs-material
blog plugin create new files that are added to the site dynamically.By adding a
generated_by
attribute to theFile
class that plugin developers can use, other plugins can improve interopt.This PR is meant to open the discussion, feel free to close the PR if this is not the right direction / not useful.