Docsify Documentation

repository·develop·Indexed 12 days ago

https://github.com/docsifyjs/docsify

A lightweight documentation site generator that transforms Markdown files into a website dynamically in the browser without requiring a build step. Version 5.0.0 supports features like nested sidebars, custom key bindings, cover pages, and flexible CDN deployment via jsDelivr, cdnjs, and unpkg.

Tokens
37.8K
Snippets
144
Records
165
Agent score
97%

What's inside Docsify

  1. What is a Docsify plugin?

    develop

    A Docsify plugin is a function that allows you to execute custom JavaScript code at specific stages of the Docsify lifecycle. It receives two arguments: hook (an object containing lifecycle methods) and vm (the Docsify instance).

    function myPlugin(hook, vm) {
      // ...
    }
  2. What is docsify and how does it work?

    develop
    docsify is a documentation site generator that turns Markdown files into a website instantly. Unlike traditional static site generators, it does not require a build step to generate HTML files. Instead, it works by dynamically loading and parsing your Markdown files at runtime to display them as a website. To use it, you simply need an index.html file which acts as the entry point for the application.
  3. Separate the Coverpage from the Homepage

    develop
    By default, the coverpage and the homepage appear at the same time. If you want the coverpage to act as a standalone landing page and separate it from the main documentation content, use the onlyCover configuration option.
  4. Understand the Vue processing lifecycle in Docsify

    develop

    Docsify follows a specific order when processing Vue content on every page load:

    1. Execute markdown <script> tags: Any manual mounting logic in the file is run first.
    2. Register vueComponents: Global components defined in your config are registered.
    3. Mount vueMounts: Specific DOM elements targeted by selectors are mounted.
    4. Auto-mount components: Any unmounted vueComponents found in the Markdown are automatically mounted.
    5. Auto-mount global options: Any unmounted Vue template syntax or components are mounted using vueGlobalOptions.

    Key Behaviors:

    • Auto-mounting: Docsify mounts top-level elements containing template syntax or components (e.g., a <p> containing {{ foo }} or a <my-component />).
    • Avoid Conflicts: Docsify will not attempt to mount an element that already contains an existing Vue instance.
    • Cleanup: Docsify automatically destroys/unmounts all Vue instances it created before loading a new page to prevent memory leaks and conflicts.
  5. Configure embedded file types

    develop

    Docsify automatically recognizes file extensions to determine how to embed them:

    • iframe: .html, .htm
    • markdown: .markdown, .md
    • audio: .mp3
    • video: .mp4, .ogg
    • code: any other file extension

    You can force a specific type using the :type attribute. For example, to embed a Markdown file as a code block, use :type=code.

    [filename](_media/example.md ':include :type=code')
  6. Structure of a docsify project

    develop

    After running docsify init ./docs, the following files are created in the ./docs directory:

    • index.html: The entry file for the application.
    • README.md: The home page of your documentation.
    • .nojekyll: A file used to prevent GitHub Pages from ignoring files that begin with an underscore.

    You can update the home page by editing ./docs/README.md or add more pages to your site.

  7. Implement Nested Sidebars

    develop

    To have the sidebar update dynamically based on the current directory (e.g., showing sub-navigation when entering a folder), add a _sidebar.md file to each subdirectory. Docsify will load the _sidebar.md from the current directory level; if it doesn't exist, it falls back to the parent directory's sidebar.

    You can use the alias configuration to control this fallback behavior and avoid unnecessary parent sidebar loading.

    <script>
      window.$docsify = {
        loadSidebar: true,
        alias: {
          '/.*/_sidebar.md': '/_sidebar.md',
        },
      };
    </script>
  8. Deploy to Netlify with HTML5 Router

    develop

    To deploy to Netlify:

    1. Connect your GitHub repository in the Netlify dashboard.
    2. Set the Base Directory to your docs subfolder (e.g., docs).
    3. Leave the Build Command blank.
    4. The Publish directory will automatically populate as docs/.

    HTML5 Router Support: If you are using the HTML5 router, create a _redirects file in your docs directory to ensure all requests are redirected to index.html with a 200 status code.

    # Create _redirects in your docs directory
    /*    /index.html   200
  9. Upgrade Docsify from v4 to v5

    develop

    Upgrading from Docsify v4 to v5 primarily involves updating CDN URLs and theme files. Most configuration settings in window.$docsify and your Markdown content remain unchanged.

    Key changes to apply:

    1. CDN Path: Change from /lib/ to /dist/.
    2. Version: Update version specifiers from @4 to @5.
    3. Themes: v5 introduces a core theme. Legacy themes like buble, dark, or pure are replaced by the core theme, though specific styles (like Vue) are now available as add-ons.
    4. Browser Support: Note that legacy browsers like Internet Explorer 11 are no longer supported in v5.

    If you were using non-version-locked URLs (e.g., //cdn.jsdelivr.net/npm/docsify/lib/...), you must update both the path and the version specifier.

  10. Create a custom navbar using Markdown

    develop

    You can define your navigation using a Markdown file by enabling the loadNavbar configuration option.

    1. Set loadNavbar: true in your $docsify configuration object in index.html.
    2. Create a file named _navbar.md in your directory.
    3. Note for GitHub Pages: If you are deploying to GitHub Pages, you must create an empty file named .nojekyll in your ./docs directory. This prevents GitHub from ignoring files that start with an underscore (like _navbar.md).

    Navbar Hierarchy: _navbar.md is loaded from the current directory level. If no _navbar.md is found in the current directory, docsify will fall back to the parent directory's _navbar.md.

    <!-- index.html -->
    <script>
      window.$docsify = {
        loadNavbar: true,
      };
    </script>
    <script src="//cdn.jsdelivr.net/npm/docsify@5/dist/docsify.min.js"></script>
    <!-- _navbar.md -->
    
    - [En](/)
    - [chinese](/zh-cn/)
  11. Add new pages to Docsify

    develop

    To add new pages, create markdown files within your docs directory. Docsify uses the filename to determine the route. For example, a file named guide.md will be accessible at /#/guide.

    Route Mapping Examples:

    • docs/README.md => http://domain.com
    • docs/guide.md => http://domain.com/#/guide
    • docs/zh-cn/README.md => http://domain.com/#/zh-cn/
    • docs/zh-cn/guide.md => http://domain.com/#/zh-cn/guide

    You can also create a README.md file in a subdirectory to serve as the landing page for that specific route.

    .
    └── docs
        ├── README.md
        ├── guide.md
        └── zh-cn
            ├── README.md
            └── guide.md
  12. Add attributes to links (disabled, ignore, target)

    develop

    Docsify allows you to extend Markdown link syntax with special attributes passed inside single quotes.

    Use :disabled to make a link non-clickable.

    Ignore compilation (href)

    By default, Docsify compiles relative links to internal routes (e.g., /demo/ becomes /#/demo/). To link to an actual file or external path without Docsify's routing interference, use :ignore.

    You can also provide a title using the title keyword.

    Use :target to specify where the link should open (e.g., _blank or _self).

    [link](/demo ':disabled')
    
    [link](/demo/ ':ignore')
    
    [link](/demo/ ':ignore title')
    
    [link](/demo ':target=_blank')
    [link](/demo2 ':target=_self')