How to modify sidebar in hooks? #257
-
I wrote a plugin that automatically generates sidebars. I hope users use the plugin to introduce the plugin and start writing posts without caring about how to introduce the sidebar.
In VuePress v1, i can do this:
const sidebarPlugin = (options, ctx) => {
let sidebar = {}
return {
name: 'vuepress-plugin-auto-sidebar',
ready() {
// change sidebar
sidebar = {}
},
enhanceAppFiles () {
return {
name: 'auto-sidebar-enhance',
content: `export default ({ siteData }) => { siteData.themeConfig.sidebar = ${JSON.stringify(sidebar)} }`
}
},
}
} In VuePress v2,
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Theme is loaded before initialization, so you can't modify theme config in plugin hooks. In fact, it makes no sence to make a plugin only for the default theme. A plugin should provide its own functionality, no matter what theme the user is using |
Beta Was this translation helpful? Give feedback.
-
Besides the original answer, you can try to make a helper function instead of a plugin. import { defineUserConfig } from '@vuepress/cli'
import { sidebarHelper } from 'your package'
export default defineUserConfig(sidebarHelper({
// user config
}}); |
Beta Was this translation helpful? Give feedback.
InonInitialized
hookTheme is loaded before initialization, so you can't modify theme config in plugin hooks.
In fact, it makes no sence to make a plugin only for the default theme. A plugin should provide its own functionality, no matter what theme the user is using