landmaj / mkdocs-d2-plugin

A plugin for embedding D2 diagrams in MkDocs.
https://landmaj.github.io/mkdocs-d2-plugin/
MIT License
16 stars 3 forks source link

File not found errors caused by blog plugin #13

Open landmaj opened 2 months ago

landmaj commented 2 months ago

Material for MkDocs blog plugin copies markdown files to create excerpts but it does not copy assets alongside them. This causes "file not found" errors.

For context: https://github.com/landmaj/mkdocs-d2-plugin/pull/11

landmaj commented 2 months ago

There are two ways to solve this:

  1. Find the original file
  2. Allow specifying diagram source directory in config, essentially pointing to assets directory where blog plugin copies all assets

I don't know if the first option is even possible, so number two seems like a more likely solution.

Karreg commented 2 months ago

I'm not sure 2. is a good idea. One of the good side of MkDocs versus Jekyll or Hugo, is the ease to target resources without specifying where they are. Especially with preview. Also, it breaks the way the plugin is working now, i.e. handling d2 files as if they were images. It's intuitive.

Isn't there a way to force the sequence of plugin execution? The issue here is that the blog plugin is executed before the d2 plugin.

landmaj commented 2 months ago

I just tested and it wouldn't work anyway since .d2 files are not copied to assets directory. Back to the drawing board.

Your plugin priority suggestion might work, I will test it.

Karreg commented 2 months ago

Order is documented here. Note sure we can do much. In my sample, the blog plugin is defined after the d2 plugin, but it's still executed before...

It would be worth the try to open a issue on material, I can do that.

Karreg commented 2 months ago

I just tested and it wouldn't work anyway since .d2 files are not copied to assets directory. Back to the drawing board.

Your plugin priority suggestion might work, I will test it.

Oh I missed that! 🤞

landmaj commented 2 months ago

I fixed the issue by rewriting all relative links to absolute links before blog plugin copies the files. I'm not merging it yet because I still need to make sure it doesn't break anything.

https://github.com/landmaj/mkdocs-d2-plugin/pull/15/files

landmaj commented 2 months ago

Please test version 1.4.0rc1 and let me know if it works for you.

Karreg commented 1 month ago

Sure, will do and get back to you as soon as possible :)

Karreg commented 1 month ago

I have tested it on my blog and it's indeed breaking with other plugins. I have isolated one, the git plugin:

WARNING -  [git-revision-date-localized-plugin] Unable to find a git directory and/or git is not installed. To ignore this error, set option 'fallback_to_build_date: true'
ERROR   -  Error reading page 'blog/index.md': /tmp/tmpor6ev5i5
Traceback (most recent call last):
  File "/home/gitpod/.local/lib/python3.10/site-packages/mkdocs/livereload/__init__.py", line 211, in _build_loop
    self.builder()
  File "/home/gitpod/.local/lib/python3.10/site-packages/mkdocs/commands/serve.py", line 67, in builder
    build(config, serve_url=None if is_clean else serve_url, dirty=is_dirty)
  File "/home/gitpod/.local/lib/python3.10/site-packages/mkdocs/commands/build.py", line 310, in build
    _populate_page(file.page, config, files, dirty)
  File "/home/gitpod/.local/lib/python3.10/site-packages/mkdocs/commands/build.py", line 163, in _populate_page
    page.markdown = config.plugins.on_page_markdown(
  File "/home/gitpod/.local/lib/python3.10/site-packages/mkdocs/plugins.py", line 635, in on_page_markdown
    return self.run_event('page_markdown', markdown, page=page, config=config, files=files)
  File "/home/gitpod/.local/lib/python3.10/site-packages/mkdocs/plugins.py", line 566, in run_event
    result = method(item, **kwargs)
  File "/home/gitpod/.local/lib/python3.10/site-packages/mkdocs_git_revision_date_localized_plugin/plugin.py", line 202, in on_page_markdown
    last_revision_timestamp = self.util.get_git_commit_timestamp(
  File "/home/gitpod/.local/lib/python3.10/site-packages/mkdocs_git_revision_date_localized_plugin/util.py", line 110, in get_git_commit_timestamp
    raise err
  File "/home/gitpod/.local/lib/python3.10/site-packages/mkdocs_git_revision_date_localized_plugin/util.py", line 81, in get_git_commit_timestamp
    git = self._get_repo(realpath)
  File "/home/gitpod/.local/lib/python3.10/site-packages/mkdocs_git_revision_date_localized_plugin/util.py", line 40, in _get_repo
    self.repo_cache[path] = Repo(path, search_parent_directories=True).git
  File "/home/gitpod/.local/lib/python3.10/site-packages/git/repo/base.py", line 289, in __init__
    raise InvalidGitRepositoryError(epath)
git.exc.InvalidGitRepositoryError: /tmp/tmpor6ev5i5
ERROR   -  [15:16:13] An error happened during the rebuild. The server will appear stuck until build errors are resolved.

I don't understand how the d2 plugin version could impact the path for the git plugin to find the git binary, but with version 1.3.3 it's working fine.

I have updated the test project to get the 1.4.0rc1 version and activate the git plugin: https://github.com/Karreg/mkdocs-d2-plugin-playground

To test with the previous version, you just need to force the version to 1.3.3 and call .devcontainer/post_attach.sh again, that is if you are using the devcontainer...

landmaj commented 1 month ago

It's probably because I had to change abs_src_path and it no longer points to a git directory. I guess the solution would be to execute my plugin as late as possible but before the blog plugin.

landmaj commented 1 month ago

Yup, I was right - https://github.com/squidfunk/mkdocs-material/issues/5336

landmaj commented 1 month ago

Changing plugin execution order does not solve the issue and I can't find a way to alter the Markdown without affecting other plugins, because files created by the blog plugin during on_files event have placeholder content. I started a discussion in the Material for MkDocs repo.

Karreg commented 1 month ago

It's probably because I had to change abs_src_path and it no longer points to a git directory. I guess the solution would be to execute my plugin as late as possible but before the blog plugin.

Maybe you can change it before executing your plugin, and set it back to its previous value just after? This way you don't have effect on other plugins, and whatever the order as long as it's before the blog plugin?

Karreg commented 1 month ago

I will check if the issue happens with drawio too. If not, maybe they have a way to work around this issue.

Karreg commented 1 month ago

I confirm the issue is also there with the drawio exporter. With blog plugin, image generation is broken, without blog plugin, it's working.

(I have added it to the playground project)

Karreg commented 1 month ago

Wouldn't this setup interesting to work around the issue? https://github.com/squidfunk/mkdocs-material/issues/5336#issuecomment-1502065618