-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
feat: improve typescript support for config file #465
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,146 +1 @@ | ||
export namespace DefaultTheme { | ||
export interface Config { | ||
logo?: string | ||
nav?: NavItem[] | false | ||
sidebar?: SideBarConfig | MultiSideBarConfig | ||
|
||
/** | ||
* GitHub repository following the format <user>/<project>. | ||
* | ||
* @example `"vuejs/vue-next"` | ||
*/ | ||
repo?: string | ||
|
||
/** | ||
* Customize the header label. Defaults to GitHub/Gitlab/Bitbucket | ||
* depending on the provided repo. | ||
* | ||
* @example `"Contribute!"` | ||
*/ | ||
repoLabel?: string | ||
|
||
/** | ||
* If your docs are in a different repository from your main project. | ||
* | ||
* @example `"vuejs/docs-next"` | ||
*/ | ||
docsRepo?: string | ||
|
||
/** | ||
* If your docs are not at the root of the repo. | ||
* | ||
* @example `"docs"` | ||
*/ | ||
docsDir?: string | ||
|
||
/** | ||
* If your docs are in a different branch. Defaults to `master`. | ||
* | ||
* @example `"next"` | ||
*/ | ||
docsBranch?: string | ||
|
||
/** | ||
* Enable links to edit pages at the bottom of the page. | ||
*/ | ||
editLinks?: boolean | ||
|
||
/** | ||
* Custom text for edit link. Defaults to "Edit this page". | ||
*/ | ||
editLinkText?: string | ||
|
||
/** | ||
* Show last updated time at the bottom of the page. Defaults to `false`. | ||
* If given a string, it will be displayed as a prefix (default value: | ||
* "Last Updated"). | ||
*/ | ||
lastUpdated?: string | boolean | ||
|
||
prevLinks?: boolean | ||
nextLinks?: boolean | ||
|
||
locales?: Record<string, LocaleConfig & Omit<Config, 'locales'>> | ||
|
||
algolia?: AlgoliaSearchOptions | ||
|
||
carbonAds?: { | ||
carbon: string | ||
custom?: string | ||
placement: string | ||
} | ||
} | ||
|
||
// navbar -------------------------------------------------------------------- | ||
|
||
export type NavItem = NavItemWithLink | NavItemWithChildren | ||
|
||
export interface NavItemBase { | ||
text: string | ||
target?: string | ||
rel?: string | ||
ariaLabel?: string | ||
activeMatch?: string | ||
} | ||
|
||
export interface NavItemWithLink extends NavItemBase { | ||
link: string | ||
} | ||
|
||
export interface NavItemWithChildren extends NavItemBase { | ||
items: NavItemWithLink[] | ||
} | ||
|
||
// sidebar ------------------------------------------------------------------- | ||
|
||
export type SideBarConfig = SideBarItem[] | 'auto' | false | ||
|
||
export interface MultiSideBarConfig { | ||
[path: string]: SideBarConfig | ||
} | ||
|
||
export type SideBarItem = SideBarLink | SideBarGroup | ||
|
||
export interface SideBarLink { | ||
text: string | ||
link: string | ||
} | ||
|
||
export interface SideBarGroup { | ||
text: string | ||
link?: string | ||
|
||
/** | ||
* @default false | ||
*/ | ||
collapsable?: boolean | ||
|
||
children: SideBarItem[] | ||
} | ||
|
||
// algolia ------------------------------------------------------------------ | ||
// partially copied from @docsearch/react/dist/esm/DocSearch.d.ts | ||
export interface AlgoliaSearchOptions { | ||
appId?: string | ||
apiKey: string | ||
indexName: string | ||
placeholder?: string | ||
searchParameters?: any | ||
disableUserPersonalization?: boolean | ||
initialQuery?: string | ||
} | ||
|
||
// locales ------------------------------------------------------------------- | ||
|
||
export interface LocaleConfig { | ||
/** | ||
* Text for the language dropdown. | ||
*/ | ||
selectText?: string | ||
|
||
/** | ||
* Label for this locale in the language dropdown. | ||
*/ | ||
label?: string | ||
} | ||
} | ||
export { DefaultTheme } from '../shared' |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,5 @@ | |
"vitepress": ["index.ts"] | ||
} | ||
}, | ||
"include": [".", "../../types/shared.d.ts"] | ||
"include": ["."] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,8 @@ import { | |
SiteData, | ||
HeadConfig, | ||
LocaleConfig, | ||
createLangDictionary | ||
createLangDictionary, | ||
DefaultTheme, | ||
} from './shared' | ||
import { resolveAliases, APP_PATH, DEFAULT_THEME_PATH } from './alias' | ||
import { MarkdownOptions } from './markdown/markdown' | ||
|
@@ -26,14 +27,16 @@ const debug = _debug('vitepress:config') | |
|
||
export type { MarkdownOptions } | ||
|
||
export interface UserConfig<ThemeConfig = any> { | ||
extends?: RawConfigExports | ||
export type ThemeConfig = any; | ||
|
||
export interface UserConfig<T extends ThemeConfig = ThemeConfig> { | ||
extends?: RawConfigExports<T> | ||
lang?: string | ||
base?: string | ||
title?: string | ||
description?: string | ||
head?: HeadConfig[] | ||
themeConfig?: ThemeConfig | ||
themeConfig?: T | ||
locales?: Record<string, LocaleConfig> | ||
markdown?: MarkdownOptions | ||
/** | ||
|
@@ -55,15 +58,15 @@ export interface UserConfig<ThemeConfig = any> { | |
mpa?: boolean | ||
} | ||
|
||
export type RawConfigExports = | ||
| UserConfig | ||
| Promise<UserConfig> | ||
| (() => UserConfig | Promise<UserConfig>) | ||
export type RawConfigExports<T extends ThemeConfig = ThemeConfig> = | ||
| UserConfig<T> | ||
| Promise<UserConfig<T>> | ||
| (() => UserConfig<T> | Promise<UserConfig<T>>) | ||
|
||
export interface SiteConfig<ThemeConfig = any> { | ||
export interface SiteConfig<T = ThemeConfig> { | ||
root: string | ||
srcDir: string | ||
site: SiteData<ThemeConfig> | ||
site: SiteData<T> | ||
configPath: string | undefined | ||
themeDir: string | ||
outDir: string | ||
|
@@ -82,7 +85,12 @@ const resolve = (root: string, file: string) => | |
/** | ||
* Type config helper | ||
*/ | ||
export function defineConfig(config: RawConfigExports) { | ||
export function defineConfig<T extends ThemeConfig = ThemeConfig>( | ||
config: UserConfig<T>, | ||
usingCustomTheme: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Motivation here: TypeScript cannot overload a function type inferring based on the number of generic type parameters, so I added an additional function parameters here. |
||
): void | ||
export function defineConfig(config: UserConfig<DefaultTheme.Config>): void | ||
export function defineConfig(config: ThemeConfig) { | ||
return config | ||
} | ||
|
||
|
@@ -150,7 +158,7 @@ async function resolveUserConfig( | |
} | ||
} | ||
|
||
const userConfig: RawConfigExports = configPath | ||
const userConfig: RawConfigExports<ThemeConfig> = configPath | ||
? (( | ||
await loadConfigFromFile( | ||
{ | ||
|
@@ -173,7 +181,7 @@ async function resolveUserConfig( | |
} | ||
|
||
async function resolveConfigExtends( | ||
config: RawConfigExports | ||
config: RawConfigExports<ThemeConfig> | ||
): Promise<UserConfig> { | ||
const resolved = await (typeof config === 'function' ? config() : config) | ||
if (resolved.extends) { | ||
|
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ | |
"compilerOptions": { | ||
"baseUrl": "." | ||
}, | ||
"include": [".", "../../types/shared.d.ts"] | ||
"include": ["."] | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this file to shared types since we need it between server and client.