ultrabug / mkdocs-static-i18n

MkDocs i18n plugin using static translation markdown files
https://ultrabug.github.io/mkdocs-static-i18n/
MIT License
218 stars 37 forks source link

Incompatibility with Mkdocs Material Blog plugin? #283

Open nathancatania opened 6 months ago

nathancatania commented 6 months ago

Tested with mkdocs-static.i18n 1.2.0 and mkdocs-material 9.5.2-insiders.4.47.1:

Whenever I add the blog plugin with a post, I get the following error attempting to run mkdocs serve:

Traceback (most recent call last):
  File "/Users/user/Projects/project-name/venv/bin/mkdocs", line 8, in <module>
    sys.exit(cli())
             ^^^^^
  File "/Users/user/Projects/project-name/venv/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/Projects/project-name/venv/lib/python3.11/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/Users/user/Projects/project-name/venv/lib/python3.11/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/Projects/project-name/venv/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/Projects/project-name/venv/lib/python3.11/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/Projects/project-name/venv/lib/python3.11/site-packages/mkdocs/__main__.py", line 270, in serve_command
    serve.serve(**kwargs)
  File "/Users/user/Projects/project-name/venv/lib/python3.11/site-packages/mkdocs/commands/serve.py", line 86, in serve
    builder(config)
  File "/Users/user/Projects/project-name/venv/lib/python3.11/site-packages/mkdocs/commands/serve.py", line 67, in builder
    build(config, live_server=None if is_clean else server, dirty=is_dirty)
  File "/Users/user/Projects/project-name/venv/lib/python3.11/site-packages/mkdocs/commands/build.py", line 311, in build
    nav = config.plugins.on_nav(nav, config=config, files=files)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/Projects/project-name/venv/lib/python3.11/site-packages/mkdocs/plugins.py", line 536, in on_nav
    return self.run_event('nav', nav, config=config, files=files)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/Projects/project-name/venv/lib/python3.11/site-packages/mkdocs/plugins.py", line 507, in run_event
    result = method(item, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/Projects/project-name/venv/lib/python3.11/site-packages/mkdocs_static_i18n/plugin.py", line 76, in on_nav
    nav = self.reconfigure_material_blog(nav, config, files)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/Projects/project-name/venv/lib/python3.11/site-packages/mkdocs_static_i18n/reconfigure.py", line 765, in reconfigure_material_blog
    file.page._set_canonical_url(mkdocs_config.get('site_url', None))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

site_url is definitely set in my mkdocs.yml:

site_name: Help Center
site_url: "https://help.company.com"
site_description: "Help Center"
repo_url: https://[REDACTED]
repo_name: repo/repo-name
edit_uri: edit/main/docs/
copyright: "&copy; 2023, [COMPANY] Inc."

theme:
  language: en
  name: material
  custom_dir: overrides
  icon:
    repo: material/github
  palette:
    - media: "(prefers-color-scheme: light)"
      scheme: company
      toggle:
          icon: material/weather-sunny
          name: Switch to dark mode
    - media: "(prefers-color-scheme: dark)"
      scheme: slate
      toggle:
        icon: material/weather-night
        name: Switch to system preference
  features:
    - navigation.indexes
    - navigation.tabs
    - navigation.sections
    - navigation.top
    - navigation.footer
    - navigation.path
    - announce.dismiss
    - content.action.edit
    - content.action.view
  alternate:
    - name: English
      link: /
      lang: en
    - name: Japanese
      link: /ja/
      lang: ja

markdown_extensions:
  - tables
  - attr_list
  - def_list
  - admonition
  - pymdownx.details
  - meta
  - pymdownx.superfences:
      custom_fences:
        - name: mermaid
          class: mermaid
          format: !!python/name:pymdownx.superfences.fence_code_format
  - md_in_html
  - pymdownx.tabbed:
      alternate_style: true
  - pymdownx.emoji:
      emoji_index: !!python/name:material.extensions.emoji.twemoji
      emoji_generator: !!python/name:material.extensions.emoji.to_svg

plugins:
  - awesome-pages
  - table-reader
  - search
  - social:
      cards_layout_options:
        cards_layout: default
  - i18n:
      docs_structure: suffix
      fallback_to_default: true
      reconfigure_material: true
      languages:
        - locale: en
          default: true
          name: English
          build: true
        - locale: ja
          name: Japanese
          build: true
          nav_translations:
            Home: ホーム
            Getting Started: はじめに
  - blog:
      enabled: true
      blog_dir: blog
      post_dir: "{blog}/posts"
      post_date_format: long
      post_url_format: "{slug}"
      draft_if_future_date: true
      draft_on_serve: true
      blog_toc: true
  - git-revision-date-localized:
      type: date
  - git-committers:
      repository: [REDACTED]
      branch: main
      enabled: !ENV [CI, false]

extra_css:
  - stylesheets/extra.css

janvanveldhuizen has the same issue as per this comment: https://github.com/ultrabug/mkdocs-static-i18n/pull/268#issuecomment-1825246468

youyoubilly commented 6 months ago

I'm having the same error, please help! Big thanks!

ultrabug commented 4 months ago

Yes this is a known incompatibility unfortunately, mkdocs-material broke support when they rewrote the blog plugin and I've been unable to fix that since.

That's on my TODO list but can't share any ETA tbh.

niccokunzmann commented 2 months ago

I just added an if:

                if file.page:
                    file.page._set_canonical_url(mkdocs_config.get('site_url', None))

This makes the build succeed but the blog is still empty.