Skip to content

Consider changing the precedence of title sources #3532

Open
mkdocs-ng/mkdocs
#52

Description

@ofek

Specifically, on this page https://www.mkdocs.org/user-guide/writing-your-docs/#meta-data I would prefer that the level 1 headers take priority over the navigation name, or an option to do so.

I'm realizing that deriving the title from the navigation sidebar does not provide a good user experience and I would like to avoid having to add meta-content to every page. I will use a real example: https://hatch.pypa.io/1.9/config/static-analysis/

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:

image

A consequence is that when you scroll down with Material for MkDocs the title on top is now no longer specific:

image

As well as the social card that shows up when you post a link to a page in Slack or Twitter or Discord:

slack-imgs

Activity

  1. ofek commented on Dec 30, 2023

    @ofek
    Author

    Maybe there is a way to achieve this immediately with a hook?

  2. oprypin commented on Dec 30, 2023

    @oprypin
  3. oprypin commented on Dec 30, 2023

    @oprypin
  4. oprypin commented on Dec 30, 2023

    @oprypin
  5. oprypin commented on Dec 30, 2023

    @oprypin
  6. ofek commented on Dec 30, 2023

    @ofek
    Author

    cc @squidfunk for visibility

  7. ofek commented on Dec 30, 2023

    @ofek
    Author

    Actually all themes at the moment explicitly use the same title attribute for both the page title and the nav entry.

    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?

  8. squidfunk commented on Dec 31, 2023

    @squidfunk
    Contributor

    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.

    My 2 cents.

  9. ofek commented on Jan 21, 2024

    @ofek
    Author

    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?

  10. ofek commented on Jan 28, 2024

    @ofek
    Author

    @oprypin Do you know of a way?

  11. ofek commented on Feb 1, 2024

    @ofek
    Author

    I tried looking into the code base but I still am not sure unfortunately.

  12. oprypin commented on Feb 1, 2024

    @oprypin
  13. ofek commented on Feb 1, 2024

    @ofek
    Author

    Sure I'm fine with waiting sorry to bother! Although, outside of titles is there any way to change front matter via a hook?

  14. oprypin commented on Feb 3, 2024

    @oprypin
  15. oprypin commented on Feb 3, 2024

    @oprypin
  16. 10 remaining items

  17. waylan commented on Feb 5, 2024

    @waylan
    Contributor

    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.

  18. ofek commented on Feb 5, 2024

    @ofek
    Author

    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.

  19. pawamoy commented on Feb 5, 2024

    @pawamoy
    Contributor

    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 :)

  20. oprypin commented on Mar 16, 2024

    @oprypin
  21. lovelydinosaur commented on Apr 15, 2024

    @lovelydinosaur
    Collaborator

    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.

  22. squidfunk commented on Apr 15, 2024

    @squidfunk
    Contributor

    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.

  23. lovelydinosaur commented on Apr 15, 2024

    @lovelydinosaur
    Collaborator

    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

    Related #3356

  24. honzajavorek commented on Jul 1, 2024

    @honzajavorek

    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?

  25. honzajavorek commented on Jul 1, 2024

    @honzajavorek

    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.

  26. pawamoy commented on Jul 1, 2024

    @pawamoy
    Contributor

    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 🤔

  27. squidfunk commented on Aug 1, 2024

    @squidfunk
    Contributor

    I think squidfunk/mkdocs-material#7411 might also be related.

  28. honzajavorek commented on Oct 21, 2024

    @honzajavorek

    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!

  29. added a commit that references this issue on Apr 28, 2026
    50c080c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions