Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide more slots when extending the default theme #1050

Closed
6 tasks done
Charles7c opened this issue Jul 23, 2022 · 8 comments · Fixed by #1052
Closed
6 tasks done

Provide more slots when extending the default theme #1050

Charles7c opened this issue Jul 23, 2022 · 8 comments · Fixed by #1052

Comments

@Charles7c
Copy link
Contributor

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

Hi there!

I want to expand the theme to add content in the red box below, but we do not have slots in these two positions, I would like to ask if there is a good way?

image
image

Describe the solution you'd like

Is it possible to add doc-title-after, doc-footer-before slots like that.

Describe alternatives you've considered

For example, if I wanted to add content after the title, the schemes I tried were as follows:

  • Use custom components under the heading of each MD document.
  • Expand the custom content in the doc-before slot position, then move it when the page loads.

Obviously, they are either troublesome or have problems.

Additional context

I'm sorry to disturb you. In addition to completely customizing the theme, do you have a better plan to guide me? I am just like an amateur in front end.
Say thank you in advance.

Validations

@brc-dd
Copy link
Member

brc-dd commented Jul 23, 2022

Is it possible to add doc-title-after, doc-footer-before slots like that.

doc-title-after would not be possible because that heading is part of your markdown content. We don't have control over that. You can create some component say MainHeader, register it globally and use it on your every doc page instead of the # foo syntax. You'll also have to add title in frontmatter if you are adding it through your MainHeader component.

doc-footer-before is possible, feel free to create a PR for that.

Use custom components under the heading of each MD document.
Obviously, they are either troublesome or have problems.

What's the issue you're having?

@Charles7c
Copy link
Contributor Author

Charles7c commented Jul 23, 2022

Is it possible to add doc-title-after, doc-footer-before slots like that.

doc-title-after would not be possible because that heading is part of your markdown content. We don't have control over that. You can create some component say , register it globally and use it on your every doc page instead of the syntax. You'll also have to add in frontmatter if you are adding it through your component.MainHeader``# foo``title``MainHeader

doc-footer-before is possible, feel free to create a PR for that.

Use custom components under the heading of each MD document.
Obviously, they are either troublesome or have problems.

What's the issue you're having?

Sure enough, I have also considered this issue and I also think it is unlikely to add doc-title-after.

About the first solution I mentioned above, once you define a global component, you need to use it after the title every time you write an article. As a programmer, you obviously hate this kind of repetitive transaction. But I have to say it works.

Obviously, they are either troublesome or have problems.

The content mentioned in this section mainly refers to the second solution I tried above, which has a serious problem and requires a manual refresh after opening the page to take effect. That solution has been vetoed by me.

In replying to you, I thought of another way:

  • If the title attribute is set in FrontMatter, it is assumed that it needs to be specified as the title of the article, that is, use <h1>{{frontmatter. Title}}</h1> when rendering, then you can add doc-title-before, doc-title-after slots.

Do you think this solution is feasible? 😄

@brc-dd
Copy link
Member

brc-dd commented Jul 23, 2022

If the title attribute is set in FrontMatter, it is assumed that it needs to be specified as the title of the article,

Not exactly, people use it to change navbar title. Like having # foo in md, and having foo | ctx (ref).

@brc-dd
Copy link
Member

brc-dd commented Jul 23, 2022

you need to use it after the title every time you write an article. As a programmer, you obviously hate this kind of repetitive transaction

Probably you can write a markdown-it plugin for this to add your component after every h1.

Charles7c added a commit to Charles7c/vitepress that referenced this issue Jul 23, 2022
@brc-dd
Copy link
Member

brc-dd commented Jul 23, 2022

Register AfterTitle globally, then you can add this:

// .vitepress/config.ts
import { defineConfig } from 'vitepress'

export default defineConfig({
  markdown: {
    config: (md) => {
      md.renderer.rules.heading_close = (tokens, idx, options, env, slf) => {
        let htmlResult = slf.renderToken(tokens, idx, options, env, slf)
        if (tokens[idx].tag === 'h1') htmlResult += `\n<AfterTitle />`
        return htmlResult
      }
    }
  }
})

@Charles7c
Copy link
Contributor Author

全局注册,然后您可以添加以下内容:AfterTitle

// .vitepress/config.ts
import { defineConfig } from 'vitepress'

export default defineConfig({
  markdown: {
    config: (md) => {
      md.renderer.rules.heading_close = (tokens, idx, options, env, slf) => {
        let htmlResult = slf.renderToken(tokens, idx, options, env, slf)
        if (tokens[idx].tag === 'h1') htmlResult += `\n<AfterTitle />`
        return htmlResult
      }
    }
  }
})

awesome, thank you. 😃 👍

@Charles7c
Copy link
Contributor Author

Thank you again for solving the problem of doc-title-after well. @brc-dd

image

Looking forward to doc-footer-before.

brc-dd added a commit that referenced this issue Aug 1, 2022
Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
@Charles7c
Copy link
Contributor Author

Very good.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants