Skip to main content

Command Palette

Search for a command to run...

VuePress and Docusaurus-Building Efficient Documentation Sites

Updated
6 min read
VuePress and Docusaurus-Building Efficient Documentation Sites

VuePress and Docusaurus are highly popular open-source static site generators, particularly well-suited for building technical documentation and knowledge bases. Both offer attractive default themes, easy-to-use Markdown syntax support, and automatically generated navigation and sidebars.

VuePress

1. Introduction

VuePress is a lightweight static site generator developed by Evan You, the creator of Vue.js. It leverages Vue.js's component system and Markdown rendering to create documentation sites.

2. Key Features

  • Markdown Support: VuePress includes enhanced Markdown support, such as MathJax and Mermaid diagrams.

  • Vue-Driven: All pages are Vue components, allowing for custom templates and logic.

  • Themes and Plugins: A rich ecosystem of themes and plugins enables high customization.

  • Instant Preview: Changes during local development are instantly reflected in the browser.

3. Code Example: Creating a Basic VuePress Project

# Install VuePress
npm install -g vuepress

# Initialize project
vuepress init my-docs

# Run local server
cd my-docs
vuepress dev

Markdown files in the docs folder can include Vue components and custom Vue code.

Docusaurus

1. Introduction

Docusaurus is a static site generator developed by Facebook, primarily used for building documentation sites for open-source projects.

2. Key Features

  • React-Based: Built with React, offering high performance and scalability.

  • Multilingual Support: Built-in support for language switching.

  • Version Management: Easily manage different versions of documentation.

  • Community Plugins: Extensive community plugins, such as Algolia search integration.

3. Code Example: Initializing a Docusaurus Project

# Install Docusaurus CLI
npx @docusaurus/init@latest init my-website classic

# Start local development server
cd my-website
yarn start

Docusaurus Markdown files support YAML front matter for configuring page metadata, for example:

---
id: guide-getting-started
title: Getting Started
---

# Welcome to Docusaurus!

This is a sample page.

Comparative Analysis

  • Learning Curve: VuePress is more approachable for developers familiar with Vue, while Docusaurus is more intuitive for React developers.

  • Customizability: VuePress offers more low-level control, while Docusaurus focuses on an out-of-the-box experience.

  • Community Support: Both have active communities, but VuePress may benefit from the broader Vue.js community.

  • Feature Set: Docusaurus’s version management and multilingual support may be critical for certain projects.

The choice depends on your specific needs, team skills, and preferences.

VuePress Custom Theme

VuePress allows you to extend functionality by creating custom themes. Below are the steps to create a simple VuePress theme:

  1. Create a new folder named my-theme to store the custom theme.

  2. Create index.js in my-theme to define the theme:

// my-theme/index.js
module.exports = (options, ctx) => ({
  name: 'my-theme', // Theme name
  enhanceAppFiles: ['enhanceApp.js'], // Add application enhancement files
  layouts: { // Custom layouts
    Layout: path.resolve(__dirname, 'layouts/Layout.vue')
  },
  clientAppEnhanceFiles: ['clientAppEnhance.js'], // Client application enhancement files
});
  1. In my-theme/layouts/Layout.vue, write a Vue component as the main layout template:
<template>
  <div id="app">
    <Header />
    <main class="theme-default-content">
      <nuxt /> <!-- Replaced by Markdown file content -->
    </main>
    <Footer />
  </div>
</template>

<script>
import Header from './components/Header.vue';
import Footer from './components/Footer.vue';

export default {
  components: {
    Header,
    Footer,
  },
};
</script>
  1. Configure the custom theme in my-docs/config.js:
module.exports = {
  theme: 'my-theme', // Path to custom theme
  themeConfig: {
    // Theme configuration
  },
};
  1. Add a my-theme/components folder and create Header.vue and Footer.vue to customize the header and footer.

Docusaurus Feature Extension: Algolia

Docusaurus provides built-in Algolia search integration. Here’s how to configure it:

  1. Add Algolia configuration in docusaurus.config.js:
module.exports = {
  // ...
  plugins: [
    [
      '@docusaurus/plugin-search-algolia',
      {
        algolia: {
          apiKey: 'your-api-key',
          indexName: 'your-index-name',
        },
      },
    ],
  ],
};
  1. Replace apiKey and indexName with your Algolia application’s values.

  2. Rebuild your Docusaurus site to apply changes:

yarn build

Your Docusaurus site should now have a fully functional search bar powered by Algolia.

Both VuePress and Docusaurus provide powerful tools for building efficient documentation sites. The choice depends on your project requirements, team’s technical stack, and customization needs. VuePress is ideal for projects requiring extensive customization and Vue integration, while Docusaurus is perfect for simpler setups and React ecosystem integration.

VuePress Plugin Development and Usage

VuePress’s strength lies in its rich plugin system, allowing users to extend functionality as needed. Below is a simplified example of developing and using a VuePress plugin.

Developing a Simple VuePress Plugin

Suppose we want to create a plugin that adds copyright information to the bottom of each page.

Create Plugin Directory Structure

Create a directory named vuepress-plugin-copyright, then create an index.js file as the plugin’s entry point.

Write Plugin Code

In index.js, define the plugin’s behavior using VuePress lifecycle hooks to inject copyright information:

// vuepress-plugin-copyright/index.js
const copyrightPlugin = (options, app) => {
  return {
    name: 'vuepress-plugin-copyright',
    async clientDynamicModules() {
      return {
        name: 'copyright.js',
        content: `
          export const copyright = "${options.text}";
        `,
      };
    },
    extendPageData($page) {
      $page.copyright = options.text;
    },
  };
};

module.exports = copyrightPlugin;

Publish and Install Plugin

While we won’t publish to npm for simplicity, you can typically publish the plugin via npm. In a development environment, link to the plugin directory locally.

Using the Custom Plugin in a VuePress Project

Install Plugin

For a locally developed plugin, use npm link or reference the local path directly in package.json.

Configure VuePress

In the VuePress project’s config.js, add the plugin configuration:

module.exports = {
  plugins: [
    [
      'vuepress-plugin-copyright',
      {
        text: 'Copyright © 2023 Your Company. All rights reserved.',
      },
    ],
  ],
};

Use Plugin Effect

In your VuePress theme (typically Layout.vue), access $page.copyright to display the copyright information:

<template>
  <!-- ... -->
  <footer>
    <p>{{ $page.copyright }}</p>
  </footer>
  <!-- ... -->
</template>

Docusaurus Plugin and Theme Development

Docusaurus also supports plugin and custom theme development, with a focus on configuration rather than direct code modification. In most cases, you can achieve your goals by configuring existing plugins or themes.

Custom Docusaurus Theme

While directly modifying a theme’s JavaScript and CSS files is possible, the recommended approach is to extend an existing theme and override specific parts.

Create Theme

Create a src/theme folder in your Docusaurus project root to house your custom theme.

Override Components

For example, to customize the footer, create a new Footer.js component in src/theme/Footer.js:

// src/theme/Footer.js
import React from 'react';

function Footer() {
  return (
    <footer>
      <div className="footer__copyright">
        © 2023 Your Company. All rights reserved.
      </div>
    </footer>
  );
}

export default Footer;

Configure Custom Theme

In docusaurus.config.js, ensure the site configuration points to the correct theme path:

module.exports = {
  // ...
  theme: path.resolve(__dirname, 'src/theme'),
  // ...
};

Both VuePress and Docusaurus offer flexible mechanisms for extending and customizing documentation sites. VuePress provides high flexibility through direct Vue plugin development, ideal for scenarios requiring deep customization. Docusaurus simplifies the process with a configuration-first approach, making it suitable for quickly setting up and maintaining large documentation projects. Choosing the right tool and approach can significantly enhance documentation development efficiency and end-user experience.

Web Development

Part 24 of 46

The content covers the three basics of HTML/CSS/JS/TS, modular development, mainstream frameworks (Vue, React, Angular, Svelte), build tools, browser plug-in development, Node.js backend practice, Next.js/Nest.js and other popular technologies.

Up next

Prettier and ESLint-Automating Code Style and Quality

Prettier and ESLint are complementary tools that together ensure code style consistency and quality. Prettier handles code formatting, while ESLint performs more complex static analysis and rule checking. Prettier Purpose Automates code formatting, ...

More from this blog

T

Tianya School Technical Articles

48 posts

Welcome to our tech publication, where we share high-quality content on full-stack development, frontend/backend/web3/AI, and engineering best practices.