Skip to content

Commit 3ce4d84

Browse files
committed
docs: add extending-a-theme recipe (close #104)
1 parent fbb7538 commit 3ce4d84

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed

Diff for: docs/.vuepress/configs/sidebar/en.ts

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const en: SidebarConfig = {
3838
'/advanced/cookbook/README.md',
3939
'/advanced/cookbook/usage-of-client-app-enhance.md',
4040
'/advanced/cookbook/adding-extra-pages.md',
41+
'/advanced/cookbook/extending-a-theme.md',
4142
'/advanced/cookbook/markdown-and-vue-sfc.md',
4243
],
4344
},

Diff for: docs/.vuepress/configs/sidebar/zh.ts

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const zh: SidebarConfig = {
3838
'/zh/advanced/cookbook/README.md',
3939
'/zh/advanced/cookbook/usage-of-client-app-enhance.md',
4040
'/zh/advanced/cookbook/adding-extra-pages.md',
41+
'/zh/advanced/cookbook/extending-a-theme.md',
4142
'/zh/advanced/cookbook/markdown-and-vue-sfc.md',
4243
],
4344
},
13.1 KB
Loading

Diff for: docs/advanced/cookbook/extending-a-theme.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Extending a Theme
2+
3+
Sometimes you might want make some minor changes to a theme, but you may not want to fork and modify the entire project.
4+
5+
With the help of [Theme API](../../reference/theme-api.md), you can extend a theme and make your own modifications:
6+
7+
```js
8+
module.exports = {
9+
// your theme
10+
name: 'vuepress-theme-foo',
11+
// parent theme to extend
12+
extends: 'vuepress-theme-bar',
13+
// override layouts of parent theme
14+
layouts: {
15+
Layout: path.resolve(__dirname, 'layouts/Layout.vue'),
16+
},
17+
}
18+
```
19+
20+
In this case, your `vuepress-theme-foo` will inherit all configuration, plugins and layouts from `vuepress-theme-bar`, and you can override corresponding layouts as needed.
21+
22+
## Extending Default Theme
23+
24+
First, create the theme directory and theme entry `.vuepress/theme/index.js`:
25+
26+
```js
27+
const { path } = require('@vuepress/utils')
28+
29+
module.exports = {
30+
name: 'vuepress-theme-local',
31+
extends: '@vuepress/theme-default',
32+
layouts: {
33+
Layout: path.resolve(__dirname, 'layouts/Layout.vue'),
34+
},
35+
}
36+
```
37+
38+
You local theme will extends default theme, and override the `Layout` layout.
39+
40+
Next, create `.vuepress/layouts/Layout.vue`, and make use of the slots that provided by the `Layout` of default theme:
41+
42+
```vue
43+
<template>
44+
<Layout>
45+
<template #page-bottom>
46+
<div class="my-footer">This is my custom page footer</div>
47+
</template>
48+
</Layout>
49+
</template>
50+
51+
<script>
52+
import Layout from '@vuepress/theme-default/lib/client/layouts/Layout.vue'
53+
54+
export default {
55+
components: {
56+
Layout,
57+
},
58+
}
59+
</script>
60+
61+
<style lang="css">
62+
.my-footer {
63+
text-align: center;
64+
}
65+
</style>
66+
```
67+
68+
You will add a custom footer to every normal pages in default theme (excluding homepage):
69+
70+
![extending-a-theme](/images/cookbook/extending-a-theme-01.png)
71+
72+
Here are all the slots that provided by the `Layout` of default theme:
73+
74+
- `navbar-before`
75+
- `navbar-after`
76+
- `sidebar-top`
77+
- `sidebar-bottom`
78+
- `page-top`
79+
- `page-bottom`

Diff for: docs/zh/advanced/cookbook/extending-a-theme.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 继承一个主题
2+
3+
有时你可能希望对一个主题进行一些小改动,但是又不想 Fork 并修改整个项目。
4+
5+
借助于 [主题 API](../../reference/theme-api.md) ,你可以继承一个主题并添加你自己的改动:
6+
7+
```ts
8+
export default {
9+
// 你的主题
10+
name: 'vuepress-theme-foo',
11+
// 要继承的父主题
12+
extends: 'vuepress-theme-bar',
13+
// 覆盖父主题的布局
14+
layouts: {
15+
Layout: path.resolve(__dirname, 'layouts/Layout.vue'),
16+
},
17+
};
18+
```
19+
20+
在这个例子中,你的 `vuepress-theme-foo` 将会继承 `vuepress-theme-bar` 的全部配置、插件和布局,并且你可以按照需要来覆盖对应的布局。
21+
22+
## 继承默认主题
23+
24+
首先,创建主题目录和主题入口 `.vuepress/theme/index.js`
25+
26+
```js
27+
const { path } = require('@vuepress/utils')
28+
29+
module.exports = {
30+
name: 'vuepress-theme-local',
31+
extends: '@vuepress/theme-default',
32+
layouts: {
33+
Layout: path.resolve(__dirname, 'layouts/Layout.vue'),
34+
},
35+
}
36+
```
37+
38+
你的本地主题将会继承默认主题,并且覆盖 `Layout` 布局。
39+
40+
接下来,创建 `.vuepress/layouts/Layout.vue` ,并使用由默认主题的 `Layout` 提供的插槽:
41+
42+
```vue
43+
<template>
44+
<Layout>
45+
<template #page-bottom>
46+
<div class="my-footer">This is my custom page footer</div>
47+
</template>
48+
</Layout>
49+
</template>
50+
51+
<script>
52+
import Layout from '@vuepress/theme-default/lib/client/layouts/Layout.vue'
53+
54+
export default {
55+
components: {
56+
Layout,
57+
},
58+
}
59+
</script>
60+
61+
<style lang="css">
62+
.my-footer {
63+
text-align: center;
64+
}
65+
</style>
66+
```
67+
68+
你将会在除了首页外的所有页面添加一个自定义的页脚:
69+
70+
![extending-a-theme](/images/cookbook/extending-a-theme-01.png)
71+
72+
下面列出了默认主题的 `Layout` 所提供的所有插槽:
73+
74+
- `navbar-before`
75+
- `navbar-after`
76+
- `sidebar-top`
77+
- `sidebar-bottom`
78+
- `page-top`
79+
- `page-bottom`

0 commit comments

Comments
 (0)