vuejs / vitepress

Vite & Vue powered static site generator.

Home Page:https://vitepress.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dynamic `titleTemplate` needed

boxsnake opened this issue · comments

Is your feature request related to a problem? Please describe.

In my case, I want make different titleTemplate for different route patterns.

For example, I have /guide and /faq. I want to use :title | A for /guide and :title | B for /faq.

Describe the solution you'd like

Two optional solutions:

  1. Expose a build hooks, which allows users to access pageData and / or siteConfig before build stage.
  2. Allow titleTemplate in config.js (config.ts) to use a function to generate template string according to pageData and siteConfig.

Describe alternatives you've considered

No response

Additional context

No response

Validations

@brc-dd

I tried transformPageData to change frontmatter's titleTemplate, but the page's <title> label didn't display as expected.

I did something like:

{
  ...
  transformPageData(pageData) {
    if (pageData.relativePath.includes('guide')) {
      pageData.frontmatter.titleTemplate = ':title | B';
    }
  }
}

However, the title is still in the pattern :title | A.

It seems only affect the variables used in vue templates, but it does not change <title> in build stage.

  transformPageData(pageData) {
    if (pageData.relativePath.includes('guide')) {
      pageData.titleTemplate = ':title | B'
    }
  }

By the way, the doc didn't state the differences between outer titleTemplate and the one in frontmatter object.

Hope this can be updated in the doc.

Thanks.