Skip to content

How to add a watched directory #1952

Closed
@pawamoy

Description

@pawamoy

The docs says:

The serve event is only called when the serve command is used during development. It is passed the Server instance which can be modified before it is activated. For example, additional files or directories could be added to the list of "watched" files for auto-reloading.

How can I add files or directories?

I tried this:

    def on_serve(self, server, config, **kwargs):
        for element in self.config["watch"]:
            server.watch(element)
        return server

With this in my mkdocs.yml:

plugins:
  - search
  - mkdocstrings:
      watch:
        - src/mkdocstrings

It detects the changes, but since I gave no function to server.watch(dir, func), the site is not rebuilt.

I checked the source code of mkdocs, and I see that you are using a local function that we cannot reuse ourselves without rewriting it:

def builder():
log.info("Building documentation...")
config = load_config(
config_file=config_file,
dev_addr=dev_addr,
strict=strict,
theme=theme,
theme_dir=theme_dir,
site_dir=site_dir,
**kwargs
)
# Override a few config settings after validation
config['site_url'] = 'http://{}/'.format(config['dev_addr'])
live_server = livereload in ['dirty', 'livereload']
dirty = livereload == 'dirty'
build(config, live_server=live_server, dirty=dirty)
return config

and

server.watch(config['docs_dir'], builder)

What would be the best way to add a directory with this same builder functionality? Should I simply copy paste it?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions