JonasDoesThings / mkdocs-exclude-unused-files

Exclude orphaned (unused) static files from your mkdocs build
MIT License
4 stars 2 forks source link

`asset_files` is a class attribute #6

Open thdhondt opened 4 weeks ago

thdhondt commented 4 weeks ago

Hello Jonas,

Currently, asset_files is defined as a class attribute:

class ExcludeUnusedFilesPlugin(BasePlugin[ExcludeUnusedFilesPluginConfig]):
    asset_files: Set[str] = set()

I'm currently working on a use case where I need to build several mkdocs websites consecutively from a script. Each website uses your plugin to get rid of unused files.

Since asset_files is a class attribute, its value is common for all instances of the ExcludeUnusedFilesPlugin. In practice, this means that the Set is not reset in between two consecutive builds. So, the next build complains because it tries to delete files that were already deleted during the previous build.

Do you think it would be possible to make asset_files an instance attribute instead? According to the mkdocs documentation, the right place to instantiate this variable would be the on_config callback or __init__ method: https://www.mkdocs.org/dev-guide/plugins/#on_startup

Note that for initializing variables, the init method is still preferred. For initializing per-build variables (and whenever in doubt), use the on_config event.

Following this advise, I think some of the stuff that is currently in on_startup might be better suited for on_config.

Thanks for your help

Thomas

thdhondt commented 2 weeks ago

Would you be open to a PR that fixes this issue?

JonasDoesThings commented 2 weeks ago

Hey, thanks for poking me again and sorry for not responding earlier - the issue was buried in my todo-stack. Sounds like a good improvement and yeah, I would definitely be open to changing that @thdhondt

If you would write a PR yourself that would be wonderful, else I could look into it and code it myself sometime next week, just let me know

Greetings from Austria :)

thdhondt commented 2 weeks ago

Sure, I will propose something later this week :).