docsify/docs/configuration.md
Roy Sommer 8cbc7c8073
Virtual Routes Support (#1799)
* add first test

* new VirtualRoutes mixin that handles routes. fetch tries to use VirtualRoutes. default config updated

* cover all basic use cases

* regex matching in routes

* covered all virtual routes tests

* added hack to fix config test on firefox

* removed formatting regex matches into string routes

* added support for "next" function

* added docs

* navigate now supports both hash and history routerModes

* waiting for networkidle in navigateToRoute helper

* promiseless implementation

* remove firefox workaround from catchPluginErrors test, since we no longer use promises

* updated docs

* updated docs for "alias" as well

* minor rephrasing

* removed non-legacy code from exact-match; updated navigateToRoute helper to infer router mode from page

* moved endsWith from router utils to general utils; added startsWith util; refactored makeExactMatcher to use both

* updated docs per feedback

* moved navigateToRoute helper into the virtual-routes test file

* moved navigateToRoute to top of file

* updated docs per pr comments
2022-05-23 14:03:33 -07:00

19 KiB

Configuration

You can configure Docsify by defining window.$docsify as an object:

<script>
  window.$docsify = {
    repo: 'docsifyjs/docsify',
    maxLevel: 3,
    coverpage: true,
  };
</script>

The config can also be defined as a function, in which case the first argument is the Docsify vm instance. The function should return a config object. This can be useful for referencing vm in places like the markdown configuration:

<script>
  window.$docsify = function (vm) {
    return {
      markdown: {
        renderer: {
          code(code, lang) {
            // ... use `vm` ...
          },
        },
      },
    };
  };
</script>

alias

  • Type: Object

Set the route alias. You can freely manage routing rules. Supports RegExp. Do note that order matters! If a route can be matched by multiple aliases, the one you declared first takes precedence.

window.$docsify = {
  alias: {
    '/foo/(.*)': '/bar/$1', // supports regexp
    '/zh-cn/changelog': '/changelog',
    '/changelog':
      'https://raw.githubusercontent.com/docsifyjs/docsify/master/CHANGELOG',
    '/.*/_sidebar.md': '/_sidebar.md', // See #301
  },
};

auto2top

  • Type: Boolean
  • Default: false

Scrolls to the top of the screen when the route is changed.

window.$docsify = {
  auto2top: true,
};

autoHeader

  • Type: Boolean
  • Default: false

If loadSidebar and autoHeader are both enabled, for each link in _sidebar.md, prepend a header to the page before converting it to HTML. See #78.

window.$docsify = {
  loadSidebar: true,
  autoHeader: true,
};

basePath

  • Type: String

Base path of the website. You can set it to another directory or another domain name.

window.$docsify = {
  basePath: '/path/',

  // Load the files from another site
  basePath: 'https://docsify.js.org/',

  // Even can load files from other repo
  basePath:
    'https://raw.githubusercontent.com/ryanmcdermott/clean-code-javascript/master/',
};

catchPluginErrors

  • Type: Boolean
  • Default: true

Determines if Docsify should handle uncaught synchronous plugin errors automatically. This can prevent plugin errors from affecting docsify's ability to properly render live site content.

cornerExternalLinkTarget

  • Type: String
  • Default: '_blank'

Target to open external link at the top right corner. Default '_blank' (new window/tab)

window.$docsify = {
  cornerExternalLinkTarget: '_self', // default: '_blank'
};

coverpage

  • Type: Boolean|String|String[]|Object
  • Default: false

Activate the cover feature. If true, it will load from _coverpage.md.

window.$docsify = {
  coverpage: true,

  // Custom file name
  coverpage: 'cover.md',

  // multiple covers
  coverpage: ['/', '/zh-cn/'],

  // multiple covers and custom file name
  coverpage: {
    '/': 'cover.md',
    '/zh-cn/': 'cover.md',
  },
};
  • Type: Array

When routerMode: 'history', you may face cross-origin issues. See #1379. In Markdown content, there is a simple way to solve it: see extends Markdown syntax Cross-Origin link in helpers.

window.$docsify = {
  crossOriginLinks: ['https://example.com/cross-origin-link'],
};

el

  • Type: String
  • Default: '#app'

The DOM element to be mounted on initialization. It can be a CSS selector string or an actual HTMLElement.

window.$docsify = {
  el: '#app',
};

executeScript

  • Type: Boolean
  • Default: null

Execute the script on the page. Only parses the first script tag (demo). If Vue is detected, this is true by default.

window.$docsify = {
  executeScript: true,
};
## This is test

<script>
  console.log(2333)
</script>

Note that if you are running an external script, e.g. an embedded jsfiddle demo, make sure to include the external-script plugin.

ext

  • Type: String
  • Default: '.md'

Request file extension.

window.$docsify = {
  ext: '.md',
};

externalLinkRel

  • Type: String
  • Default: 'noopener'

Default 'noopener' (no opener) prevents the newly opened external page (when externalLinkTarget is '_blank') from having the ability to control our page. No rel is set when it's not '_blank'. See this post for more information about why you may want to use this option.

window.$docsify = {
  externalLinkRel: '', // default: 'noopener'
};

externalLinkTarget

  • Type: String
  • Default: '_blank'

Target to open external links inside the markdown. Default '_blank' (new window/tab)

window.$docsify = {
  externalLinkTarget: '_self', // default: '_blank'
};

fallbackLanguages

  • Type: Array<string>

List of languages that will fallback to the default language when a page is requested and it doesn't exist for the given locale.

Example:

  • try to fetch the page of /de/overview. If this page exists, it'll be displayed.
  • then try to fetch the default page /overview (depending on the default language). If this page exists, it'll be displayed.
  • then display the 404 page.
window.$docsify = {
  fallbackLanguages: ['fr', 'de'],
};

formatUpdated

  • Type: String|Function

We can display the file update date through {docsify-updated} variable. And format it by formatUpdated. See https://github.com/lukeed/tinydate#patterns

window.$docsify = {
  formatUpdated: '{MM}/{DD} {HH}:{mm}',

  formatUpdated: function (time) {
    // ...

    return time;
  },
};

hideSidebar

  • Type : Boolean
  • Default: true

This option will completely hide your sidebar and won't render any content on the side.

window.$docsify = {
  hideSidebar: true,
};

homepage

  • Type: String
  • Default: 'README.md'

README.md in your docs folder will be treated as the homepage for your website, but sometimes you may need to serve another file as your homepage.

window.$docsify = {
  // Change to /home.md
  homepage: 'home.md',

  // Or use the readme in your repo
  homepage:
    'https://raw.githubusercontent.com/docsifyjs/docsify/master/README.md',
};

loadNavbar

  • Type: Boolean|String
  • Default: false

Loads navbar from the Markdown file _navbar.md if true, else loads it from the path specified.

window.$docsify = {
  // load from _navbar.md
  loadNavbar: true,

  // load from nav.md
  loadNavbar: 'nav.md',
};

loadSidebar

  • Type: Boolean|String
  • Default: false

Loads sidebar from the Markdown file _sidebar.md if true, else loads it from the path specified.

window.$docsify = {
  // load from _sidebar.md
  loadSidebar: true,

  // load from summary.md
  loadSidebar: 'summary.md',
};
  • Type: String

Website logo as it appears in the sidebar. You can resize it using CSS.

window.$docsify = {
  logo: '/_media/icon.svg',
};

markdown

  • Type: Function

See Markdown configuration.

window.$docsify = {
  // object
  markdown: {
    smartypants: true,
    renderer: {
      link: function () {
        // ...
      },
    },
  },

  // function
  markdown: function (marked, renderer) {
    // ...
    return marked;
  },
};

maxLevel

  • Type: Number
  • Default: 6

Maximum Table of content level.

window.$docsify = {
  maxLevel: 4,
};

mergeNavbar

  • Type: Boolean
  • Default: false

Navbar will be merged with the sidebar on smaller screens.

window.$docsify = {
  mergeNavbar: true,
};

name

  • Type: String

Website name as it appears in the sidebar.

window.$docsify = {
  name: 'docsify',
};

The name field can also contain custom HTML for easier customization:

window.$docsify = {
  name: '<span>docsify</span>',
};
  • Type: String
  • Default: 'window.location.pathname'

The URL that the website name links to.

window.$docsify = {
  nameLink: '/',

  // For each route
  nameLink: {
    '/zh-cn/': '#/zh-cn/',
    '/': '#/',
  },
};

nativeEmoji

  • Type: Boolean
  • Default: false

Render emoji shorthand codes using GitHub-style emoji images or platform-native emoji characters.

window.$docsify = {
  nativeEmoji: true,
};
:smile:
:partying_face:
:joy:
:+1:
:-1:

GitHub-style images when false:

smile partying_face joy +1 -1

Platform-native characters when true:

😄︎ 🥳︎ 😂︎ 👍︎ 👎︎

To render shorthand codes as text, replace : characters with the &colon; HTML entity.

&colon;100&colon;

💯

  • Type: Array<string>

Sometimes we do not want docsify to handle our links. See #203. We can skip compiling of certain links by specifying an array of strings. Each string is converted into to a regular expression (RegExp) and the whole href of a link is matched against it.

window.$docsify = {
  noCompileLinks: ['/foo', '/bar/.*'],
};

noEmoji

  • Type: Boolean
  • Default: false

Disabled emoji parsing and render all emoji shorthand as text.

window.$docsify = {
  noEmoji: true,
};
:100:

💯

To disable emoji parsing of individual shorthand codes, replace : characters with the &colon; HTML entity.

:100:

&colon;100&colon;

💯

💯

notFoundPage

  • Type: Boolean | String | Object
  • Default: false

Display default "404 - Not found" message:

window.$docsify = {
  notFoundPage: false,
};

Load the _404.md file:

window.$docsify = {
  notFoundPage: true,
};

Load the customized path of the 404 page:

window.$docsify = {
  notFoundPage: 'my404.md',
};

Load the right 404 page according to the localization:

window.$docsify = {
  notFoundPage: {
    '/': '_404.md',
    '/de': 'de/_404.md',
  },
};

Note: The options for fallbackLanguages don't work with the notFoundPage options.

onlyCover

  • Type: Boolean
  • Default: false

Only coverpage is loaded when visiting the home page.

window.$docsify = {
  onlyCover: false,
};

relativePath

  • Type: Boolean
  • Default: false

If true, links are relative to the current context.

For example, the directory structure is as follows:

.
└── docs
    ├── README.md
    ├── guide.md
    └── zh-cn
        ├── README.md
        ├── guide.md
        └── config
            └── example.md

With relative path enabled and current URL http://domain.com/zh-cn/README, given links will resolve to:

guide.md              => http://domain.com/zh-cn/guide
config/example.md     => http://domain.com/zh-cn/config/example
../README.md          => http://domain.com/README
/README.md            => http://domain.com/README
window.$docsify = {
  // Relative path enabled
  relativePath: true,

  // Relative path disabled (default value)
  relativePath: false,
};

repo

  • Type: String

Configure the repository url, or a string of username/repo, to add the GitHub Corner widget in the top right corner of the site.

window.$docsify = {
  repo: 'docsifyjs/docsify',
  // or
  repo: 'https://github.com/docsifyjs/docsify/',
};

requestHeaders

  • Type: Object

Set the request resource headers.

window.$docsify = {
  requestHeaders: {
    'x-token': 'xxx',
  },
};

Such as setting the cache

window.$docsify = {
  requestHeaders: {
    'cache-control': 'max-age=600',
  },
};

routerMode

  • Type: String
  • Default: 'hash'
window.$docsify = {
  routerMode: 'history', // default: 'hash'
};

routes

  • Type: Object

Define "virtual" routes that can provide content dynamically. A route is a map between the expected path, to either a string or a function. If the mapped value is a string, it is treated as markdown and parsed accordingly. If it is a function, it is expected to return markdown content.

A route function receives up to three parameters:

  1. route - the path of the route that was requested (e.g. /bar/baz)
  2. matched - the RegExpMatchArray that was matched by the route (e.g. for /bar/(.+), you get ['/bar/baz', 'baz'])
  3. next - this is a callback that you may call when your route function is async

Do note that order matters! Routes are matched the same order you declare them in, which means that in cases where you have overlapping routes, you might want to list the more specific ones first.

window.$docsify = {
  routes: {
    // Basic match w/ return string
    '/foo': '# Custom Markdown',

    // RegEx match w/ synchronous function
    '/bar/(.*)': function(route, matched) {
      return '# Custom Markdown';
    },

    // RegEx match w/ asynchronous function
    '/baz/(.*)': function(route, matched, next) {
       // Requires `fetch` polyfill for legacy browsers (https://github.github.io/fetch/)
      fetch('/api/users?id=12345')
        .then(function(response) {
          next('# Custom Markdown');
        })
        .catch(function(err) {
          // Handle error...
        });
    }
  }
}

Other than strings, route functions can return a falsy value (null \ undefined) to indicate that they ignore the current request:

window.$docsify = {
  routes: {
    // accepts everything other than dogs (synchronous)
    '/pets/(.+)': function(route, matched) {
      if (matched[0] === 'dogs') {
        return null;
      } else {
        return 'I like all pets but dogs';
      }
    }

    // accepts everything other than cats (asynchronous)
    '/pets/(.*)': function(route, matched, next) {
      if (matched[0] === 'cats') {
        next();
      } else {
        // Async task(s)...
        next('I like all pets but cats');
      }
    }
  }
}

Finally, if you have a specific path that has a real markdown file (and therefore should not be matched by your route), you can opt it out by returning an explicit false value:

window.$docsify = {
  routes: {
    // if you look up /pets/cats, docsify will skip all routes and look for "pets/cats.md"
    '/pets/cats': function(route, matched) {
      return false;
    }

    // but any other pet should generate dynamic content right here
    '/pets/(.+)': function(route, matched) {
      const pet = matched[0];
      return `your pet is ${pet} (but not a cat)`;
    }
  }
}

subMaxLevel

  • Type: Number
  • Default: 0

Add table of contents (TOC) in custom sidebar.

window.$docsify = {
  subMaxLevel: 2,
};

If you have a link to the homepage in the sidebar and want it to be shown as active when accessing the root url, make sure to update your sidebar accordingly:

- Sidebar
  - [Home](/)
  - [Another page](another.md)

For more details, see #1131.

themeColor

  • Type: String

Customize the theme color. Use CSS3 variables feature and polyfill in older browsers.

window.$docsify = {
  themeColor: '#3F51B5',
};

topMargin

  • Type: Number
  • Default: 0

Adds a space on top when scrolling the content page to reach the selected section. This is useful in case you have a sticky-header layout and you want to align anchors to the end of your header.

window.$docsify = {
  topMargin: 90, // default: 0
};

vueComponents

  • Type: Object

Creates and registers global Vue components. Components are specified using the component name as the key with an object containing Vue options as the value. Component data is unique for each instance and will not persist as users navigate the site.

window.$docsify = {
  vueComponents: {
    'button-counter': {
      template: `
        <button @click="count += 1">
          You clicked me {{ count }} times
        </button>
      `,
      data() {
        return {
          count: 0,
        };
      },
    },
  },
};
<button-counter></button-counter>

vueGlobalOptions

  • Type: Object

Specifies Vue options for use with Vue content not explicitly mounted with vueMounts, vueComponents, or a markdown script. Changes to global data will persist and be reflected anywhere global references are used.

window.$docsify = {
  vueGlobalOptions: {
    data() {
      return {
        count: 0,
      };
    },
  },
};
<p>
  <button @click="count -= 1">-</button>
  {{ count }}
  <button @click="count += 1">+</button>
</p>

- {{ count }} +

vueMounts

  • Type: Object

Specifies DOM elements to mount as Vue instances and their associated options. Mount elements are specified using a CSS selector as the key with an object containing Vue options as their value. Docsify will mount the first matching element in the main content area each time a new page is loaded. Mount element data is unique for each instance and will not persist as users navigate the site.

window.$docsify = {
  vueMounts: {
    '#counter': {
      data() {
        return {
          count: 0,
        };
      },
    },
  },
};
<div id="counter">
  <button @click="count -= 1">-</button>
  {{ count }}
  <button @click="count += 1">+</button>
</div>
- {{ count }} +