@vuepress/plugin-search

Headers-based search plugin

Install

  1. yarn add -D @vuepress/plugin-search
  2. # OR npm install -D @vuepress/plugin-search

TIP

Note that this plugin has been included in default theme, the search box you see now is powered by the plugin.

Usage

  • Enable this plugin:
  1. // .vuepress/config.js or themePath/index.js
  2. module.exports = {
  3. plugins: [
  4. ['@vuepress/search', {
  5. searchMaxSuggestions: 10
  6. }]
  7. ]
  8. }
  • This plugin will automatically inject a webpack alias @SearchBox pointing to the search component so that you can use it directly in your layout component:
  1. <template>
  2. <div class="foo-layout">
  3. <header>
  4. <SearchBox/>
  5. </header>
  6. <main>
  7. ...
  8. </main>
  9. </div>
  10. </template>
  11. <script>
  12. import SearchBox from '@SearchBox'
  13. export default {
  14. components: { SearchBox }
  15. }
  16. </script>

Options

searchMaxSuggestions

  • Type: number
  • Default: 5

Set the maximum number of results for search.

test

  • Type: RegExp | Array<RegExp>
  • Default: null

Set up searchable paths with regular expressions. If no test expression is provided it will search on all paths. Considering you have this structure:

  1. docs/
  2. ├── .vuepress/
  3. └── ...
  4. ├── master/
  5. └── ...
  6. ├── 1.0/
  7. └── ...
  8. └── 2.0/
  9. └── ...

You can set up searchable paths with test as:

  • RegExp: '/1.0/'
  • Array of RegExp: ['/1.0/', '/2.0/']

Otherwise, the default search will return duplicates, once you can have similar content between folders /master/, /1.0/ and /2.0/.

searchHotkeys

  • Type: Array<string>
  • Default: ['s', '/']

Configure the hotkeys which when pressed will focus the search box. Set to an empty array to disable this feature.

Tips

Tweak the default colors.

Since the Search component leverages the built-in palette, you can tweak the default colors via styles/palette.styl:

  1. // colors of the searchbox you see now:
  2. $accentColor = #3eaf7c
  3. $textColor = #2c3e50
  4. $borderColor = #eaecef
  5. $codeBgColor = #282c34
  6. $arrowBgColor = #ccc