Replies: 3 comments 2 replies
-
Hello @day-jeff, mkdocs-material/src/templates/base.html Lines 48 to 51 in 39507ac and it's set to site_url during Page object initialization: https://github.com/mkdocs/mkdocs/blob/d6fcc56a3e2b287a2dc0310a7472ba4cdb177854/mkdocs/structure/pages.py#L49
So the only solution I see, is to change the def on_page_markdown(markdown, *, page, config, files):
# Get key from front matter
canonical_url = page.meta.get("canonical_url")
# Skip processing page when key not found
if canonical_url is None:
return
# Assign the custom url to the page
page.canonical_url = canonical_url
Also you could make it more dynamic perhaps, you said that some sites should have the changed canonical_url, so maybe they're in a directory or have some other matching pattern. You could use that pattern instead of adding front matter keys to them. |
Beta Was this translation helpful? Give feedback.
-
The path and file names are the same on the new site, but only a subset of pages are published there. So I use your code as is to retrieve front matter value indicating whether the page is published to the new site, and I rewrite page.canonical_url when it is. So simple now that I understand how hooks and mkdocs events work. |
Beta Was this translation helpful? Give feedback.
-
Ah! You and I are aligned. We already use custom front matter to specify which pages are published to the new site. Thus, I wrote code that looks at this existing value and automatically generates the correct canonical URL if needed. Thus, no need to edit the pages. Everything happens in the hook. |
Beta Was this translation helpful? Give feedback.
-
Is there a way to specify a custom canonical URL for each page?
Here's my scenario:
All help appreciated!
Beta Was this translation helpful? Give feedback.
All reactions