You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/docs/docs/plugin/README.md
+90-41
Original file line number
Diff line number
Diff line change
@@ -4,16 +4,16 @@ sidebar: auto
4
4
5
5
# Plugins
6
6
7
-
## Writing a Plugin
8
-
9
7
Plugins usually add global-level functionality to VuePress. There is no strictly defined scope for a plugin - there are typically several types of plugins:
10
8
11
9
1. Extend the data generated at compile time. e.g. [@vuepress/plugin-last-updated](https://github.com/vuejs/vuepress/tree/master/packages/@vuepress/plugin-last-updated).
12
10
2. Generate extra files before or after compilation. e.g. [@vuepress/plugin-pwa](https://github.com/vuejs/vuepress/tree/master/packages/%40vuepress/plugin-pwa)
13
11
3. Add extra pages. e.g. [@vuepress/plugin-i18n-ui](https://github.com/vuejs/vuepress/tree/master/packages/@vuepress/plugin-i18n-ui)
14
12
4. Inject global UI. e.g. [@vuepress/plugin-back-to-top](https://github.com/vuejs/vuepress/tree/master/packages/%40vuepress/plugin-back-to-top).
15
13
16
-
A plugin should export a `plain object`(`#1`). If the plugin needs to take options, it can be a function that exports a plain object(`#2`). The function will be called with the plugin's options as the first argument, along with [context](#plugin-context) which provides some compile-time metadata.
14
+
## Writing a Plugin
15
+
16
+
A plugin should export a `plain JavaScript object`(`#1`). If the plugin needs to take options, it can be a function that exports a plain object(`#2`). The function will be called with the plugin's options as the first argument, along with [ctx](#ctx) which provides some compile-time metadata.
We can set aliases via [chainWebpack](chainwebpack):
333
+
We can set aliases via [chainWebpack](#chainwebpack):
285
334
286
335
```js
287
336
module.exports= (options, ctx) => ({
@@ -417,7 +466,7 @@ The file can `export default` a hook function which will work like `.vuepress/en
417
466
It's worth mentioning that in order for plugin developers to be able to do more things at compile time, this option also supports dynamic code:
418
467
419
468
```js
420
-
module.exports= (option, context) => {
469
+
module.exports= (option, ctx) => {
421
470
return {
422
471
enhanceAppFiles: [{
423
472
name:'dynamic-code',
@@ -456,14 +505,14 @@ import { SOURCE_DIR } from '@dynamic/constans'
456
505
- Type: `Function`
457
506
- Default: `undefined`
458
507
459
-
A function used to extend or modify the [$page](../miscellaneous/global-computed.md#page) object. This function will be invoking once for each page at compile time
508
+
A function used to extend or modify the [$page](../miscellaneous/global-computed.md#page) object. This function will be invoking once for each page at compile time.
460
509
461
510
```js
462
511
module.exports= {
463
512
extendPageData ($page) {
464
513
const {
465
514
_filePath, // file's absolute path
466
-
_i18n, // access the client global mixins at build time, e.g _i18n.$localePath.
515
+
_computed, // access the client global computed mixins at build time, e.g _computed.$localePath.
467
516
_content, // file's raw content string
468
517
_strippedContent, // file's content string without frontmatter
469
518
key, // page's unique hash key
@@ -481,11 +530,11 @@ module.exports = {
481
530
}
482
531
```
483
532
484
-
::: warning Note
485
-
These fields starting with an `_`means you can only access them during build time.
533
+
::: warning 注意
534
+
那些以 `_`开头的字段意味着你只能在编译期访问。
486
535
:::
487
536
488
-
e.g.
537
+
例子:
489
538
490
539
```js
491
540
module.exports= {
@@ -495,17 +544,17 @@ module.exports = {
495
544
}
496
545
```
497
546
498
-
Then you can use this value via `this.$page.size`in any Vue component.
547
+
然后你可以在任意的 Vue 中通过 `this.$page.size`来访问这个变量。
499
548
500
549
### clientRootMixin
501
550
502
-
-Type: `String`
503
-
-Default: `undefined`
551
+
-类型: `String`
552
+
-默认值: `undefined`
504
553
505
-
A path to the mixin file which allow you to control the life cycle of root component.
554
+
指向 `mixin` 文件的路径,它让你你可以控制根组件的生命周期:
506
555
507
556
```js
508
-
//plugin's entry
557
+
//插件的入口
509
558
constpath=require('path')
510
559
511
560
module.exports= {
@@ -523,10 +572,10 @@ export default {
523
572
524
573
### additionalPages
525
574
526
-
-Type: `Array|Function`
527
-
-Default: `undefined`
575
+
-类型: `Array|Function`
576
+
-默认值: `undefined`
528
577
529
-
Add a page pointing to a markdown file:
578
+
增加一个指向某个 markdown 文件的页面:
530
579
531
580
```js
532
581
constpath=require('path')
@@ -541,7 +590,7 @@ module.exports = {
541
590
}
542
591
```
543
592
544
-
Add a page with explicit content:
593
+
或增加一个具有明确内容的页面:
545
594
546
595
```js
547
596
module.exports= {
@@ -561,7 +610,7 @@ module.exports = {
561
610
}
562
611
```
563
612
564
-
Add a pure route:
613
+
或增加一个纯粹的路由:
565
614
566
615
```js
567
616
module.exports= {
@@ -578,10 +627,10 @@ module.exports = {
578
627
579
628
### globalUIComponents
580
629
581
-
-Type: `Array|String`
582
-
-Default: `undefined`
630
+
-类型: `Array|String`
631
+
-默认值: `undefined`
583
632
584
-
You might want to inject some global UI fixed somewhere on the page, e.g. `back-to-top`, `popup`. In VuePress, **a global UI is a Vue component**, you can define the component's name(s) in the plugin, e.g.
Starting with VuePress 1.x.x, VuePress provides an `AppContext` object that stores all the state of the current app and can be accessed through the plugin API.
610
659
@@ -613,48 +662,48 @@ Context of each plugin is a isolated context, they just inherit from the same ap
613
662
:::
614
663
615
664
```js
616
-
module.exports= (options, context) => {
665
+
module.exports= (options, ctx) => {
617
666
// ...
618
667
}
619
668
```
620
669
621
-
### context.isProd
670
+
### ctx.isProd
622
671
623
672
- Type: `boolean`
624
673
625
674
Whether vuepress run in production environment mode.
626
675
627
-
### context.sourceDir
676
+
### ctx.sourceDir
628
677
629
678
- Type: `string`
630
679
631
680
Root directory where the documents are located.
632
681
633
-
### context.tempPath
682
+
### ctx.tempPath
634
683
635
684
- Type: `string`
636
685
637
686
Root directory where the temporary files are located.
0 commit comments