You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consider changing the precedence of title sources #3532
The sidebar already represents clearly defined sections and in this case the entire sidebar relates to config so it would be very verbose to add the word "configuration" to every name.
Here is the full(ish) picture showing the desired title as the level 1 header:
A consequence is that when you scroll down with Material for MkDocs the title on top is now no longer specific:
As well as the social card that shows up when you post a link to a page in Slack or Twitter or Discord:
Title handling is broken since MkDocs 1.5 (see #3357). There are some instances where markup ends up in Page.title (see linked issue). Should Page.meta.title be allowed to contain markup? Plugins that rely on reading page titles in on_page_markdown or on_page_content might end up with inconsistent results, if Page.title is changed after reading the Markdown, but I guess this is no more worse than the current situation. Before anything is changed on title handling, it should be solidly spec'd, because there are several cases that currently lead to undefined behavior.
I just tried setting the front matter via a hook but it looks like at that point the metadata is already stripped out and processed. Is there a way to set the title based on the h1 via a hook?
why on earth would the user specify the meta title if it's impossible for it to be used if the site specifies titles in the nav config
Some more background. MkDocs didn't always to support defining page titles in the meta-data. And so if a user wanted a page title to be something other than the the <h1>, then they needed to use the nav. However, understandably, users did not like editing page attributes somewhere other than in the page file itself. So we added the meta-data option for those users.
So nav was the oldest and only option to begin with. (with filename as a fallback) Those users need to continue to be supported. Each additional option was built later, so they all have lower priority. And as explicit takes precedence over implicit, then we get nav, meta-data, content, filename as the only order we can support without breaking existing users sites.
But yes, a theme (or a custom template provided by a user) could conceivably check page.meta.title and use that instead of page.title. However, that only makes sense when page.title comes from the nav. Because in any other scenario, page.meta.title and page.title will be the same. That said, if a theme used some different meta-data key (like maybe nav-title), then anything is possible (including <title>{{ page.meta.nav-title or page.title }}</title>. In fact, this doesn't even need any changes to MkDocs for the user to do. A theme could build this in or even use a theme config option to optionally enable it. Or a user could define their own custom template block to override the relevant block of the theme they use.
In some pages of mine, I have the opposite case of @ofek's: Markdown title is short, nav title is longer and more descriptive, so I want the nav title to be used as HTML title (current behavior).
@pawamoy Can you please share an example? I think in the vast majority of cases users would benefit from the default sourced from the h1. I understand that probably won't happen, I'm just saying.
And you might be right! It would be interesting to look at current usage of meta+nav titles (only working with mkdocs-material IIUC) compared to nav+markdown titles, to see which use is the most common. I'm going to bed but I'll share an example tomorrow :)
I would prefer that the level 1 headers take priority over the navigation name
We can't make a switch like that, no. It'd break other users expectations. Echoing @waylan's comment above... "if I have a title defined in my nav, under no circumstances do I want a future update to MkDocs to suddenly start using the <h1> of the page as the page title."
or an option to do so
We should avoid configuration options where possible.
Theming options, great. Configuration options, bad.
I'd suggest that if there is an approach to resolving this, then it should be through theming and passing sufficient nav + toc context to the theme.
There's also a design flaw that's been pointed to...
The existing prioritization is based on the idea that explicit trumps implicit. If a user explicitly defines a title for a page (in nav or meta-data), then that title is used. However, if the user does not explicitly define a title, then get the implied title from the first <h1> of the page content, and if that doesn't exist, then from the file name.
That's something that I think? goes way back to early versions of MkDocs. A much neater approach would be to just be explicit. A nav config that always includes titles would be how I'd approach this if I was looking at a redesign. The toc structure could also be passed as context, and the decision around which to us would be a theming issue.
That's something that I think? goes way back to early versions of MkDocs. A much neater approach would be to just be explicit. A nav config that always includes titles would be how I'd approach this if I was looking at a redesign. The toc structure could also be passed as context, and the decision around which to us would be a theming issue.
I just had a talk with @pawamoy on another backlog grooming call that we did today on this very topic, after we didn't have enough time yesterday. It's important to know that there are many users that just throw a folder of Markdown files at MkDocs without specifying an explicit structure, which is an awesome way to get started quickly.
There are also several plugins that allow to manage explicit navigation auto-magically outside of mkdocs.yml. Thus, keeping this functionality, IMHO, is absolutely essential to provide a great out-of-the box experience ☺️ Also note that it's not primarily a theming issue, as plugins also need to know titles of pages, e.g. the search plugin.
All of that just from the top of my head – deeper evaluation required.
It seems to me that with the upgrade to 1.6.0 the page.title attribute now gives me whatever I've written to the front matter as a title:, is that correct? Because that completely breaks how I've been using MkDocs until now. I need three distinct titles:
nav title, a short one which displays in the navigation, footer of the website, etc.
SEO title, the one which displays in the <title> tag and thus in Google
H1 title, the one which displays as the largest one to the actual reader of the page
I'm using a custom theme. Until 1.6.0 I believe the mechanics of titles worked the way that in theme, page.title gave me the nav title and page.meta.title gave me the SEO title. I didn't need the H1 title in the theme for anything, thus no regrets it wasn't accessible. Maybe through page.toc? Dunno.
Now it seems that page.title and page.meta.title return the same thing, which breaks everything I have. It wouldn't be an issue if I could use a different attribute instead to access the nav title, but I see no such thing. I'm reading code of Page and scanning through this issue, but it seems the only way to fix this for me is to pin to an old version of MkDocs and then figure out what to do and if writing a custom plugin or something would help me.
Was such change of behavior intended or is it a bug?
I can now confirm that downgrading to 1.5.3 solves the issue. This change of behavior isn't mentioned in the 1.6.0 changelog (only that HTML gets parsed etc.), so it seems like a bug to me, but YMMV. If you want me to go to a separate issue with this, let me know and I'll do that and delete these comments not to pollute this one.
Yes, please open a new issue with a minimal reproducible example so that it is easier for us to investigate. I did a quick test and page.title gives me the title defined in nav, not in the front-matter. page.meta is empty though 🤔
Today I rolled up my sleeves to debug this. I made a minimal example and everything. And I couldn't reproduce the issue I previously described. So I added mkdocs-awesome-pages-plugin as that's what I use on production. I still couldn't reproduce the issue. So I tried to upgrade mkdocs at the production repo to see what actually happens, and I couldn't reproduce the issue. Titles work for me now 🤷♂️
A while back I changed the code which generates my navigation, for different reasons. I suspect that perhaps the code worked in a way which was all right with MkDocs 1.5, but wouldn't work with MkDocs 1.6? And when I changed it, I accidentally fixed the issue? Who knows… But for now, I guess I'm out!
Activity
ofek commentedon Dec 30, 2023
Maybe there is a way to achieve this immediately with a hook?
oprypin commentedon Dec 30, 2023
oprypin commentedon Dec 30, 2023
oprypin commentedon Dec 30, 2023
oprypin commentedon Dec 30, 2023
ofek commentedon Dec 30, 2023
cc @squidfunk for visibility
ofek commentedon Dec 30, 2023
In Material for MkDocs if I set the following then the title is in fact updated appropriately without changing the text in the sidebar: https://squidfunk.github.io/mkdocs-material/reference/#setting-the-page-title
Is there some way I can artificially set this metadata attribute in a hook based on the level 1 header?
squidfunk commentedon Dec 31, 2023
Title handling is broken since MkDocs 1.5 (see #3357). There are some instances where markup ends up in
Page.title(see linked issue). ShouldPage.meta.titlebe allowed to contain markup? Plugins that rely on reading page titles inon_page_markdownoron_page_contentmight end up with inconsistent results, ifPage.titleis changed after reading the Markdown, but I guess this is no more worse than the current situation. Before anything is changed on title handling, it should be solidly spec'd, because there are several cases that currently lead to undefined behavior.My 2 cents.
ofek commentedon Jan 21, 2024
pypa/hatch#1216
I just tried setting the front matter via a hook but it looks like at that point the metadata is already stripped out and processed. Is there a way to set the title based on the h1 via a hook?
ofek commentedon Jan 28, 2024
@oprypin Do you know of a way?
ofek commentedon Feb 1, 2024
I tried looking into the code base but I still am not sure unfortunately.
oprypin commentedon Feb 1, 2024
ofek commentedon Feb 1, 2024
Sure I'm fine with waiting sorry to bother! Although, outside of titles is there any way to change front matter via a hook?
oprypin commentedon Feb 3, 2024
oprypin commentedon Feb 3, 2024
10 remaining items
waylan commentedon Feb 5, 2024
Some more background. MkDocs didn't always to support defining page titles in the meta-data. And so if a user wanted a page title to be something other than the the
<h1>, then they needed to use the nav. However, understandably, users did not like editing page attributes somewhere other than in the page file itself. So we added the meta-data option for those users.So nav was the oldest and only option to begin with. (with filename as a fallback) Those users need to continue to be supported. Each additional option was built later, so they all have lower priority. And as explicit takes precedence over implicit, then we get
nav, meta-data, content, filenameas the only order we can support without breaking existing users sites.But yes, a theme (or a custom template provided by a user) could conceivably check
page.meta.titleand use that instead ofpage.title. However, that only makes sense whenpage.titlecomes from the nav. Because in any other scenario,page.meta.titleandpage.titlewill be the same. That said, if a theme used some different meta-data key (like maybenav-title), then anything is possible (including<title>{{ page.meta.nav-title or page.title }}</title>. In fact, this doesn't even need any changes to MkDocs for the user to do. A theme could build this in or even use a theme config option to optionally enable it. Or a user could define their own custom template block to override the relevant block of the theme they use.ofek commentedon Feb 5, 2024
@pawamoy Can you please share an example? I think in the vast majority of cases users would benefit from the default sourced from the
h1. I understand that probably won't happen, I'm just saying.pawamoy commentedon Feb 5, 2024
And you might be right! It would be interesting to look at current usage of meta+nav titles (only working with mkdocs-material IIUC) compared to nav+markdown titles, to see which use is the most common. I'm going to bed but I'll share an example tomorrow :)
oprypin commentedon Mar 16, 2024
lovelydinosaur commentedon Apr 15, 2024
We can't make a switch like that, no. It'd break other users expectations. Echoing @waylan's comment above... "if I have a title defined in my nav, under no circumstances do I want a future update to MkDocs to suddenly start using the <h1> of the page as the page title."
We should avoid configuration options where possible.
Theming options, great. Configuration options, bad.
I'd suggest that if there is an approach to resolving this, then it should be through theming and passing sufficient nav + toc context to the theme.
There's also a design flaw that's been pointed to...
That's something that I think? goes way back to early versions of MkDocs. A much neater approach would be to just be explicit. A nav config that always includes titles would be how I'd approach this if I was looking at a redesign. The
tocstructure could also be passed as context, and the decision around which to us would be a theming issue.squidfunk commentedon Apr 15, 2024
I just had a talk with @pawamoy on another backlog grooming call that we did today on this very topic, after we didn't have enough time yesterday. It's important to know that there are many users that just throw a folder of Markdown files at MkDocs without specifying an explicit structure, which is an awesome way to get started quickly.
There are also several plugins that allow to manage explicit navigation auto-magically outside of☺️ Also note that it's not primarily a theming issue, as plugins also need to know titles of pages, e.g. the search plugin.
mkdocs.yml. Thus, keeping this functionality, IMHO, is absolutely essential to provide a great out-of-the box experienceAll of that just from the top of my head – deeper evaluation required.
lovelydinosaur commentedon Apr 15, 2024
Related #3356
honzajavorek commentedon Jul 1, 2024
It seems to me that with the upgrade to 1.6.0 the
page.titleattribute now gives me whatever I've written to the front matter as atitle:, is that correct? Because that completely breaks how I've been using MkDocs until now. I need three distinct titles:<title>tag and thus in GoogleI'm using a custom theme. Until 1.6.0 I believe the mechanics of titles worked the way that in theme,
page.titlegave me the nav title andpage.meta.titlegave me the SEO title. I didn't need the H1 title in the theme for anything, thus no regrets it wasn't accessible. Maybe throughpage.toc? Dunno.Now it seems that
page.titleandpage.meta.titlereturn the same thing, which breaks everything I have. It wouldn't be an issue if I could use a different attribute instead to access the nav title, but I see no such thing. I'm reading code ofPageand scanning through this issue, but it seems the only way to fix this for me is to pin to an old version of MkDocs and then figure out what to do and if writing a custom plugin or something would help me.Was such change of behavior intended or is it a bug?
honzajavorek commentedon Jul 1, 2024
I can now confirm that downgrading to 1.5.3 solves the issue. This change of behavior isn't mentioned in the 1.6.0 changelog (only that HTML gets parsed etc.), so it seems like a bug to me, but YMMV. If you want me to go to a separate issue with this, let me know and I'll do that and delete these comments not to pollute this one.
downgrade mkdocs because of mkdocs/mkdocs#3532 (comment)
pawamoy commentedon Jul 1, 2024
Yes, please open a new issue with a minimal reproducible example so that it is easier for us to investigate. I did a quick test and
page.titlegives me the title defined innav, not in the front-matter.page.metais empty though 🤔squidfunk commentedon Aug 1, 2024
I think squidfunk/mkdocs-material#7411 might also be related.
honzajavorek commentedon Oct 21, 2024
Today I rolled up my sleeves to debug this. I made a minimal example and everything. And I couldn't reproduce the issue I previously described. So I added
mkdocs-awesome-pages-pluginas that's what I use on production. I still couldn't reproduce the issue. So I tried to upgrademkdocsat the production repo to see what actually happens, and I couldn't reproduce the issue. Titles work for me now 🤷♂️A while back I changed the code which generates my navigation, for different reasons. I suspect that perhaps the code worked in a way which was all right with MkDocs 1.5, but wouldn't work with MkDocs 1.6? And when I changed it, I accidentally fixed the issue? Who knows… But for now, I guess I'm out!
Expose page.content_title property for plugin authors (#52)