diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index ebaf9670..d92a9d7e 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -1,10 +1,6 @@ -import { createRequire } from 'module' import { defineConfig } from 'vitepress' import locales from './locales' -const require = createRequire(import.meta.url) -const pkg = require('vitepress/package.json') - export default defineConfig({ title: 'VitePress', @@ -12,7 +8,7 @@ export default defineConfig({ cleanUrls: true, head: [['meta', { name: 'theme-color', content: '#3c8772' }]], - + markdown: { headers: { level: [0, 0] diff --git a/docs/.vitepress/locales/en.ts b/docs/.vitepress/locales/en.ts index 7894b6cc..d052f361 100644 --- a/docs/.vitepress/locales/en.ts +++ b/docs/.vitepress/locales/en.ts @@ -11,12 +11,9 @@ export default defineConfig({ themeConfig: { nav: nav(), - lastUpdatedText: 'last Updated', - sidebar: { '/guide/': sidebarGuide(), - '/config/': sidebarConfig(), - '/api/': sidebarGuide() + '/reference/': sidebarReference() }, editLink: { @@ -31,6 +28,17 @@ export default defineConfig({ footer: { message: 'Released under the MIT License.', copyright: 'Copyright © 2019-present Evan You' + }, + + algolia: { + appId: '8J64VVRP8K', + apiKey: 'a18e2f4cc5665f6602c5631fd868adfd', + indexName: 'vitepress' + }, + + carbonAds: { + code: 'CEBDT27Y', + placement: 'vuejsorg' } } }) @@ -39,14 +47,9 @@ function nav() { return [ { text: 'Guide', link: '/guide/what-is-vitepress', activeMatch: '/guide/' }, { - text: 'Config Reference', - link: '/config/introduction', - activeMatch: '/config/' - }, - { - text: 'Runtime API', - link: '/api/', - activeMatch: '/api/' + text: 'Reference', + link: '/reference/site-config', + activeMatch: '/reference/' }, { text: pkg.version, @@ -72,9 +75,8 @@ function sidebarGuide() { items: [ { text: 'What is VitePress?', link: '/guide/what-is-vitepress' }, { text: 'Getting Started', link: '/guide/getting-started' }, - { text: 'Configuration', link: '/guide/configuration' }, { text: 'Routing', link: '/guide/routing' }, - { text: 'Deploying', link: '/guide/deploying' } + { text: 'Deploy', link: '/guide/deploy' } ] }, { @@ -92,45 +94,112 @@ function sidebarGuide() { text: 'Customization', collapsed: false, items: [ - { text: 'Default Theme', link: '/guide/default-theme' }, + { text: 'Using a Custom Theme', link: '/guide/custom-theme' }, { text: 'Extending the Default Theme', - link: '/guide/customization-extending-default-theme' + link: '/guide/extending-default-theme' }, - { text: 'Building a Custom Theme', link: '/guide/customization-intro' }, - { - text: 'Runtime API', - link: '/api/' - }, - { text: 'Build-Time Data Loading', link: '/guide/data-loading' } + { text: 'Build-Time Data Loading', link: '/guide/data-loading' }, + { text: 'Connecting to a CMS', link: '/guide/cms' } ] }, { - text: 'Migrations', + text: 'Experimental', collapsed: false, items: [ { - text: 'Migration from VuePress', - link: '/guide/migration-from-vuepress' - }, - { - text: 'Migration from VitePress 0.x', - link: '/guide/migration-from-vitepress-0' + text: 'MPA Mode', + link: '/guide/mpa-mode' } ] + }, + // { + // text: 'Migrations', + // collapsed: false, + // items: [ + // { + // text: 'Migration from VuePress', + // link: '/guide/migration-from-vuepress' + // }, + // { + // text: 'Migration from VitePress 0.x', + // link: '/guide/migration-from-vitepress-0' + // } + // ] + // }, + { + text: 'Config & API Reference', + link: '/reference/site-config' } ] } -function sidebarConfig() { +function sidebarReference() { return [ { - text: 'Config Reference', + text: 'Reference', items: [ - { text: 'Introduction', link: '/config/introduction' }, - { text: 'App Config', link: '/config/app-config' }, - { text: 'Default Theme Config', link: '/config/theme-config' }, - { text: 'Frontmatter Config', link: '/config/frontmatter-config' } + { text: 'Site Config', link: '/reference/site-config' }, + { text: 'Frontmatter Config', link: '/reference/frontmatter-config' }, + { text: 'Runtime API', link: '/reference/runtime-api' }, + { text: 'CLI', link: '/reference/cli' }, + { + text: 'Default Theme', + items: [ + { + text: 'Overview', + link: '/reference/default-theme-config' + }, + { + text: 'Nav', + link: '/reference/default-theme-nav' + }, + { + text: 'Sidebar', + link: '/reference/default-theme-sidebar' + }, + { + text: 'Home Page', + link: '/reference/default-theme-home-page' + }, + { + text: 'Footer', + link: '/reference/default-theme-footer' + }, + { + text: 'Layout', + link: '/reference/default-theme-layout' + }, + { + text: 'Badge', + link: '/reference/default-theme-badge' + }, + { + text: 'Team Page', + link: '/reference/default-theme-team-page' + }, + { + text: 'Prev / Next Links', + link: '/reference/default-theme-prev-next-links' + }, + { + text: 'Edit Link', + link: '/reference/default-theme-edit-link' + }, + { + text: 'Last Updated Timestamp', + link: '/reference/default-theme-last-updated' + }, + { + text: 'Algolia Search', + link: '/reference/default-theme-search' + }, + { + text: 'Carbon Ads', + link: '/reference/default-theme-carbon-ads' + } + ] + } ] } ] diff --git a/docs/.vitepress/locales/zh.ts b/docs/.vitepress/locales/zh.ts index 90581a15..fe3ec964 100644 --- a/docs/.vitepress/locales/zh.ts +++ b/docs/.vitepress/locales/zh.ts @@ -17,8 +17,7 @@ export default defineConfig({ sidebar: { '/zh/guide/': sidebarGuide(), - '/zh/config/': sidebarConfig(), - '/zh/api/': sidebarGuide() + '/zh/reference/': sidebarReference() }, editLink: { @@ -33,6 +32,17 @@ export default defineConfig({ footer: { message: 'Released under the MIT License.', copyright: 'Copyright © 2019-present Evan You' + }, + + algolia: { + appId: '8J64VVRP8K', + apiKey: 'a18e2f4cc5665f6602c5631fd868adfd', + indexName: 'vitepress' + }, + + carbonAds: { + code: 'CEBDT27Y', + placement: 'vuejsorg' } } }) @@ -40,19 +50,14 @@ export default defineConfig({ function nav() { return [ { - text: '指南', + text: 'Guide', link: '/zh/guide/what-is-vitepress', activeMatch: '/zh/guide/' }, { - text: '运行时 API', - link: '/zh/api/', - activeMatch: '/zh/api/' - }, - { - text: '配置参考', - link: '/zh/config/introduction', - activeMatch: '/zh/config/' + text: '参考', + link: '/zh/reference/site-config', + activeMatch: '/zh/reference/' }, { text: pkg.version, @@ -74,88 +79,138 @@ function sidebarGuide() { return [ { text: '简介', - collapsed: true, + collapsed: false, items: [ - { text: 'VitePress 是什么', link: '/zh/guide/what-is-vitepress' }, + { text: ' VitePress 是什么?', link: '/zh/guide/what-is-vitepress' }, { text: '快速开始', link: '/zh/guide/getting-started' }, - { text: '配置', link: '/zh/guide/configuration' }, { text: '路由', link: '/zh/guide/routing' }, - { text: '部署', link: '/zh/guide/deploying' } + { text: '部署', link: '/zh/guide/deploy' } ] }, { text: '写作', - collapsed: true, + collapsed: false, items: [ - { text: 'Markdown 扩展', link: '/zh/guide/markdown' }, - { text: '资源处理', link: '/zh/guide/asset-handling' }, + { text: 'Markdown Extensions', link: '/zh/guide/markdown' }, + { text: 'Asset Handling', link: '/zh/guide/asset-handling' }, { text: 'Frontmatter', link: '/zh/guide/frontmatter' }, - { text: '在 Markdown 中使用 Vue', link: '/zh/guide/using-vue' }, - { text: '国际化', link: '/zh/guide/i18n' } - ] - }, - { - text: '主题', - collapsed: true, - items: [ - { text: '简介', link: '/zh/guide/customization-intro' }, - { text: '导航', link: '/zh/guide/theme-nav' }, - { text: '侧边栏', link: '/zh/guide/theme-sidebar' }, - { text: '上下页链接', link: '/zh/guide/theme-prev-next-link' }, - { text: '编辑链接', link: '/zh/guide/theme-edit-link' }, - { text: '最后更新', link: '/zh/guide/theme-last-updated' }, - { text: '布局', link: '/zh/guide/theme-layout' }, - { text: '主页', link: '/zh/guide/theme-home-page' }, - { text: '团队页', link: '/zh/guide/theme-team-page' }, - { text: '徽章', link: '/zh/guide/theme-badge' }, - { text: '页脚', link: '/zh/guide/theme-footer' }, - { text: '搜索', link: '/zh/guide/theme-search' }, - { text: 'Carbon Ads', link: '/zh/guide/theme-carbon-ads' } + { text: 'Using Vue in Markdown', link: '/zh/guide/using-vue' }, + { text: 'Internationalization', link: '/zh/guide/i18n' } ] }, { - text: '迁移', - collapsed: true, + text: 'Customization', + collapsed: false, items: [ + { text: 'Using a Custom Theme', link: '/zh/guide/custom-theme' }, { - text: '从 VuePress 迁移', - link: '/zh/guide/migration-from-vuepress' + text: 'Extending the Default Theme', + link: '/zh/guide/extending-default-theme' }, - { - text: '从 VitePress 0.x 迁移', - link: '/zh/guide/migration-from-vitepress-0' - } + { text: 'Build-Time Data Loading', link: '/zh/guide/data-loading' }, + { text: 'Connecting to a CMS', link: '/zh/guide/cms' } ] }, { - text: '自定义', + text: 'Experimental', collapsed: false, items: [ - { text: '默认主题', link: '/zh/guide/default-theme' }, - { - text: '扩展默认主题', - link: '/zh/guide/customization-extending-default-theme' - }, - { text: '构建自定义主题', link: '/zh/guide/customization-intro' }, { - text: '运行时 API', - link: '/zh/api/' - }, - { text: 'Build-Time Data Loading', link: '/zh/guide/data-loading' } + text: 'MPA Mode', + link: '/zh/guide/mpa-mode' + } ] + }, + // { + // text: 'Migrations', + // collapsed: false, + // items: [ + // { + // text: 'Migration from VuePress', + // link: '/zh/guide/migration-from-vuepress' + // }, + // { + // text: 'Migration from VitePress 0.x', + // link: '/zh/guide/migration-from-vitepress-0' + // } + // ] + // }, + { + text: 'Config & API Reference', + link: '/zh/reference/site-config' } ] } -function sidebarConfig() { +function sidebarReference() { return [ { - text: '配置参考', + text: 'Reference', items: [ - { text: '简介', link: '/zh/config/introduction' }, - { text: '应用全局配置', link: '/zh/config/app-config' }, - { text: '主题配置', link: '/zh/config/theme-config' }, - { text: 'Frontmatter 配置', link: '/zh/config/frontmatter-config' } + { text: 'Site Config', link: '/zh/reference/site-config' }, + { + text: 'Frontmatter Config', + link: '/zh/reference/frontmatter-config' + }, + { text: 'Runtime API', link: '/zh/reference/runtime-api' }, + { text: 'CLI', link: '/zh/reference/cli' }, + { + text: 'Default Theme', + items: [ + { + text: 'Overview', + link: '/zh/reference/default-theme-config' + }, + { + text: 'Nav', + link: '/zh/reference/default-theme-nav' + }, + { + text: 'Sidebar', + link: '/zh/reference/default-theme-sidebar' + }, + { + text: 'Home Page', + link: '/zh/reference/default-theme-home-page' + }, + { + text: 'Footer', + link: '/zh/reference/default-theme-footer' + }, + { + text: 'Layout', + link: '/zh/reference/default-theme-layout' + }, + { + text: 'Badge', + link: '/zh/reference/default-theme-badge' + }, + { + text: 'Team Page', + link: '/zh/reference/default-theme-team-page' + }, + { + text: 'Prev / Next Links', + link: '/zh/reference/default-theme-prev-next-links' + }, + { + text: 'Edit Link', + link: '/zh/reference/default-theme-edit-link' + }, + { + text: 'Last Updated Timestamp', + link: '/zh/reference/default-theme-last-updated' + }, + { + text: 'Algolia Search', + link: '/zh/reference/default-theme-search' + }, + { + text: 'Carbon Ads', + link: '/zh/reference/default-theme-carbon-ads' + } + ] + } ] } ] diff --git a/docs/zh/api/index.md b/docs/zh/api/index.md deleted file mode 100644 index a42293fb..00000000 --- a/docs/zh/api/index.md +++ /dev/null @@ -1,94 +0,0 @@ -# 运行时 API 参考 {#runtime-api-reference} - -VitePress 提供了几个内置 API 来获取数据。VitePress 还提供了一些可以全局使用的内置组件。 - -可以从 `vitepress` 全局引入辅助函数,通常用于自定义主题 Vue 组件。当然,它们也可以在 .md 页面中使用,因为 Markdown 文件会被编译成 Vue 单文件组件。 - -以 `use*` 开头的方法表示它是一个 [Vue 3 组合式 API](https://cn.vuejs.org/guide/introduction.html#composition-api) 函数,只能在 `setup()` 内部使用或者使用 ` - - -``` - -## `useRoute` - -返回具有以下类型的当前路由对象: - -```ts -interface Route { - path: string - data: PageData - component: Component | null -} -``` - -## `useRouter` - -返回 VitePress 路由实例,用来以编程方式导航到另一个页面。 - -```ts -interface Router { - route: Route - go: (href?: string) => Promise -} -``` - -## `withBase` - -- **Type**: `(path: string) => string` - -将配置的 [`base`](/config/app-config#base) 添加到给定的 URL 路径。另请参阅 [Base URL](/guide/asset-handling#base-url)。 - -## `` - -`` 组件显示渲染的 markdown 内容。这在[创建你自己的主题时](/guide/customization-intro)很有用。 - -```vue - -``` - -## `` - -`` 组件只在客户端渲染它的插槽。 - -由于 VitePress 应用在生成静态文件之后会在 Node.js 中进行服务端渲染,因此任何 Vue 的使用都必须符合通用代码的要求。简而言之,确保只在 beforeMount 或 mounted 钩子中访问浏览器以及 DOM API。 - -如果你正在使用不支持 SSR 的组件 (例如,包含自定义指令),你可以将它们包装在 `ClientOnly` 组件中。 - -```vue-html - - - -``` diff --git a/docs/zh/config/app-config.md b/docs/zh/config/app-config.md deleted file mode 100644 index f3718abb..00000000 --- a/docs/zh/config/app-config.md +++ /dev/null @@ -1,393 +0,0 @@ -# 应用全局配置 {#app-config} - -应用全局配置是定义站点的全局配置的地方。应用全局配置不仅限于主题配置,还有如“根目录”的配置,或站点的“标题”设置。 - -```ts -export default { - // These are app level configs. - lang: 'en-US', - title: 'VitePress', - description: 'Vite & Vue powered static site generator.', - ... -} -``` - -## appearance - -- 类型:`boolean | 'dark'` -- 默认值:`true` - -这个配置项可以配置是否开启“黑暗”模式。 - -- 如果选项设置为 `true`,默认的主题将由用户的首选的颜色方案来决定。 -- 如果选项设置为 `dark`,该主题将默认为深色,用户手动切换颜色才会发生改变。 -- 如果选项设置为 `false`,用户将无法切换主题。 - -它还注入了内联脚本,通过 `vitepress-theme-appearance` 键从本地存储中读取用户设置,并恢复用户偏好的颜色模式。 - -```ts -export default { - appearance: true -} -``` - -## base - -- 类型:`string` -- 默认值:`/` - -站点将被部署到的根 URL。如果你打算在一个子路径下部署你的站点,例如 GitHub 页面,你就需要进行配置。如果你计划将站点部署到 `https://foo.github.io/bar/`,那么你应该将 base 设置为 `'/bar/'`。它应该总是以斜线开始,以斜线结束。 - -base 会自动预置到其他选项中以/开头的所有 URL 中,所以你只需要指定一次。 - -```ts -export default { - base: '/base/' -} -``` - -## description - -- 类型:`string` -- 默认值:`A VitePress site` - -站点的描述。在 HTML 页面中将被渲染成 `` 标签。 - -```ts -export default { - description: 'A VitePress site' -} -``` - -## head - -- 类型:`HeadConfig[]` -- 默认值:`[]` - -在 HTML 页面的 `` 标签中呈现的额外元素。用户添加的标签将在 `head` 标签结束前呈现,在 VitePress 标签之后。 - -```ts -export default { - head: [ - [ - 'link', - { rel: 'preconnect', href: 'https://fonts.gstatic.com', crossorigin: '' } - ] - // would render: - ] -} -``` - -```ts -type HeadConfig = - | [string, Record] - | [string, Record, string] -``` - -## ignoreDeadLinks - -- 类型:`boolean | 'localhostLinks'` -- 默认值:`false` - -当将其设置为 `true` 时,VitePress 不会因为死链接构建失败。若将其设置为 `localhostLinks`,`localhost` 链接不会构建失败,其他的死链接仍然会使构建失败。 - -```ts -export default { - ignoreDeadLinks: true -} -``` - -## lang - -- 类型:`string` -- 默认值:`en-US` - -站点的语言属性。在 HTML 页面中将被渲染为 `` 标签。 - -```ts -export default { - lang: 'en-US' -} -``` - -## lastUpdated - -- 类型:`boolean` -- 默认值:`false` - -使用 git commit 来获取时间戳。该选项使默认主题显示页面的最后更新时间。你可以通过 [`themeConfig.lastUpdatedText`](theme-config#lastupdatedtext) 选项来自定义文本。 - -```ts -export default { - lastUpdated: true -} -``` - -## markdown - -- 类型:`MarkdownOption` - -配置 Markdown 解析器的选项。VitePress 使用 [Markdown-it](https://github.com/markdown-it/markdown-it) 作为解析器,并使用 [Shiki](https://shiki.matsu.io/) 来高亮语言的语法。在这个选项中,你可以通过各种与 Markdown 有关的选项来满足你的需要。 - -```js -export default { - markdown: { - theme: 'material-palenight', - lineNumbers: true - } -} -``` - -以下是你在这个对象中可以进行设置的所有选项: - -```ts -interface MarkdownOptions extends MarkdownIt.Options { - // Custom theme for syntax highlighting. - // You can use an existing theme. - // See: https://github.com/shikijs/shiki/blob/main/docs/themes.md#all-themes - // Or add your own theme. - // See: https://github.com/shikijs/shiki/blob/main/docs/themes.md#loading-theme - theme?: - | Shiki.IThemeRegistration - | { light: Shiki.IThemeRegistration; dark: Shiki.IThemeRegistration } - - // Enable line numbers in code block. - lineNumbers?: boolean - - // Add support for your own languages. - // https://github.com/shikijs/shiki/blob/main/docs/languages.md#supporting-your-own-languages-with-shiki - languages?: Shiki.ILanguageRegistration - - // markdown-it-anchor plugin options. - // See: https://github.com/valeriangalliat/markdown-it-anchor#usage - anchor?: anchorPlugin.AnchorOptions - - // markdown-it-attrs plugin options. - // See: https://github.com/arve0/markdown-it-attrs - attrs?: { - leftDelimiter?: string - rightDelimiter?: string - allowedAttributes?: string[] - disable?: boolean - } - - // specify default language for syntax highlighter - defaultHighlightLang?: string - - // @mdit-vue/plugin-frontmatter plugin options. - // See: https://github.com/mdit-vue/mdit-vue/tree/main/packages/plugin-frontmatter#options - frontmatter?: FrontmatterPluginOptions - - // @mdit-vue/plugin-headers plugin options. - // See: https://github.com/mdit-vue/mdit-vue/tree/main/packages/plugin-headers#options - headers?: HeadersPluginOptions - - // @mdit-vue/plugin-sfc plugin options. - // See: https://github.com/mdit-vue/mdit-vue/tree/main/packages/plugin-sfc#options - sfc?: SfcPluginOptions - - // @mdit-vue/plugin-toc plugin options. - // See: https://github.com/mdit-vue/mdit-vue/tree/main/packages/plugin-toc#options - toc?: TocPluginOptions - - // Configure the Markdown-it instance. - config?: (md: MarkdownIt) => void -} -``` - -## outDir - -- 类型:`string` -- 默认值:`./.vitepress/dist` - -站点的构建输出位置,相对于项目根目录。(比如相对于 `docs` 目录,如果你运行的是 `vitepress build docs`)。 - -```ts -export default { - outDir: '../public' -} -``` - -## cacheDir - -- 类型:`string` -- 默认值:`./.vitepress/cache` - -缓存文件的目录,相对于项目根目录。(比如相对于 `docs` 目录,如果你运行的是 `vitepress build docs`)。参见:[cacheDir](https://cn.vitejs.dev/config/shared-options.html#cachedir)。 - -```ts -export default { - cacheDir: './.vitepress/.vite' -} -``` - -## srcDir - -- 类型:`string` -- 默认值:`.` - -存储 markdown 文件的位置,相对于项目根目录。 - -```ts -export default { - srcDir: './src' -} -``` - -## title - -- 类型:`string` -- 默认值:`VitePress` - -站点的标题。这将显示在导航栏中。除非定义了 `titleTemplate`,否则也作为所有页面标题的后缀。 - -```ts -export default { - title: 'VitePress' -} -``` - -## titleTemplate - -- 类型:`string | boolean` - -标题的后缀。例如,如果你将 `title` 设置为 `VitePress`,并将 `titleTemplate` 设置为 `My Site`,那么html 标题就变成 `VitePress | My Site`。 - -设置为 `false` 表示禁用该功能。如果该选项是 `undefined`,那么将使用 `title` 选项的值。 - -```ts -export default { - title: 'VitePress', - titleTemplate: 'Vite & Vue powered static site generator' -} -``` - -要配置 `|` 以外的标题分隔符,你可以省略 `title`,在 `titleTemplate` 中使用 `:title` 符号。 - -```ts -export default { - titleTemplate: ':title - Vitepress' -} -``` - -## cleanUrls {#cleanurls} - -- 类型:`boolean` -- 默认值:`false` - -允许从 URL 中去除尾部的 `.html`,并可选择生成简洁的目录结构。 - -```ts -export default { - cleanUrls: true -} -``` - -::: warning -启用此功能可能需要在你的托管平台上进行额外配置。为了使其正常工作,你的服务器必须**在不重定向的情况下**,请求 `/foo` 时提供 `/foo.html` 。 -::: - -## rewrites - -- Type: `Record` - -定义自定义目录和 URL 的映射。有关详细信息,请参阅[路由:自定义映射](/guide/routing#customize-the-mappings)。 - -```ts -export default { - rewrites: { - 'source/:page': 'destination/:page' - } -} -``` - -## 构建钩子 {#build-hooks} - -VitePress 构建钩子允许你向你的网站添加新的功能和行为: - -- Sitemap -- Search Indexing -- PWA - -### transformHead - -- 类型:`(ctx: TransformContext) => Awaitable` - -`transformHead` 是一个构建钩子,用于在生成每个页面之前转换 head。它将允许你添加不能静态添加到你的VitePress 配置中的 head 选项。你只需要返回额外的选项,它们将被自动合并到现有的选项中。 - -::: warning -不要改变 `ctx` 中的任何东西。 -::: - -```ts -export default { - async transformHead(ctx) { - // ... - } -} -``` - -```ts -interface TransformContext { - siteConfig: SiteConfig - siteData: SiteData - pageData: PageData - title: string - description: string - head: HeadConfig[] - content: string -} -``` - -### transformHtml - -- 类型:`(code: string, id: string, ctx: TransformContext) => Awaitable` - -`transformHtml` 是一个构建钩子,用于在保存到磁盘之前转换每个页面的内容。 - -::: warning -不要改变 `ctx` 中的任何东西。另外,修改 html 内容可能会在运行时引起激活问题。 -::: - -```ts -export default { - async transformHtml(code, id, context) { - // ... - } -} -``` - -### transformPageData - -- 类型:`(pageData: PageData) => Awaitable | { [key: string]: any } | void>` - -`transformPageData` 是一个用于转换每个页面的 `pageData` 的钩子。你可以直接改变 `pageData` 或者返回改变的值,这些值将被合并到 PageData 中。 - -```ts -export default { - async transformPageData(pageData) { - pageData.contributors = await getPageContributors(pageData.relativePath) - } - - // or return data to be merged - async transformPageData(pageData) { - return { - contributors: await getPageContributors(pageData.relativePath) - } - } -} -``` - -### buildEnd - -- 类型:`(siteConfig: SiteConfig) => Awaitable` - -`buildEnd` 是一个构建 CLI 的钩子,它将在构建 (SSG) 完成后,VitePress CLI 进程退出前运行。 - -```ts -export default { - async buildEnd(siteConfig) { - // ... - } -} -``` diff --git a/docs/zh/config/frontmatter-config.md b/docs/zh/config/frontmatter-config.md deleted file mode 100644 index 9d38f5fd..00000000 --- a/docs/zh/config/frontmatter-config.md +++ /dev/null @@ -1,192 +0,0 @@ -# Frontmatter 配置 {#frontmatter-config} - -Frontmatter 支持基于页面的配置。在每个标签上,你可以自由地添加任何设置来覆盖任何全局应用或主题配置。此外,还有一些配置,你只能在 Frontmatter 中定义。 - -```yaml ---- -title: Docs with VitePress -editLink: true ---- -``` - -你可以通过 `$frontmatter` 在任何 markdown 文件中访问 frontmatter。 - -```md -{{ $frontmatter.title }} -``` - -## title - -- Type: `string` - -页面的标题。它与 [config.title](../config/app-config#title) 相同,并覆盖了应用全局配置。 - -```yaml ---- -title: VitePress ---- -``` - -## titleTemplate - -- Type: `string | boolean` - -标题的后缀。它与 [config.titleTemplate](../config/app-config#titletemplate) 相同,并覆盖了应用全局配置。 - -```yaml ---- -title: VitePress -titleTemplate: Vite & Vue powered static site generator ---- -``` - -## description - -- Type: `string` - -页面的描述。它与 [config.description](../config/app-config#description) 相同,并覆盖了应用全局配置。 - -```yaml ---- -description: VitePress ---- -``` - -## head - -- Type: `HeadConfig[]` - -指定要注入的额外 head 标签。 - -```yaml ---- -head: - - - meta - - name: description - content: hello - - - meta - - name: keywords - content: super duper SEO ---- -``` - -```ts -type HeadConfig = - | [string, Record] - | [string, Record, string] -``` - -## lastUpdated - -- Type: `boolean` -- Default: `true` - -在当前页面是否显示[最后更新](../guide/theme-last-updated)文本。 - -```yaml ---- -lastUpdated: false ---- -``` - -## layout - -- Type: `doc | home | page` -- Default: `doc` - -决定页面的布局。 - -- `doc` - It applies default documentation styles to the markdown content. -- `home` - Special layout for "Home Page". You may add extra options such as `hero` and `features` to rapidly create beautiful landing page. -- `page` - Behave similar to `doc` but it applies no styles to the content. Useful when you want to create a fully custom page. - -```yaml ---- -layout: doc ---- -``` - -## hero - -- Type: `Hero` - -This option only takes effect when `layout` is set to `home`. - -It defines contents of home hero section. - -```yaml ---- -layout: home - -hero: - name: VitePress - text: Vite & Vue powered static site generator. - tagline: Lorem ipsum... - actions: - - theme: brand - text: Get Started - link: /guide/what-is-vitepress - - theme: alt - text: View on GitHub - link: https://github.com/vuejs/vitepress ---- -``` - -```ts -interface Hero { - // The string shown top of `text`. Comes with brand color - // and expected to be short, such as product name. - name?: string - - // The main text for the hero section. This will be defined - // as `h1` tag. - text: string - - // Tagline displayed below `text`. - tagline?: string - - // Action buttons to display in home hero section. - actions?: HeroAction[] -} - -interface HeroAction { - // Color theme of the button. Defaults to `brand`. - theme?: 'brand' | 'alt' - - // Label of the button. - text: string - - // Destination link of the button. - link: string -} -``` - -## features - -- Type: `Feature[]` - -This option only takes effect when `layout` is set to `home`. - -It defines items to display in features section. - -You may learn more about it in [Theme: Home Page](../guide/theme-home-page). - -## aside - -- Type: `boolean` -- Default: `true` - -If you want the right aside component in `doc` layout not to be shown, set this option to `false`. - -```yaml ---- -aside: false ---- -``` - -## outline - -- Type: `number | [number, number] | 'deep' | false` -- Default: `2` - -The levels of header in the outline to display for the page. It's same as [config.themeConfig.outline](../config/theme-config#outline), and it overrides the theme config. diff --git a/docs/zh/config/introduction.md b/docs/zh/config/introduction.md deleted file mode 100644 index 9ee0f3ec..00000000 --- a/docs/zh/config/introduction.md +++ /dev/null @@ -1,76 +0,0 @@ -# 简介 {#introduction} - -将配置文件放在 `.vitepress/config.js` 中。 这是放置所有 VitePress 特定文件的地方。 - -``` -. -├─ docs -│ ├─ .vitepress -│ │ └─ config.js -│ └─ index.md -└─ package.json -``` - -::: tip 提示 -你也可以使用任意的 `.ts`、`.cjs`、`.mjs`、`.cts` 和 `.mts` 作为配置文件的扩展名。 -::: - -VitePress 带有 2 种类型的配置。一种是[应用全局配置](./app-config),它配置站点的基本功能,例如设置站点的标题,或自定义 Markdown 解析器的工作方式。其次是[主题配置](./theme-config),用于配置站点的主题,例如添加侧边栏,或者添加“在 GitHub 上编辑此页面”链接等功能。 - -你还可以在 [Frontmatter](./frontmatter-config) 中进行另一种配置。Frontmatter 配置可以覆盖在该特定页面的应用全局配置或主题配置上定义的全局配置。但是,有几个选项也仅在 frontmatter 中可用。 - -请参阅相应的配置页面以了解更多信息。 - -## 配置智能提示 {#config-intellisense} - -由于 VitePress 附带 TypeScript 类型,你可以利用 IDE 的智能提示和 jsdoc 类型提示: - -```js -/** - * @type {import('vitepress').UserConfig} - */ -const config = { - // ... -} - -export default config -``` - -或者,你可以使用 `defineConfig` 辅助函数,它会提供智能提示,而无需 jsdoc 注释: - -```js -import { defineConfig } from 'vitepress' - -export default defineConfig({ - // ... -}) -``` - -VitePress 也直接支持 TS 配置文件。 你也可以将 `.vitepress/config.ts` 与 `defineConfig` 辅助函数一起使用。 - -## 主题配置类型 {#typed-theme-config} - -默认情况下,`defineConfig` 辅助函数使用默认主题的主题配置类型: - -```ts -import { defineConfig } from 'vitepress' - -export default defineConfig({ - themeConfig: { - // Type is `DefaultTheme.Config` - } -}) -``` - -如果你使用自定义主题并希望对主题配置进行类型检查,则需要使用 `defineConfigWithTheme`,并通过通用参数传递自定义主题的配置类型: - -```ts -import { defineConfigWithTheme } from 'vitepress' -import { ThemeConfig } from 'your-theme' - -export default defineConfigWithTheme({ - themeConfig: { - // Type is `ThemeConfig` - } -}) -``` diff --git a/docs/zh/guide/asset-handling.md b/docs/zh/guide/asset-handling.md index a571d5bd..c1f46675 100644 --- a/docs/zh/guide/asset-handling.md +++ b/docs/zh/guide/asset-handling.md @@ -1,46 +1,51 @@ -# 资源处理 {#asset-handling} +# Asset Handling -所有的 Markdown 文件都编译成 Vue 组件并由 [Vite](https://github.com/vitejs/vite) 处理。你可以**并且应该**使用相对路径引用资源: +All Markdown files are compiled into Vue components and processed by [Vite](https://vitejs.dev/guide/assets.html). You can, **and should**, reference any assets using relative URLs: ```md -![一张图片](./image.png) +![An image](./image.png) ``` -你可以在 markdown 文件、主题中的 `*.vue` 组件、styles 里和纯 `.css` 文件中通过使用绝对路径 (基于项目根目录) 或相对路径 (基于你的文件系统) 引用静态资源。相对路径的方式类似于使用 `vue-cli` 或 webpack 的 `file-loader` 时所习惯的写法。 +You can reference static assets in your markdown files, your `*.vue` components in the theme, styles and plain `.css` files either using absolute public paths (based on project root) or relative paths (based on your file system). The latter is similar to the behavior you are used to if you have used Vite, Vue CLI, or webpack's `file-loader`. -常规的图片、媒体和字体文件类型会被自动检测为静态资源。 +Common image, media, and font filetypes are detected and included as assets automatically. -所有引用的资源,包括使用绝对路径的资源,都将被复制到 dist 文件夹中,并在生产打包后生成哈希文件名。但不会复制未引用的资源。与 `vue-cli` 一样,小于 4kb 的图片资源将编译成 base64 的内联样式。 +All referenced assets, including those using absolute paths, will be copied to the dist folder with a hashed file name in the production build. Never-referenced assets will not be copied. Image assets smaller than 4kb will be base64 inlined - this can be configured via the [`vite`](/reference/site-config#vite) config option. -所有资源路径的引用,包括绝对路径,都应基于你的工作目录结构。 +All **static** path references, including absolute paths, should be based on your working directory structure. -## Public 文件 {#public-files} +## Public Files -有时你可能需要提供一些 Markdown 或主题组件中未直接引用的静态资源 (例如,站点图标和 PWA 图标)。 项目根目录下的 `public` 目录 (如果你正在运行的是 `vitepress build docs`,则为 `docs` 文件夹) 将会保留,用以提供源代码中从未引用的静态资源 (例如 `robots.txt`) 和需要保留完全相同的文件名 (不生成哈希) 的资源。 +Sometimes you may need to provide static assets that are not directly referenced in any of your Markdown or theme components (for example, favicons and PWA icons). The `public` directory under project root (`docs` folder if you're running `vitepress build docs`) can be used as an escape hatch to provide static assets that either are never referenced in source code (e.g. `robots.txt`), or must retain the exact same file name (without hashing). -放在 `public` 中的资源将会直接复制到 dist 的根目录。 +Assets placed in `public` will be copied to the root of the dist directory as-is. -注意,你应该使用从根目录开始以绝对路径引用放在 `public` 中的文件——例如,`public/icon.png` 在源代码中应始终引用为 `/icon.png`。 +Note that you should reference files placed in `public` using root absolute path - for example, `public/icon.png` should always be referenced in source code as `/icon.png`. + +There is one exception to this: if you have an HTML page in `public` and link to it from the main site, the router will yield a 404 by default. To get around this, VitePress provides a `pathname://` protocol which allows you to link to another page in the same domain as if the link is external. Contrast these two links: + +- [/pure.html](/pure.html) +- ## Base URL -如果你的站点没有部署到根 URL,则需要在 `.vitepress/config.js` 中设置 `base` 选项。 例如,如果你要将站点部署到 `https://foo.github.io/bar/`,那么 `base` 应该设置为 `'/bar/'` (以斜线开头和结尾)。 +If your site is deployed to a non-root URL, you will need to set the `base` option in `.vitepress/config.js`. For example, if you plan to deploy your site to `https://foo.github.io/bar/`, then `base` should be set to `'/bar/'` (it should always start and end with a slash). -所有静态资源路径都会自动处理以适配不同的 `base` 配置值。例如,在 markdown 中对 `public` 下的资源使用绝对路径引用: +All your static asset paths are automatically processed to adjust for different `base` config values. For example, if you have an absolute reference to an asset under `public` in your markdown: ```md -![一张照片](/image-inside-public.png) +![An image](/image-inside-public.png) ``` -使用这种引用方式,当你更改 `base` 配置值时无需再做修改。 +You do **not** need to update it when you change the `base` config value in this case. -但是,如果你正在创作一个动态链接到资源的主题组件,例如图片的 `src` 是基于主题设置的: +However, if you are authoring a theme component that links to assets dynamically, e.g. an image whose `src` is based on a theme config value: ```vue ``` -在这种情况下,建议使用 VitePress 提供的 [`withBase` 辅助函数](/api/#withbase) 来引用静态资源: +In this case it is recommended to wrap the path with the [`withBase` helper](/reference/runtime-api#withbase) provided by VitePress: ```vue + + +``` + +The [`useData()`](/reference/runtime-api#usedata) helper provides us with all the runtime data we need to conditionally render different layouts. One of the other data we can access is the current page's frontmatter. We can leverage this to allow the end user to control the layout in each page. For example, the user can indicate the page should use a special home page layout with: + +```md +--- +layout: home +--- +``` + +And we can adjust our theme to handle this: + +```vue{3,12-14} + + + +``` + +You can, of course, split the layout into more components: + +```vue{3-5,12-15} + + + +``` + +Consult the [Runtime API Reference](/reference/runtime-api) for everything available in theme components. In addition, you can leverage [Build-Time Data Loading](./data-loading) to generate data-driven layout - for example, a page that lists all blog posts in the current project. + +## Distributing a Custom Theme + +The easiest way to distribute a custom theme is by providing it as a [template repository on GitHub](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-template-repository). + +If you wish to distribute the theme as an npm package, follow these steps: + +1. Export the theme object as the default export in your package entry. + +2. If applicable, export your theme config type definition as `ThemeConfig`. + +3. If your theme requires adjusting the VitePress config, export that config under a package sub-path (e.g. `my-theme/config`) so the user can extend it. + +4. Document the theme config options (both via config file and frontmatter). + +5. Provide clear instructions on how to consume your theme (see below). + +## Consuming a Custom Theme + +To consume an external theme, import and re-export it from the custom theme entry: + +```js +// .vitepress/theme/index.js +import Theme from 'awesome-vitepress-theme' + +export default Theme +``` + +If the theme needs to be extended: + +```js +// .vitepress/theme/index.js +import Theme from 'awesome-vitepress-theme' + +export default { + extends: Theme, + enhanceApp(ctx) { + // ... + } +} +``` + +If the theme requires special VitePress config, you will need to also extend it in your own config: + +```ts +// .vitepress/theme/config.ts +import baseConfig from 'awesome-vitepress-theme/config' + +export default { + // extend theme base config (if needed) + extends: baseConfig +} +``` + +Finally, if the theme provides types for its theme config: + +```ts +// .vitepress/theme/config.ts +import baseConfig from 'awesome-vitepress-theme/config' +import { defineConfigWithTheme } from 'vitepress' +import type { ThemeConfig } from 'awesome-vitepress-theme' + +export default defineConfigWithTheme({ + extends: baseConfig, + themeConfig: { + // Type is `ThemeConfig` + } +}) +``` diff --git a/docs/zh/guide/customization-intro.md b/docs/zh/guide/customization-intro.md deleted file mode 100644 index 2bd0f70d..00000000 --- a/docs/zh/guide/customization-intro.md +++ /dev/null @@ -1,220 +0,0 @@ -# 主题介绍 {#theme-introduction} - -VitePress 带有默认主题,并提供许多开箱即用的功能。通过下面列出的导航来了解有关每个功能的更多信息。 - -- [导航](./theme-nav) -- [侧边栏](./theme-sidebar) -- [上一页/下一页链接](./theme-prev-next-link) -- [编辑链接](./theme-edit-link) -- [最后更新](./theme-last-updated) -- [布局](./theme-layout) -- [主页](./theme-home-page) -- [团队页面](./theme-team-page) -- [页脚](./theme-footer) -- [搜索](./theme-search) -- [Carbon Ads](./theme-carbon-ads) - -如果你没有找到所需的功能,或者你想创建自己的主题,你可以自定义 VitePress 以满足你的要求。在下面,我们将介绍自定义 VitePress 主题的方式。 - -## 使用自定义主题 {#using-a-custom-theme} - -你可以通过添加 `.vitepress/theme/index.js` 或 `.vitepress/theme/index.ts` 文件 (“主题入口文件”) 来启用自定义主题。 - -``` -. -├─ docs -│ ├─ .vitepress -│ │ ├─ theme -│ │ │ └─ index.js -│ │ └─ config.js -│ └─ index.md -└─ package.json -``` - -VitePress 自定义主题是一个只包含四个属性的对象,定义如下: - -```ts -interface Theme { - Layout: Component // Vue 3 component - NotFound?: Component - enhanceApp?: (ctx: EnhanceAppContext) => void - setup?: () => void -} - -interface EnhanceAppContext { - app: App // Vue 3 app instance - router: Router // VitePress router instance - siteData: Ref -} -``` - -主题入口文件应将主题作为其默认导出: - -```js -// .vitepress/theme/index.js -import Layout from './Layout.vue' - -export default { - // root component to wrap each page - Layout, - - // this is a Vue 3 functional component - NotFound: () => 'custom 404', - - enhanceApp({ app, router, siteData }) { - // app is the Vue 3 app instance from `createApp()`. - // router is VitePress' custom router. `siteData` is - // a `ref` of current site-level metadata. - } - - setup() { - // this function will be executed inside VitePressApp's - // setup hook. all composition APIs are available here. - } -} -``` - -... `Layout` 组件会如下所示: - -```vue - - -``` - -默认导出是自定义主题的唯一方式。 在自定义主题中,它就像普通的 Vite + Vue 3 应用程序一样工作。 注意,主题还需要[兼容 SSR](./using-vue#browser-api-access-restrictions)。 - -要分发主题,只需在包入口里导出对象。要使用外部主题,请从自定义主题入口导入并重新导出: - -```js -// .vitepress/theme/index.js -import Theme from 'awesome-vitepress-theme' - -export default Theme -``` - -## 扩展默认主题 {#extending-the-default-theme} - -如果你想扩展和自定义默认主题,你可以从 `vitepress/theme` 导入它并在导出自定义主题中对其进行扩展。以下是一些常见自定义的示例: - -### 注册全局组件 {#registering-global-components} - -```js -// .vitepress/theme/index.js -import DefaultTheme from 'vitepress/theme' - -export default { - ...DefaultTheme, - enhanceApp({ app }) { - // 注册一个全局组件 - app.component('MyGlobalComponent', /* ... */) - } -} -``` - -由于我们使用 Vite,你还可以利用 Vite 的[全局导入特性](https://vitejs.dev/guide/features.html#glob-import)自动注册组件目录。 - -### 自定义 CSS {#customizing-css} - -默认主题 CSS 可通过覆盖根元素的 CSS 变量进行自定义: - -```js -// .vitepress/theme/index.js -import DefaultTheme from 'vitepress/theme' -import './custom.css' - -export default DefaultTheme -``` - -```css -/* .vitepress/theme/custom.css */ -:root { - --vp-c-brand: #646cff; - --vp-c-brand-light: #747bff; -} -``` - -请参阅可以被覆盖的[默认的主题 CSS 变量](https://github.com/vuejs/vitepress/blob/main/src/client/theme-default/styles/vars.css)。 - -### Layout 组件插槽 {#layout-slots} - -默认主题 `` 组件有一些插槽,可用于在页面的某些位置注入内容。 这是一个将组件注入到之前的大纲中的示例: - -```js -// .vitepress/theme/index.js -import DefaultTheme from 'vitepress/theme' -import MyLayout from './MyLayout.vue' - -export default { - ...DefaultTheme, - // override the Layout with a wrapper component that - // injects the slots - Layout: MyLayout -} -``` - -```vue - - - - -``` - -或者你也可以使用渲染函数。 - -```js -// .vitepress/theme/index.js -import DefaultTheme from 'vitepress/theme' -import MyComponent from './MyComponent.vue' - -export default { - ...DefaultTheme, - Layout() { - return h(DefaultTheme.Layout, null, { - 'aside-outline-before': () => h(MyComponent) - }) - } -} -``` - -默认主题布局中可用插槽的完整列表: - -- 当通过 frontmatter 开启 `layout: 'doc'` (default): - - `doc-footer-before` - - `doc-before` - - `doc-after` - - `aside-top` - - `aside-bottom` - - `aside-outline-before` - - `aside-outline-after` - - `aside-ads-before` - - `aside-ads-after` -- 当通过 frontmatter 开启 `layout: 'home'`: - - `home-hero-before` - - `home-hero-info` - - `home-hero-after` - - `home-features-before` - - `home-features-after` -- 一定有的: - - `layout-top` - - `layout-bottom` - - `nav-bar-title-before` - - `nav-bar-title-after` - - `nav-bar-content-before` - - `nav-bar-content-after` - - `nav-screen-content-before` - - `nav-screen-content-after` diff --git a/docs/zh/guide/data-loading.md b/docs/zh/guide/data-loading.md index ee9c1c2b..0a6134e3 100644 --- a/docs/zh/guide/data-loading.md +++ b/docs/zh/guide/data-loading.md @@ -1,6 +1,15 @@ +# Build-Time Data Loading + +VitePress provides a feature called **data loaders** that allows you to load arbitrary data and import it from pages or components. The data loading is executed **only at build time**: the resulting data will be serialized as JSON in the final JavaScript bundle. + +Data loaders can be used to fetch remote data, or generate metadata based on local files. For example, you can use data loaders to parse all your local API pages and automatically generate an index of all API entries. + ## Basic Usage +A data loader file must end with either `.data.js` or `.data.ts`. The file should provide a default export of an object with the `load()` method: + ```js +// example.data.js export default { load() { return { @@ -10,30 +19,76 @@ export default { } ``` +The loader module is evaluated only in Node.js, so you can import Node APIs and npm dependencies as needed. + +You can then import data from this file in `.md` pages and `.vue` components using the `data` named export: + +```html + + +
{{ data }}
+``` + +Output: + +```json +{ + "data": "hello" +} +``` + +You'll notice the data loader itself does not export the `data`. It is VitePress calling the `load()` method behind the scenes and implicitly exposing the result via the `data` named export. + +This works even if the loader is async: + ```js export default { async load() { + // fetch remote data return (await fetch('...')).json() } } ``` -## Generating Data Based On Local Files +## Data from Local Files + +When you need to generate data based on local files, you should use the `watch` option in the data loader so that changes made to these files can trigger hot updates. + +The `watch` option is also convenient in that you can use [glob patterns](https://github.com/mrmlnc/fast-glob#pattern-syntax) to match multiple files. The patterns can be relative to the loader file itself, and the `load()` function will receive the matched files as absolute paths: ```js -import { readDirSync } from 'node:fs' +import fs from 'node:fs' +import parseFrontmatter from 'gray-matter' export default { - watch: ['*.md'], - async load() { - // + // watch all blog posts + watch: ['./posts/*.md'], + load(watchedFiles) { + // watchedFiles will be an array of absolute paths of the matched files. + // generate an array of blog post metadata that can be used to render + // a list in the theme layout + return watchedFiles.map(file => { + const content = fs.readFileSync(file, 'utf-8') + const { data, excerpt } = parseFrontmatter(content) + return { + file, + data, + excerpt + } + }) } } ``` -## Typed Data +## Typed Data Loaders + +When using TypeScript, you can type your loader and `data` export like so: ```ts +import { defineLoader } from 'vitepress' + export interface Data { // data type } @@ -41,9 +96,11 @@ export interface Data { declare const data: Data export { data } -export default { +export default defineLoader({ + // type checked loader options + glob: ['...'], async load(): Promise { // ... } -} +}) ``` diff --git a/docs/zh/guide/deploy.md b/docs/zh/guide/deploy.md new file mode 100644 index 00000000..904924c5 --- /dev/null +++ b/docs/zh/guide/deploy.md @@ -0,0 +1,230 @@ +# Deploy Your VitePress Site + +The following guides are based on some shared assumptions: + +- You are placing your docs inside the `docs` directory of your project. +- You are using the default build output location (`.vitepress/dist`). +- VitePress is installed as a local dependency in your project, and you have set up the following scripts in your `package.json`: + + ```json + { + "scripts": { + "docs:build": "vitepress build docs", + "docs:preview": "vitepress preview docs" + } + } + ``` + +::: tip + +If your site is to be served at a subdirectory (`https://example.com/subdir/`), then you have to set `'/subdir/'` as the [`base`](/reference/site-config#base) in your `docs/.vitepress/config.js`. + +**Example:** If you're using Github (or GitLab) Pages and deploying to `user.github.io/repo/`, then set your `base` to `/repo/`. + +::: + +## Build and Test Locally + +- You may run this command to build the docs: + + ```sh + $ npm run docs:build + ``` + +- Once you've built the docs, you can test them locally by running: + + ```sh + $ npm run docs:preview + ``` + + The `preview` command will boot up a local static web server that will serve the files from `.vitepress/dist` at `http://localhost:4173`. It's an easy way to check if the production build looks fine in your local environment. + +- You can configure the port of the server by passing `--port` as an argument. + + ```json + { + "scripts": { + "docs:preview": "vitepress preview docs --port 8080" + } + } + ``` + + Now the `docs:preview` method will launch the server at `http://localhost:8080`. + +## Netlify, Vercel, AWS Amplify, Cloudflare Pages, Render + +Set up a new project and change these settings using your dashboard: + +- **Build Command:** `npm run docs:build` +- **Output Directory:** `docs/.vitepress/dist` +- **Node Version:** `14` (or above, by default it usually will be 14 or 16, but on Cloudflare Pages the default is still 12, so you may need to [change that](https://developers.cloudflare.com/pages/platform/build-configuration/)) + +::: warning +Don't enable options like _Auto Minify_ for HTML code. It will remove comments from output which have meaning to Vue. You may see hydration mismatch errors if they get removed. +::: + +## GitHub Pages + +### Using GitHub Actions + +1. In your theme config file, `docs/.vitepress/config.js`, set the `base` property to the name of your GitHub repository. If you plan to deploy your site to `https://foo.github.io/bar/`, then you should set base to `'/bar/'`. It should always start and end with a slash. + +2. Create a file named `deploy.yml` inside `.github/workflows` directory of your project with the following content: + + ```yaml + name: Deploy + on: + workflow_dispatch: {} + push: + branches: + - main + jobs: + deploy: + runs-on: ubuntu-latest + permissions: + pages: write + id-token: write + environment: + name: github-pages + url: ${{ steps.deployment.outputs.page_url }} + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: actions/setup-node@v3 + with: + node-version: 16 + cache: npm + - run: npm install --frozen-lockfile + - name: Build + run: npm run docs:build + - uses: actions/configure-pages@v2 + - uses: actions/upload-pages-artifact@v1 + with: + path: docs/.vitepress/dist + - name: Deploy + id: deployment + uses: actions/deploy-pages@v1 + ``` + + ::: tip + Please replace the corresponding branch name. For example, if the branch you want to build is `master`, then you should replace `main` with `master` in the above file. + ::: + +3. In your repository's Settings under Pages menu item, select `GitHub Actions` in Build and deployment's Source. + +4. Now commit your code and push it to the `main` branch. + +5. Wait for actions to complete. + +6. In your repository's Settings under Pages menu item, click `Visit site`, then you can see your site. Your docs will automatically deploy each time you push. + +## GitLab Pages + +### Using GitLab CI + +1. Set `outDir` in `docs/.vitepress/config.js` to `../public`. + +2. Still in your config file, `docs/.vitepress/config.js`, set the `base` property to the name of your GitLab repository. If you plan to deploy your site to `https://foo.gitlab.io/bar/`, then you should set base to `'/bar/'`. It should always start and end with a slash. + +3. Create a file called `.gitlab-ci.yml` in the root of your project with the content below. This will build and deploy your site whenever you make changes to your content: + + ```yaml + image: node:16 + pages: + cache: + paths: + - node_modules/ + script: + - npm install + - npm run docs:build + artifacts: + paths: + - public + only: + - main + ``` + +4. Alternatively, if you want to use an _alpine_ version of node, you have to install `git` manually. In that case, the code above modifies to this: + ```yaml + image: node:16-alpine + pages: + cache: + paths: + - node_modules/ + before_script: + - apk add git + script: + - npm install + - npm run docs:build + artifacts: + paths: + - public + only: + - main + ``` + +## Azure Static Web Apps + +1. Follow the [official documentation](https://docs.microsoft.com/en-us/azure/static-web-apps/build-configuration). + +2. Set these values in your configuration file (and remove the ones you don't require, like `api_location`): + + - **`app_location`**: `/` + - **`output_location`**: `docs/.vitepress/dist` + - **`app_build_command`**: `npm run docs:build` + +## Firebase + +1. Create `firebase.json` and `.firebaserc` at the root of your project: + + `firebase.json`: + + ```json + { + "hosting": { + "public": "docs/.vitepress/dist", + "ignore": [] + } + } + ``` + + `.firebaserc`: + + ```json + { + "projects": { + "default": "" + } + } + ``` + +2. After running `npm run docs:build`, run this command to deploy: + + ```sh + firebase deploy + ``` + +## Surge + +1. After running `npm run docs:build`, run this command to deploy: + + ```sh + npx surge docs/.vitepress/dist + ``` + +## Heroku + +1. Follow documentation and guide given in [`heroku-buildpack-static`](https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-static). + +2. Create a file called `static.json` in the root of your project with the below content: + + ```json + { + "root": "docs/.vitepress/dist" + } + ``` + +## Edgio + +Refer [Creating and Deploying a VitePress App To Edgio](https://docs.edg.io/guides/vitepress). diff --git a/docs/zh/guide/deploying.md b/docs/zh/guide/deploying.md deleted file mode 100644 index 8cc38f97..00000000 --- a/docs/zh/guide/deploying.md +++ /dev/null @@ -1,225 +0,0 @@ -# 部署 {#deploying} - -指南基于以下前置环境: - -- 文档放在项目的 `docs` 目录中。 -- 使用默认的构建输出位置 (`.vitepress/dist`)。 -- VitePress 作为本地依赖安装在项目中,并且在 `package.json` 中设置了以下脚本: - - ```json - { - "scripts": { - "docs:build": "vitepress build docs", - "docs:serve": "vitepress serve docs" - } - } - ``` - -::: tip 提示 - -如果使用子目录(`https://example.com/subdir/`)作为部署站点,则必须在 `docs/.vitepress/config.js` 中将 `'/subdir/'` 设置为 [`base`](../config/app-config#base) 的值。 - -**示例:** 如果你使用 Github (或 GitLab) 页面并部署到 `user.github.io/repo/`,则将 `base` 设置为 `/repo/`。 - -::: - -## 本地打包和测试 {#build-and-test-locally} - -- 运行此命令来打包文档: - - ```sh - $ npm run docs:build - ``` - -- 打包文档后,你可以通过运行命令在本地进行调试: - - ```sh - $ npm run docs:serve - ``` - -`serve` 命令将启动一个本地静态 Web 服务,该服务将在 `http://localhost:4173` 输出来自 `.vitepress/dist` 的文件。 这是检查生产版本在你的本地环境中是否正常的简易方法。 - -- 可以通过传递 `--port` 作为参数来配置服务器运行的端口。 - - ```json - { - "scripts": { - "docs:serve": "vitepress serve docs --port 8080" - } - } - ``` - - 现在 `docs:serve` 方法将在 `http://localhost:8080` 启动服务器。 - -## 在 Netlify, Vercel, AWS Amplify, Cloudflare Pages 里部署 {#netlify-vercel-aws-amplify-cloudflare-pages-render} - -创建一个新项目并改成以下这些设置: - -- **Build Command:** `npm run docs:build` -- **Output Directory:** `docs/.vitepress/dist` -- **Node Version:** `14` (或者更高,默认值通常是 14 或 16,但在 Cloudflare Pages 上,默认值仍然是 12,所以你可能需要[修改](https://developers.cloudflare.com/pages/platform/build-configuration/))。 - -::: warning 警告 -不要为 HTML 代码启用 _Auto Minify_ 之类的选项。 它将从输出中删除对 Vue 有意义的注释。如果它们被删除,可能会出现页面 hydration 不正确的问题。 -::: - -## GitHub Pages - -### 使用 GitHub Actions {#using-github-actions} - -1. 在你的主题配置文件 `docs/.vitepress/config.js` 中,将 `base` 属性设置为你的 GitHub 仓库的名称。 如果你打算将你的站点部署到 `https://foo.github.io/bar/`,那么你应该将 base 设置为`'/bar/'`。 它应该始终以斜线开头和结尾。 - -2. 在项目的 `.github/workflows` 目录中创建一个名为 `deploy.yml` 的文件,内容如下: - - ```yaml - name: Deploy - on: - workflow_dispatch: {} - push: - branches: - - main - jobs: - deploy: - runs-on: ubuntu-latest - permissions: - pages: write - id-token: write - environment: - name: github-pages - url: ${{ steps.deployment.outputs.page_url }} - steps: - - uses: actions/checkout@v3 - with: - node-version: 16 - cache: npm - - run: npm install --frozen-lockfile - - name: Build - run: npm run docs:build - - uses: actions/configure-pages@v2 - - uses: actions/upload-pages-artifact@v1 - with: - path: docs/.vitepress/dist - - name: Deploy - id: deployment - uses: actions/deploy-pages@v1 - ``` - - ::: tip 提示 - 请替换对应的分支名称。例如,如果你要构建的分支是 `master`,则应将上述文件中的 `main` 替换为 `master`。 - ::: - -3. In your repository's Settings under Pages menu item, select `GitHub Actions` in Build and deployment's Source. - -4. 现在提交你的代码并将其推送到 `main` 分支。 - -5. 等待 action 完成。 - -6. 在 git 仓库的 Setting 选项里,选择 `gh-pages` 分支作为 GitHub Pages 的来源。现在,你的文档将在每次推送时自动部署。 - -## GitLab Pages - -### 使用 GitLab CI {#using-gitlab-ci} - -1. 将 `docs/.vitepress/config.js` 中的 `outDir` 设置为 `../public`。 - -2. 在项目的根目录中创建一个名为 `.gitlab-ci.yml` 的文件,内容如下。每更改内容时,将会构建和部署你的站点: - - ```yaml - image: node:16 - pages: - cache: - paths: - - node_modules/ - script: - - npm install - - npm run docs:build - artifacts: - paths: - - public - only: - - main - ``` - -4. Alternatively, if you want to use an _alpine_ version of node, you have to install `git` manually. In that case, the code above modifies to this: - ```yaml - image: node:16-alpine - pages: - cache: - paths: - - node_modules/ - before_script: - - apk add git - script: - - npm install - - npm run docs:build - artifacts: - paths: - - public - only: - - main - ``` - -## Azure Static Web Apps - -1. 参照[官方文档](https://docs.microsoft.com/en-us/azure/static-web-apps/build-configuration)。 - -2. 在配置文件中设置这些值(并删除不需要的值,例如 `api_location`): - - - **`app_location`**: `/` - - **`output_location`**: `docs/.vitepress/dist` - - **`app_build_command`**: `npm run docs:build` - -## Firebase - -1. 在项目根目录下创建 `firebase.json` 和 `.firebaserc` - - `firebase.json`: - - ```json - { - "hosting": { - "public": "docs/.vitepress/dist", - "ignore": [] - } - } - ``` - - `.firebaserc`: - - ```json - { - "projects": { - "default": "" - } - } - ``` - -2. 执行 `npm run docs:build` 打包命令以后, 执行以下脚本进行部署: - - ```sh - firebase deploy - ``` - -## Surge - -1. 执行 `npm run docs:build` 打包命令以后,执行以下脚本进行部署: - - ```sh - npx surge docs/.vitepress/dist - ``` - -## Heroku - -1. 参照 [`heroku-buildpack-static`](https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-static) 的文档和指南。 - -2. 在项目根目录下创建一个叫 `static.json` 的文件,内容如下: - - ```json - { - "root": "docs/.vitepress/dist" - } - ``` - -## Edgio - -参考[在 Edgio 里创建和部署 VitePress 应用](https://docs.edg.io/guides/vitepress)。 diff --git a/docs/zh/guide/extending-default-theme.md b/docs/zh/guide/extending-default-theme.md new file mode 100644 index 00000000..88647cec --- /dev/null +++ b/docs/zh/guide/extending-default-theme.md @@ -0,0 +1,138 @@ +# Extending the Default Theme + +VitePress' default theme is optimized for documentation, and can be customized. Consult the [Default Theme Config Overview](/reference/default-theme-config) for a comprehensive list of options. + +However, there are a number of cases where configuration alone won't be enough. For example: + +1. You need to tweak the CSS styling; +2. You need to modify the Vue app instance, for example to register global components; +3. You need to inject custom content into the theme via layout slots. + +These advanced customizations will require using a custom theme that "extends" the default theme. + +:::tip +Before proceeding, make sure to first read [Using a Custom Theme](./custom-theme) to understand how custom themes work. +::: + +## Customizing CSS + +The default theme CSS is customizable by overriding root level CSS variables: + +```js +// .vitepress/theme/index.js +import DefaultTheme from 'vitepress/theme' +import './custom.css' + +export default DefaultTheme +``` + +```css +/* .vitepress/theme/custom.css */ +:root { + --vp-c-brand: #646cff; + --vp-c-brand-light: #747bff; +} +``` + +See [default theme CSS variables](https://github.com/vuejs/vitepress/blob/main/src/client/theme-default/styles/vars.css) that can be overridden. + +## Registering Global Components + +```js +// .vitepress/theme/index.js +import DefaultTheme from 'vitepress/theme' + +export default { + extends: DefaultTheme, + enhanceApp(ctx) { + // register your custom global components + ctx.app.component('MyGlobalComponent' /* ... */) + } +} +``` + +Since we are using Vite, you can also leverage Vite's [glob import feature](https://vitejs.dev/guide/features.html#glob-import) to auto register a directory of components. + +## Layout Slots + +The default theme's `` component has a few slots that can be used to inject content at certain locations of the page. Here's an example of injecting a component into the before outline: + +```js +// .vitepress/theme/index.js +import DefaultTheme from 'vitepress/theme' +import MyLayout from './MyLayout.vue' + +export default { + ...DefaultTheme, + // override the Layout with a wrapper component that + // injects the slots + Layout: MyLayout +} +``` + +```vue + + + + +``` + +Or you could use render function as well. + +```js +// .vitepress/theme/index.js +import { h } from 'vue' +import DefaultTheme from 'vitepress/theme' +import MyComponent from './MyComponent.vue' + +export default { + ...DefaultTheme, + Layout() { + return h(DefaultTheme.Layout, null, { + 'aside-outline-before': () => h(MyComponent) + }) + } +} +``` + +Full list of slots available in the default theme layout: + +- When `layout: 'doc'` (default) is enabled via frontmatter: + - `doc-footer-before` + - `doc-before` + - `doc-after` + - `sidebar-nav-before` + - `sidebar-nav-after` + - `aside-top` + - `aside-bottom` + - `aside-outline-before` + - `aside-outline-after` + - `aside-ads-before` + - `aside-ads-after` +- When `layout: 'home'` is enabled via frontmatter: + - `home-hero-before` + - `home-hero-info` + - `home-hero-image` + - `home-hero-after` + - `home-features-before` + - `home-features-after` +- Always: + - `layout-top` + - `layout-bottom` + - `nav-bar-title-before` + - `nav-bar-title-after` + - `nav-bar-content-before` + - `nav-bar-content-after` + - `nav-screen-content-before` + - `nav-screen-content-after` + diff --git a/docs/zh/guide/frontmatter.md b/docs/zh/guide/frontmatter.md index b0984319..e0b15e88 100644 --- a/docs/zh/guide/frontmatter.md +++ b/docs/zh/guide/frontmatter.md @@ -1,6 +1,8 @@ # Frontmatter -任何包含 YAML frontmatter 的 Markdown 文件都将由 [gray-matter](https://github.com/jonschlinkert/gray-matter) 处理。 frontmatter 必须位于 Markdown 文件的顶部,并且必须采用在三点划线之间设置的有效 YAML 的形式。例如: +## Usage + +VitePress supports YAML frontmatter in all Markdown files, parsing them with [gray-matter](https://github.com/jonschlinkert/gray-matter). The frontmatter must be at the top of the Markdown file (before any elements including ` + +# Hello +``` + +Client scripts in all theme components will be bundled together, while client script for a specific page will be split for that page only. + +Notice that ` +``` + +### Rendering Raw Content + +Params passed to the page will be serialized in the client JavaScript payload, so you should avoid passing heavy data in params, for example raw Markdown or HTML content fetched from a remote CMS. + +Instead, you can pass such content to each page using the `content` property on each path object: + +```js +export default { + paths() { + async paths() { + const posts = await (await fetch('https://my-cms.com/blog-posts')).json() + + return posts.map((post) => { + return { + params: { id: post.id }, + content: post.content // raw Markdown or HTML + } + }) + } + } +} +``` + +Then, use the following special syntax to render the content as part of the Markdown file itself: + +```md + +``` diff --git a/docs/zh/guide/theme-carbon-ads.md b/docs/zh/guide/theme-carbon-ads.md deleted file mode 100644 index f7f013c2..00000000 --- a/docs/zh/guide/theme-carbon-ads.md +++ /dev/null @@ -1,22 +0,0 @@ -# Carbon Ads - -VitePress 内置了对 [Carbon Ads](https://www.carbonads.net/) 的原生支持。通过在配置中定义Carbon Ads凭证,VitePress将在页面上显示广告。 - -```js -export default { - themeConfig: { - carbonAds: { - code: 'your-carbon-code', - placement: 'your-carbon-placement' - } - } -} -``` - -这些值用于调用 carbon CDN 脚本,如下所示。 - -```js -`//cdn.carbonads.com/carbon.js?serve=${code}&placement=${placement}` -``` - -如需了解有关 Carbon Ads 配置的更多信息,请访问 [Carbon Ads 站点](https://www.carbonads.net/)。 diff --git a/docs/zh/guide/theme-edit-link.md b/docs/zh/guide/theme-edit-link.md deleted file mode 100644 index 3c304448..00000000 --- a/docs/zh/guide/theme-edit-link.md +++ /dev/null @@ -1,28 +0,0 @@ -# 编辑链接 {#edit-link} - -编辑链接可以显示链接以编辑 Git 管理服务 (例如 GitHub 或 GitLab) 上的页面。 可以通过 `themeConfig.editLink` 选项配置来启用。 - -```js -export default { - themeConfig: { - editLink: { - pattern: 'https://github.com/vuejs/vitepress/edit/main/docs/:path' - } - } -} -``` - -`pattern` 选项定义了链接的 URL 结构,`:path` 将被页面路径替换。 - -默认情况下,这将在文档页面底部添加链接文本“编辑此页面”。你可以通过定义 `text` 选项来自定义此文本。 - -```js -export default { - themeConfig: { - editLink: { - pattern: 'https://github.com/vuejs/vitepress/edit/main/docs/:path', - text: 'Edit this page on GitHub' - } - } -} -``` diff --git a/docs/zh/guide/theme-footer.md b/docs/zh/guide/theme-footer.md deleted file mode 100644 index 785a73d7..00000000 --- a/docs/zh/guide/theme-footer.md +++ /dev/null @@ -1,26 +0,0 @@ -# 页脚 {#footer} - -配置 `themeConfig.footer` 可以使 VitePress 在页面底部展示全局的页脚。 - -```ts -export default { - themeConfig: { - footer: { - message: 'Released under the MIT License.', - copyright: 'Copyright © 2019-present Evan You' - } - } -} -``` - -```ts -export interface Footer { - // The message shown rigth before copyright. - message?: string - - // The actual copyright text. - copyright?: string -} -``` - -注意,当[侧边栏](./theme-sidebar)可见时,不会显示页脚。 diff --git a/docs/zh/guide/theme-home-page.md b/docs/zh/guide/theme-home-page.md deleted file mode 100644 index 12f1b147..00000000 --- a/docs/zh/guide/theme-home-page.md +++ /dev/null @@ -1,116 +0,0 @@ -# 主页 - -VitePress 默认主题提供主页布局,你也可以在 [本站主页](../) 上看到使用的主页布局。 你可以通过在任何页面通过 [frontmatter](./frontmatter) 指定 `layout: home` 使用它。 - -```yaml ---- -layout: home ---- -``` - -但是,仅此选项不会有太大作用。 你可以通过设置额外的其他选项 (例如 `hero` 和 `features`) 将几个不同的预模板“部分”添加到主页。 - -## Hero 部分 - -Hero 部分位于主页的顶部。 以下是配置 Hero 部分的方法。 - -```yaml ---- -layout: home - -hero: - name: VitePress - text: Vite & Vue powered static site generator. - tagline: Lorem ipsum... - image: - src: /logo.png - alt: VitePress - actions: - - theme: brand - text: Get Started - link: /guide/what-is-vitepress - - theme: alt - text: View on GitHub - link: https://github.com/vuejs/vitepress ---- -``` - -```ts -interface Hero { - // `text' 的字符串所示。带有品牌颜色,通常会很短,例如项目名称。 - name?: string - - // hero 部分的文本。这将被定义成`h1`标签 - text: string - - // Tagline 会展示在 `text` 下面。 - tagline?: string - - // action 按钮显示在 hero 区域。 - actions?: HeroAction[] -} - -interface HeroAction { - // 按钮的的主题颜色,默认为 `brand`。 - theme?: 'brand' | 'alt' - - // 按钮的内容。 - text: string - - // 按钮链接。 - link: string -} -``` - -### 自定义名字颜色 {#customizing-the-name-color} - -VitePress 使用品牌颜色 (`--vp-c-brand`) 作为 `name`。 但是,你可以通过覆盖 `--vp-home-hero-name-color` 变量来自定义此颜色。 - -```css -:root { - --vp-home-hero-name-color: blue; -} -``` - -你也可以通过组合 `--vp-home-hero-name-background` 来进一步自定义它,以赋予 `name` 渐变颜色。 - -```css -:root { - --vp-home-hero-name-color: transparent; - --vp-home-hero-name-background: -webkit-linear-gradient(120deg, #bd34fe, #41d1ff); -} -``` - -## Features 部分 {#features-section} - -在 Features 部分,你可以在 hero 部分之后列出你想要显示的任意数量的功能。 要配置它,请在 `formatter` 中配置 `features`。 - -```yaml ---- -layout: home - -features: - - icon: ⚡️ - title: Vite, The DX that can't be beat - details: Lorem ipsum... - - icon: 🖖 - title: Power of Vue meets Markdown - details: Lorem ipsum... - - icon: 🛠️ - title: Simple and minimal, always - details: Lorem ipsum... ---- -``` - -```ts -interface Feature { - // 在 feature 框里展示 icon,目前只支持 emoji - icon?: string - - // feature 标题 - title: string - - // feature 详情 - details: string -} -``` diff --git a/docs/zh/guide/theme-last-updated.md b/docs/zh/guide/theme-last-updated.md deleted file mode 100644 index d26e0b84..00000000 --- a/docs/zh/guide/theme-last-updated.md +++ /dev/null @@ -1,20 +0,0 @@ -# 最后更新 {#last-updated} - -最后内容的更新时间将显示在页面的右下角。要启用它,请在你的配置中添加 `lastUpdated` 选项。 -## 页面配置 {#page-configuration} - -添加 `lastUpdated` 选项到配置中去。 -```js -export default { - lastUpdated: true -} -``` - -## Frontmatter 配置 {#frontmatter-configuration} -如果你想隐藏最后更新的文本,请对 `lastUpdated` 选项设置为 false。 - -```yaml ---- -lastUpdated: false ---- -``` diff --git a/docs/zh/guide/theme-layout.md b/docs/zh/guide/theme-layout.md deleted file mode 100644 index 0a190142..00000000 --- a/docs/zh/guide/theme-layout.md +++ /dev/null @@ -1,37 +0,0 @@ -# Layout - -你可以通过在页面 [frontmatter](./frontmatter) 中设置 `layout` 选项选择页面布局。有 3 个布局选项,`doc`、`page` 和 `home`。 如果未指定任何内容,则该页面被视为文档页面。 - -```yaml ---- -layout: doc ---- -``` - -## Doc 布局 {#doc-layout} - -`doc` 是默认布局,它将整个 Markdown 内容样式化为“文档”外观。它的工作原理是将整个内容包装在 `vp-doc` css 类中,并将样式应用于它下面的元素。 - -几乎所有通用元素,例如 `p` 或 `h2` 都具有特殊样式。 因此,请记住,如果你在 Markdown 内容中添加任何自定义 HTML,这些元素也会受到这些样式的影响。 - -同时还提供下面列出的文档特定功能。这些功能仅在此布局中生效。 - -- 编辑链接 -- 上一页/下一页链接 -- 概述 -- [Carbon Ads](./theme-carbon-ads) - -## Page 布局 {#page-layout} - -选项 `page` 被视为“空白页”。 Markdown 仍然会被解析,并且所有 [Markdown 扩展](./markdown)与 `doc` 布局同样生效,但它不会有任何默认样式。 - -page 布局可在 VitePress 主题不会影响标签的情况下让你自行设计所有内容。当你要创建自己的自定义页面时,这很有用。 - -注意,即使在此布局中,如果页面具有匹配的侧边栏配置,侧边栏仍会显示。 - -## Home 布局 {#home-layout} - -选项 `home` 将生成模板化的“主页”。 在此布局中,你可以设置额外的选项,例如 `hero` 和 `features`,以进一步自定义内容。请访问[主题:主页](./theme-home-page)了解更多详情。 - -## No 布局 {#no-layout} -如果你不想要任何布局,你可以通过在 frontmatter 中设置 `layout: false`。如果你想要一个完全可定制的登录页面 (默认情况下没有任何侧边栏、导航栏或页脚),这个选项很有用。 \ No newline at end of file diff --git a/docs/zh/guide/theme-sidebar.md b/docs/zh/guide/theme-sidebar.md deleted file mode 100644 index cf686e3b..00000000 --- a/docs/zh/guide/theme-sidebar.md +++ /dev/null @@ -1,161 +0,0 @@ -# 侧边栏 {#sidebar} - -侧边栏是文档的主要导航块。可以在 `themeConfig.sidebar` 中配置侧边栏菜单。 - -```js -export default { - themeConfig: { - sidebar: [ - { - text: 'Guide', - items: [ - { text: 'Introduction', link: '/introduction' }, - { text: 'Getting Started', link: '/getting-started' }, - ... - ] - } - ] - } -} -``` - -## 基本使用 {#the-basics} - -侧边栏菜单的最简单形式是传入一个链接数组。第一级项目定义了侧边栏部分。它应该包含 `text`,即该部分的标题,以及 `items`,即实际的导航链接。 - -```js -export default { - themeConfig: { - sidebar: [ - { - text: 'Section Title A', - items: [ - { text: 'Item A', link: '/item-a' }, - { text: 'Item B', link: '/item-b' }, - ... - ] - }, - { - text: 'Section Title B', - items: [ - { text: 'Item C', link: '/item-c' }, - { text: 'Item D', link: '/item-d' }, - ... - ] - } - ] - } -} -``` - -每个 `link` 都应该指定以 `/` 开头的实际文件的路径。如果在链接末尾添加斜杠,它将显示相应目录的`index.md`。 - -```js -export default { - themeConfig: { - sidebar: [ - { - text: 'Guide', - items: [ - // This shows `/guide/index.md` page. - { text: 'Introduction', link: '/guide/' } - ] - } - ] - } -} -``` - -## 多个侧边栏 {#multiple-sidebars} - -你可能会根据页面路径显示不同的侧边栏。例如,如本站点所示,你可能希望在文档中创建单独的内容部分,例如“指南”页面和“配置”页面。 - -为此,首先将你的页面放到所在的目录中: - -``` -. -├─ guide/ -│ ├─ index.md -│ ├─ one.md -│ └─ two.md -└─ config/ - ├─ index.md - ├─ three.md - └─ four.md -``` - -然后,更新配置以定义每个部分的侧边栏,不同的是,这次配置的是一个对象而不是数组。 - -```js -export default { - themeConfig: { - sidebar: { - // 当用户在 `指南` 目录页面下将会展示这个侧边栏 - '/guide/': [ - { - text: 'Guide', - items: [ - // This shows `/guide/index.md` page. - { text: 'Index', link: '/guide/' }, // /guide/index.md - { text: 'One', link: '/guide/one' }, // /guide/one.md - { text: 'Two', link: '/guide/two' } // /guide/two.md - ] - } - ], - - // 当用户在 `配置` 目录页面下将会展示这个侧边栏 - '/config/': [ - { - text: 'Config', - items: [ - // This shows `/config/index.md` page. - { text: 'Index', link: '/config/' }, // /config/index.md - { text: 'Three', link: '/config/three' }, // /config/three.md - { text: 'Four', link: '/config/four' } // /config/four.md - ] - } - ] - } - } -} -``` - -## 可折叠的侧边栏组 {#collapsible-sidebar-groups} - -通过向侧边栏组添加 `collapsible` 选项,它会显示一个切换按钮来隐藏或显示子菜单。 - -```js -export default { - themeConfig: { - sidebar: [ - { - text: 'Section Title A', - collapsible: true, - items: [...] - }, - { - text: 'Section Title B', - collapsible: true, - items: [...] - } - ] - } -} -``` - -默认情况下,所有侧边栏都是展开的。如果你希望它们在初始页面加载时关闭,请将 `collapsed` 选项设置为 `true`。 - -```js -export default { - themeConfig: { - sidebar: [ - { - text: 'Section Title A', - collapsible: true, - collapsed: true, - items: [...] - } - ] - } -} -``` diff --git a/docs/zh/guide/using-vue.md b/docs/zh/guide/using-vue.md index 573741a3..734ebd54 100644 --- a/docs/zh/guide/using-vue.md +++ b/docs/zh/guide/using-vue.md @@ -1,44 +1,75 @@ -# Markdown 中使用 Vue {#using-vue-in-markdown} +# Using Vue in Markdown -在 VitePress 中,每个 markdown 文件都被编译成 HTML,然后其作为 Vue 单文件组件处理。这意味着你可以在 markdown 中使用所有的 Vue 功能,包括动态模板、Vue 组件或通过添加 ` + +## Markdown Content + +The count is: {{ count }} + + + + +``` + +:::warning Avoid ` ``` -## 脚本和样式提升 {#script-style-hoisting} - -有时,你可以只想在当前页面应用一些 JavaScript 或者 CSS,在这种情况下,你可以直接在 Markdown 文件中使用原生的 ` - -## 内置的组件 {#built-in-components} - -VitePress 提供了内置的 Vue 组件,例如 `ClientOnly` 和 `OutboundLink`,查看[全局组件指南](/api/) 了解更多信息。 - -**参见:** +## Browser API Access Restrictions -- [在标题中使用组件](#using-components-in-headers) +Because VitePress applications are server-rendered in Node.js when generating static builds, any Vue usage must conform to the [universal code requirements](https://vuejs.org/guide/scaling-up/ssr.html). In short, make sure to only access Browser / DOM APIs in `beforeMount` or `mounted` hooks. -## 浏览器 API 的访问限制 {#browser-api-access-restrictions} - -由于 VitePress 应用在生成静态构建时在 Node.js 中进行服务器渲染,因此任何 Vue 使用都必须符合[通用代码要求](https://vuejs.org/guide/scaling-up/ssr.html)。 简而言之,确保只在 `beforeMount` 或 `mounted` 钩子中访问浏览器以及 DOM API。 - -如果你正在使用不支持 SSR 的组件 (例如,包含自定义指令),你可以将它们包装在 `ClientOnly` 组件中。 +If you are using or demoing components that are not SSR-friendly (for example, contain custom directives), you can wrap them inside the built-in `` component: ```md @@ -215,7 +228,7 @@ VitePress 提供了内置的 Vue 组件,例如 `ClientOnly` 和 `OutboundLink` ``` -注意,这不会修复**在导入时**访问浏览器 API 的组件或库。 要在导入时使用浏览器环境的代码,你需要在适当的生命周期挂钩中动态导入它们: +Note this does not fix components or libraries that access Browser APIs **on import**. To use code that assumes a browser environment on import, you need to dynamically import them in proper lifecycle hooks: ```vue ``` -如果要使用 `export default` 导出的Vue 组件,你可以这样子动态注册: +If your module `export default` a Vue component, you can register it dynamically: ```vue