Writing a theme

To write a theme, create a .vuepress/theme directory in your docs root, and then create a Layout.vue file:

  1. .

  2. └─ .vuepress

  3. └─ theme

  4. └─ Layout.vue

From there it’s the same as developing a normal Vue application. It’s entirely up to you how to organize your theme.

Content Outlet

The compiled content of the current .md file being rendered will be available as a special <Content/> global component. You will need to render it somewhere in your layout to display the content of the page. The simplest theme can be a single Layout.vue component with the following content:

  1. <template>
  2. <div class="theme-container">
  3. <Content/>
  4. </div>
  5. </template>

Also see:

Directory Structure

One Layout.vue might not be enough, and you might also want to define more layout components in the theme for using on different pages. You may also want to customize the palette, and even apply some plugins.

So it’s time to reorganize your theme, an agreed theme directory structure is as follows:

  1. theme

  2. ├── global-components

  3. └── xxx.vue

  4. ├── components

  5. └── xxx.vue

  6. ├── layouts

  7. ├── Layout.vue (Mandatory)

  8. └── 404.vue

  9. ├── styles

  10. ├── index.styl

  11. └── palette.styl

  12. ├── templates

  13. ├── dev.html

  14. └── ssr.html

  15. ├── index.js

  16. ├── enhanceApp.js

  17. └── package.json

  • theme/global-components: Components under this directory will be automatically registered as global components. For details, please check out @vuepress/plugin-register-componentsWriting a theme - 图1.
  • theme/components: Your components.
  • theme/layouts: Layout components of the theme, where Layout.vue is required.
  • theme/styles: Global style and palette.
  • theme/templates: Edit default template.
  • theme/index.js: Entry file of theme configuration.
  • theme/enhanceApp.js: Theme level enhancements.

Note

When you publish your theme as an npm package, if you don’t have any theme configuration, that means you don’t have theme/index.js, you’ll need to set the "main" field to layouts/Layout.vue in package.json, only in this way VuePress can properly resolve the theme.

  1. {
  2. ...
  3. "main": "layouts/Layout.vue",
  4. ...
  5. }

Layout Component

Suppose your theme layouts folder is as follows:

  1. theme

  2. └── layouts

  3. ├── Layout.vue

  4. ├── AnotherLayout.vue

  5. └── 404.vue

Then, all the pages will use Layout.vue as layout component by default, while the routes not matching will use 404.vue.

To switch the layout of some pages to AnotherLayout.vue, all you have to do is update the frontmatter of this page:

  1. ---
  2. layout: AnotherLayout
  3. ---

TIP

Each layout component may render distinct pages. To apply some global UI (for example global header), consider using globalLayout

Apply plugins

You can apply some plugins to the theme via theme/index.js.

  1. module.exports = {
  2. plugins: [
  3. ['@vuepress/pwa', {
  4. serviceWorker: true,
  5. updatePopup: true
  6. }]
  7. ]
  8. }

Site and Page Metadata

The Layout component will be invoked once for every .md file in docs, and the metadata for the entire site and that specific page will be exposed respectively as this.$site and this.$page properties which are injected into every component in the app.

This is the value of $site of this website:

  1. {
  2. "title": "VuePress",
  3. "description": "Vue-powered Static Site Generator",
  4. "base": "/",
  5. "pages": [
  6. {
  7. "lastUpdated": 1524027677000,
  8. "path": "/",
  9. "title": "VuePress",
  10. "frontmatter": {}
  11. },
  12. ...
  13. ]
  14. }

title, description and base are copied from respective fields in .vuepress/config.js. pages contains an array of metadata objects for each page, including its path, page title (explicitly specified in YAML frontmatter or inferred from the first header on the page), and any YAML frontmatter data in that file.

This is the $page object for this page you are looking at:

  1. {
  2. "lastUpdated": 1524847549000,
  3. "path": "/guide/custom-themes.html",
  4. "title": "Custom Themes",
  5. "headers": [/* ... */],
  6. "frontmatter": {}
  7. }

If the user provided themeConfig in .vuepress/config.js, it will also be available as $site.themeConfig. You can use this to allow users to customize behavior of your theme - for example, specifying categories and page order. You can then use these data together with $site.pages to dynamically construct navigation links.

Don’t forget that this.$route and this.$router are also available as part of Vue Router’s API.

TIP

lastUpdated is the UNIX timestamp of this file’s last git commit, for more details, check out Last Updated.

Content Excerpt

If a Markdown file contains a <!— more —> comment, any content above the comment will be extracted and exposed as $page.excerpt. If you are building custom theme for blogging, this data can be used to render a post list with excerpts.

App Level Enhancements

Themes can enhance the Vue app that VuePress uses by exposing an enhanceApp.js file at the root of the theme. The file should export default a hook function which will receive an object containing some app-level values. You can use this hook to install more Vue plugins, register global components, or add router hooks:

  1. export default ({
  2. Vue, // the version of Vue being used in the VuePress app
  3. options, // the options for the root Vue instance
  4. router, // the router instance for the app
  5. siteData // site metadata
  6. }) => {
  7. // ...apply enhancements to the app
  8. }