|
1 | | -# Blog |
| 1 | +--- |
| 2 | +sidebar: auto |
| 3 | +--- |
2 | 4 |
|
| 5 | +# @vuepress/plugin-blog |
| 6 | + |
| 7 | +> Official blog theme for VuePress. |
| 8 | +
|
| 9 | +Note that this plug-in has been deeply integrated into [@vuepress/theme-blog](https://github.com/ulivz/vuepress-theme-blog). |
| 10 | + |
| 11 | +## Install |
| 12 | + |
| 13 | +```bash |
| 14 | +yarn add -D @vuepress/plugin-blog |
| 15 | +# OR npm install -D @vuepress/plugin-blog |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +```javascript |
| 21 | +module.exports = { |
| 22 | + plugins: ['@vuepress/blog', { /* options */ }] |
| 23 | + // Please keep looking down to see the available options. |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +## Tutorials |
| 28 | + |
| 29 | +### Classifier |
| 30 | + |
| 31 | +This plugin creates a concept named `classifier`. All files in a directory (e.g. `_post`) should be the |
| 32 | +same classification, all pages containing the same specific frontmatter key value (e.g. `tag: js`) can also be a |
| 33 | +classification. |
| 34 | + |
| 35 | +So there are two different types of classification. Of course, both of them may be related to another common |
| 36 | +requirement, `pagination`. Next, let's take a look at the benefits of this plugin from the shallow to the deep. |
| 37 | + |
| 38 | + |
| 39 | +### Directory Classifier |
| 40 | + |
| 41 | +Directory Classifier, that means the classification of source files in a same directory. |
| 42 | + |
| 43 | +Suppose you have the following files structure: |
| 44 | + |
| 45 | +``` vue |
| 46 | +. |
| 47 | +└── _posts |
| 48 | + ├── 2018-4-4-intro-to-vuepress.md |
| 49 | + └── 2019-6-8-intro-to-vuepress-next.md |
| 50 | +``` |
| 51 | + |
| 52 | +In the traditional VuePress site, the converted page URLs will be: |
| 53 | + |
| 54 | +- `_posts/2018-4-4-intro-to-vuepress.html` |
| 55 | +- `_posts/2019-6-8-intro-to-vuepress-next.html` |
| 56 | + |
| 57 | +It doesn't seem blogging, so it's time to use this plugin: |
| 58 | + |
| 59 | +```js |
| 60 | +// .vuepress/config.js |
| 61 | +module.exports = { |
| 62 | + plugins: [ |
| 63 | + ['@vuepress/blog', { |
| 64 | + directories: [ |
| 65 | + { |
| 66 | + // Unique ID of current classification |
| 67 | + id: 'post', |
| 68 | + // Target directory |
| 69 | + dirname: '_posts', |
| 70 | + // Path of the `entry page` (or `list page`) |
| 71 | + path: '/', |
| 72 | + }, |
| 73 | + ], |
| 74 | + }] |
| 75 | + ] |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +Then the plugin will help you to generate following pages, and automatically leverage corresponding |
| 80 | +layout: |
| 81 | + |
| 82 | +| url | layout | |
| 83 | +|---|---| |
| 84 | +| `/` | `IndexPost` | |
| 85 | +| `/2018/04/04/intro-to-vuepress/` | `Post` | |
| 86 | +| `/2019/06/08/intro-to-vuepress-next/` | `Post` | |
| 87 | + |
| 88 | +This means that you need to create two layout components(`IndexPost` and `Post`) to handle the layout of index and post |
| 89 | +pages. |
| 90 | + |
| 91 | +You can also custom the layout component name: |
| 92 | + |
| 93 | +```diff |
| 94 | +// .vuepress/config.js |
| 95 | +module.exports = { |
| 96 | + plugins: [ |
| 97 | + ['@vuepress/blog', { |
| 98 | + directories: [ |
| 99 | + { |
| 100 | + id: 'post', |
| 101 | + dirname: '_posts', |
| 102 | + path: '/', |
| 103 | ++ layout: 'MyIndexPost', |
| 104 | ++ itemLayout: 'MyPost', |
| 105 | + }, |
| 106 | + ], |
| 107 | + }] |
| 108 | + ] |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +And custom the root path and the permalink: |
| 113 | + |
| 114 | +```diff |
| 115 | +// .vuepress/config.js |
| 116 | +module.exports = { |
| 117 | + plugins: [ |
| 118 | + ['@vuepress/blog', { |
| 119 | + directories: [ |
| 120 | + { |
| 121 | + id: 'post', |
| 122 | + dirname: '_posts', |
| 123 | +- path: '/', |
| 124 | ++ path: '/post/', |
| 125 | ++ itemPermalink: '/post/:year/:month/:day/:slug', |
| 126 | + }, |
| 127 | + ], |
| 128 | + }] |
| 129 | + ] |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +::: warning |
| 134 | +It is noteworthy that the `path` and `itemPermalink` must be uniformly modified, and `itemPermalink` must be prefixed with |
| 135 | +`path`. |
| 136 | + |
| 137 | +The default value of `itemPermalink` is `'/:year/:month/:day/:slug'`. |
| 138 | +::: |
| 139 | + |
| 140 | +### Pagination |
| 141 | + |
| 142 | +As your blog articles grew more and more, you began to have the need for paging. By default, this plugin integrates a |
| 143 | + very powerful pagination system that allows you to access paging functions with simple configuration. |
| 144 | + |
| 145 | +By default, the plugin assumes that the number of pages per page is `10`. you can also modify it like this: |
| 146 | + |
| 147 | +```diff |
| 148 | +// .vuepress/config.js |
| 149 | +module.exports = { |
| 150 | + plugins: [ |
| 151 | + ['@vuepress/blog', { |
| 152 | + directories: [ |
| 153 | + { |
| 154 | + id: 'post', |
| 155 | + dirname: '_posts', |
| 156 | + path: '/', |
| 157 | ++ pagination: { |
| 158 | ++ perPagePosts: 2, |
| 159 | ++ }, |
| 160 | + }, |
| 161 | + ], |
| 162 | + }] |
| 163 | + ] |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +Suppose you have 3 pages at `_posts` direcotry: |
| 168 | + |
| 169 | +- `_posts/2018-6-8-a.md` |
| 170 | +- `_posts/2019-6-8-b.md` |
| 171 | +- `_posts/2019-6-8-c.md` |
| 172 | + |
| 173 | +When the `perPagePosts` is set to `2`, this plugin will help you generate the following pages: |
| 174 | + |
| 175 | +| url | layout | |
| 176 | +|---|---| |
| 177 | +| `/` | `IndexPost / Layout` | |
| 178 | +| `/page/2/` (New) | `DirectoryPagination / Layout` | |
| 179 | +| `/2019/06/08/a/` | `Post` | |
| 180 | +| `/2019/06/08/b/` | `Post` | |
| 181 | +| `/2018/06/08/c/` | `Post` | |
| 182 | + |
| 183 | +So how to get the matched pages in the layout component? In fact, it will be much simpler than you think. |
| 184 | + |
| 185 | +If you visit `/`, current page leverages layout `IndexPost`, in this layout component, you just need to use `this |
| 186 | +.$pagination.pages` to get the matched pages, in the above example, the actual value of `this.$pagination.pages` will |
| 187 | + be: |
| 188 | + |
| 189 | +```json |
| 190 | +[ |
| 191 | + { "relativePath": "_posts/2019-6-8-a.md", "path": "/2019/06/08/a/" ... }, |
| 192 | + { "relativePath": "_posts/2019-6-8-b.md", "path": "/2019/06/08/b/" ... }, |
| 193 | +] |
| 194 | +``` |
| 195 | + |
| 196 | +If you visit `/`, current page leverages layout `DirectoryPagination`, you can also use `this |
| 197 | +.$pagination.pages` to access it: |
| 198 | + |
| 199 | +```json |
| 200 | +[ |
| 201 | + { "relativePath": "_posts/2019-6-8-c.md", "path": "/2019/06/08/c/" ... }, |
| 202 | +] |
| 203 | +``` |
| 204 | + |
| 205 | +Isn't a very natural experiences? You just need to care about the style of your layout component, not the complicated and boring logic behind it. |
| 206 | + |
| 207 | + |
| 208 | +::: tip |
| 209 | +To save the length of docs, we omitted the data structure of `$page` object. you can get more information about |
| 210 | +the data structure of `$page` at the [official documentation](https://v1.vuepress.vuejs.org/guide/global-computed.html#page). |
| 211 | +::: |
| 212 | + |
| 213 | + |
| 214 | +### Frontmatter Classifier |
| 215 | + |
| 216 | +Frontmatter Classifier, that is the classification of pages that have the same frontmatter key values. |
| 217 | + |
| 218 | +Suppose you have three pages: |
| 219 | + |
| 220 | +- `a.md`: |
| 221 | + |
| 222 | +```mardkown |
| 223 | +--- |
| 224 | +tag: vue |
| 225 | +--- |
| 226 | +``` |
| 227 | + |
| 228 | +- `b.md`: |
| 229 | + |
| 230 | +```mardkown |
| 231 | +--- |
| 232 | +tag: vue |
| 233 | +--- |
| 234 | +``` |
| 235 | + |
| 236 | +- `c.md`: |
| 237 | + |
| 238 | +```mardkown |
| 239 | +--- |
| 240 | +tag: js |
| 241 | +--- |
| 242 | +``` |
| 243 | + |
| 244 | +If you want to easily classify them, you can config like this: |
| 245 | + |
| 246 | +```js |
| 247 | +module.exports = { |
| 248 | + plugins: [ |
| 249 | + ['@vuepress/blog', { |
| 250 | + frontmatters: [ |
| 251 | + { |
| 252 | + // Unique ID of current classification |
| 253 | + id: "tag", |
| 254 | + // Decide that the frontmatter keys will be grouped under this classification |
| 255 | + keys: ['tag'], |
| 256 | + // Path of the `entry page` (or `list page`) |
| 257 | + path: '/tag/', |
| 258 | + // Layout of the `entry page` |
| 259 | + layout: 'Tag', |
| 260 | + }, |
| 261 | + ] |
| 262 | + }] |
| 263 | + ] |
| 264 | +} |
| 265 | +``` |
| 266 | + |
| 267 | +Then the plugin will help you to generate following extra pages, and automatically leverage corresponding |
| 268 | +layout: |
| 269 | + |
| 270 | +| url | layout | |
| 271 | +|---|---| |
| 272 | +| `/tag/` | `Tag` | |
| 273 | +| `/tag/vue/` | `FrontmatterClassifier` | |
| 274 | +| `/tag/js/` | `FrontmatterClassifier` | |
| 275 | + |
| 276 | +In `Tags` component, you can use `this.$tag.list` to get the tag list. the value would be like: |
| 277 | + |
| 278 | +```json |
| 279 | +[ |
| 280 | + { |
| 281 | + "name": "vue", |
| 282 | + "path": "/tag/vue/", |
| 283 | + "pages": [ |
| 284 | + { "relativePath": "b.md", "path": "/b.html" ... }, |
| 285 | + { "relativePath": "a.md", "path": "/a.html" ... }, |
| 286 | + ] |
| 287 | + }, |
| 288 | + { |
| 289 | + "name": "js", |
| 290 | + "path": "/tag/js/", |
| 291 | + "pages": [ |
| 292 | + { "relativePath": "c.md", "path": "/c.html" ... }, |
| 293 | + ] |
| 294 | + } |
| 295 | +] |
| 296 | +``` |
| 297 | + |
| 298 | +In `FrontmatterClassifier` component, you can use `this.$pagination.pages` to get the matched pages in current tag |
| 299 | +classification. And what is wonderful is that a you don't nned to card about where you are, and you'll get a very |
| 300 | +natural experiences: |
| 301 | + |
| 302 | +- If you visit `/tag/vue/`, the `this.$pagination.pages` will be: |
| 303 | + |
| 304 | +```json |
| 305 | +[ |
| 306 | + { "relativePath": "b.md", "path": "/b.html" ... }, |
| 307 | + { "relativePath": "a.md", "path": "/a.html" ... }, |
| 308 | +] |
| 309 | +``` |
| 310 | + |
| 311 | +- If you visit `/tag/js/`, the `this.$pagination.pages` will be: |
| 312 | + |
| 313 | +```json |
| 314 | +[ |
| 315 | + { "relativePath": "c.md", "path": "/c.html" ... }, |
| 316 | +] |
| 317 | +``` |
| 318 | + |
| 319 | + |
| 320 | + |
| 321 | + |
| 322 | +## Options |
| 323 | + |
| 324 | +### |
0 commit comments