Repository navigation
Conversation
Allow a theme to ship default themeConfig values in a config file colocated with its theme entry (.vitepress/theme/config.[ext]), resolved with the existing config loader. VitePress deep-merges these defaults under the extends chain and the site config at config resolution time, so sites only declare the fields they want to override — similar to Hexo's theme_config. - add defineThemeConfig() helper and mergeThemeConfig() (objects merge recursively; arrays and non-plain-object values are replaced wholesale, unlike mergeConfig which concatenates arrays) - theme defaults form the bottom-most config layer: extends chain and the site config override them - theme config file and its imports are tracked in configDeps, so dev restarts on change like site config changes - export the DeepPartial type used by the helper - document the convention in the custom theme guide
👷 Deploy request for vitepress-docs pending review.Visit the deploys page to approve it
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Themes commonly need their own
themeConfigdefaults — outline, docFooter, search provider, theme-specific options. Today a theme can only deliver those by shipping a config export (my-theme/config) that every site has to import and wire up manually, or by duplicating defaults into each site's config. Hexo solved this years ago withtheme_config: the theme ships defaults, the site config overrides them, and the framework merges them.This PR brings that model to VitePress. It covers the config-merging half of the theme-decoupling goal discussed in #5344 (component overrides are out of scope here).
What this PR does
A theme may ship a config file colocated with its theme entry:
VitePress loads it with the existing config loader and deep-merges it as the bottom-most
themeConfiglayer:Sites only declare the fields they want to override — everything else comes from the theme:
Merge semantics
Implemented by the new
mergeThemeConfig()(distinct frommergeConfig()on purpose):outline.level/navshould be fully overridable per key, whichmergeConfig's array concatenation would breakextendschain, always winsthemeConfig, below the existing locale/additional-config layersAPI
defineThemeConfig<ThemeConfig>(config)— typed helper for the theme config file (themeConfig?: DeepPartial<ThemeConfig>)mergeThemeConfig(themeDefaults, siteConfig)— exported for tests and advanced compositionDeepPartialis now exported from the shared typesBehavior details
loadConfigFromFileused for site configs and additional configstheme/config.[ext]andtheme/config/index.[ext]are resolvedconfigDeps— the dev server restarts on change, exactly like site config changes.vitepress/theme/); the default theme is unaffectedextendschain (that would require loading theme modules in Node). Themes building on other themes should import/spread their base defaults explicitlyDocs
New "Theme Config Defaults" section in the custom theme guide, including the npm-distribution pattern (ship defaults as a package sub-path, e.g.
my-theme/config, and document a one-line re-export in the site's.vitepress/theme/config.ts).Tests
mergeThemeConfig: nested merge under site values, array/non-plain-value replacement, undefined-key passthrough, single-layer fallbackNotes / possible follow-ups
themeConfigonly for now; contributinghead/markdowndefaults could be follow-ups