From 2f0da0103dbc952023caffdfdb726f117ca3163d Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Mon, 23 Apr 2018 14:14:22 -0500 Subject: [PATCH] feat: Algolia DocSearch Integration (#201) --- README.md | 1 - docs/config/README.md | 18 ++++ docs/guide/README.md | 1 - docs/guide/i18n.md | 3 + docs/zh/config/README.md | 18 ++++ docs/zh/guide/README.md | 1 - docs/zh/guide/i18n.md | 3 + lib/default-theme/AlgoliaSearchBox.vue | 131 +++++++++++++++++++++++++ lib/default-theme/Navbar.vue | 15 ++- lib/noop.js | 1 + lib/prepare.js | 8 ++ lib/webpack/createBaseConfig.js | 4 + package.json | 1 + yarn.lock | 116 +++++++++++++++++++++- 14 files changed, 311 insertions(+), 10 deletions(-) create mode 100644 lib/default-theme/AlgoliaSearchBox.vue create mode 100644 lib/noop.js diff --git a/README.md b/README.md index 91044a78b2..5a440123ab 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,6 @@ https://vuepress.vuejs.org/ VuePress is still a work in progress. There are a few things that it currently does not support but are planned: -- Algolia DocSearch Integration - Blogging support Contributions are welcome! diff --git a/docs/config/README.md b/docs/config/README.md index 8849003146..9e1698446f 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -98,6 +98,24 @@ The `serviceWorker` option only handles the service worker. To make your site fu Also, only enable this if you are able to deploy your site with SSL, since service worker can only be registered under HTTPs URLs. ::: +### algolia + +- Type: `Object` +- Default: `undefined` + +The `algolia` option allows you to use [algolia docsearch](https://github.com/algolia/docsearch) to replace the simple built-in search. To enable it, you need to provide at least `apiKey` and `indexName`: + +```js +module.exports = { + algolia: { + apiKey: '', + indexName: '' + } +} +``` + +For more options, refer to [Algolia DocSearch's documentation](https://github.com/algolia/docsearch#docsearch-options). + ### locales - Type: `{ [path: string]: Object }` diff --git a/docs/guide/README.md b/docs/guide/README.md index 91a1a77814..34d7ebafb3 100644 --- a/docs/guide/README.md +++ b/docs/guide/README.md @@ -31,7 +31,6 @@ Each markdown file is compiled into HTML with [markdown-it](https://github.com/m VuePress is still a work in progress. There are a few things that it currently does not support but are planned: -- Algolia DocSearch Integration - Blogging support Contributions are welcome! diff --git a/docs/guide/i18n.md b/docs/guide/i18n.md index eb6ac4a8cc..2ae4b7c3cd 100644 --- a/docs/guide/i18n.md +++ b/docs/guide/i18n.md @@ -56,6 +56,8 @@ module.exports = { label: 'English', // text for the edit-on-github link editLinkText: 'Edit this page on GitHub', + // algolia docsearch options for current locale + algolia: {}, nav: [ { text: 'Nested', link: '/nested/' } ], @@ -71,6 +73,7 @@ module.exports = { nav: [ { text: '嵌套', link: '/zh/nested/' } ], + algolia: {}, sidebar: { '/zh/': [/* ... */], '/zh/nested/': [/* ... */] diff --git a/docs/zh/config/README.md b/docs/zh/config/README.md index f8f34382d4..9e764d8a0e 100644 --- a/docs/zh/config/README.md +++ b/docs/zh/config/README.md @@ -97,6 +97,24 @@ module.exports = { 当然,仅仅只在你的网站部署后能用 SSL 的时候开启它,因为 service worker 只能在 HTTPs 的链接下注册。 ::: +### algolia + +- 类型: `Object` +- 默认值: `undefined` + +使用 `algolia` 选项可以让你用 [algolia docsearch](https://github.com/algolia/docsearch) 取代默认的基于 headers 的搜索 。为了使其生效,你必须提供至少 `apiKey` 和 `indexName` 这两个选项: + +```js +module.exports = { + algolia: { + apiKey: '', + indexName: '' + } +} +``` + +其他可用的选项请参考 [docsearch options](https://github.com/algolia/docsearch#docsearch-options)。 + ### locales - 类型: `{ [path: string]: Object }` diff --git a/docs/zh/guide/README.md b/docs/zh/guide/README.md index dbd901df09..30ca07d875 100644 --- a/docs/zh/guide/README.md +++ b/docs/zh/guide/README.md @@ -29,7 +29,6 @@ VuePress 由两部分组成:一部分是支持用 Vue 开发主题的极简静 VuePress 仍然处于开发中,这里有一些目前还不支持、但已经在计划中的特性: -- Algolia DocSearch 的集成 - 博客系统 我们欢迎你为 VuePress 的开发作出贡献。 diff --git a/docs/zh/guide/i18n.md b/docs/zh/guide/i18n.md index 5d97beba56..216048225c 100644 --- a/docs/zh/guide/i18n.md +++ b/docs/zh/guide/i18n.md @@ -53,6 +53,7 @@ module.exports = { selectText: 'Languages', label: 'English', editLinkText: 'Edit this page on GitHub', + algolia: {}, nav: [ { text: 'Nested', link: '/nested/' } ], @@ -68,6 +69,8 @@ module.exports = { label: '简体中文', // 编辑链接文字 editLinkText: '在 GitHub 上编辑此页', + // 当前 locale 的 algolia docsearch 选项 + algolia: {}, nav: [ { text: '嵌套', link: '/zh/nested/' } ], diff --git a/lib/default-theme/AlgoliaSearchBox.vue b/lib/default-theme/AlgoliaSearchBox.vue new file mode 100644 index 0000000000..c27f1bbdda --- /dev/null +++ b/lib/default-theme/AlgoliaSearchBox.vue @@ -0,0 +1,131 @@ + + + + + diff --git a/lib/default-theme/Navbar.vue b/lib/default-theme/Navbar.vue index f916668739..6a99ff4d38 100644 --- a/lib/default-theme/Navbar.vue +++ b/lib/default-theme/Navbar.vue @@ -12,7 +12,8 @@ @@ -20,11 +21,21 @@ diff --git a/lib/noop.js b/lib/noop.js new file mode 100644 index 0000000000..4ba52ba2c8 --- /dev/null +++ b/lib/noop.js @@ -0,0 +1 @@ +module.exports = {} diff --git a/lib/prepare.js b/lib/prepare.js index 00b7a63faf..2dbcfadfbc 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -111,6 +111,13 @@ async function resolveOptions (sourceDir) { !fs.existsSync(path.resolve(vuepressDir, 'theme')) ) + // resolve algolia + const isAlgoliaSearch = ( + siteConfig.algolia || + Object.keys(siteConfig.locales && siteConfig.themeConfig && siteConfig.themeConfig.locales || {}) + .some(base => siteConfig.themeConfig.locales[base].algolia) + ) + const options = { siteConfig, sourceDir, @@ -123,6 +130,7 @@ async function resolveOptions (sourceDir) { themePath: null, notFoundPath: null, useDefaultTheme, + isAlgoliaSearch, markdown: createMarkdown(siteConfig) } diff --git a/lib/webpack/createBaseConfig.js b/lib/webpack/createBaseConfig.js index 06e63ac66a..20db0223df 100644 --- a/lib/webpack/createBaseConfig.js +++ b/lib/webpack/createBaseConfig.js @@ -7,6 +7,7 @@ module.exports = function createBaseConfig ({ publicPath, themePath, notFoundPath, + isAlgoliaSearch, markdown }, { debug } = {}, isServer) { const Config = require('webpack-chain') @@ -39,6 +40,9 @@ module.exports = function createBaseConfig ({ .set('@source', sourceDir) .set('@app', path.resolve(__dirname, '../app')) .set('@temp', path.resolve(__dirname, '../app/.temp')) + .set('@AlgoliaSearchBox', isAlgoliaSearch + ? path.resolve(__dirname, '../default-theme/AlgoliaSearchBox.vue') + : path.resolve(__dirname, '../noop.js')) .end() .extensions .merge(['.js', '.jsx', '.vue', '.json']) diff --git a/package.json b/package.json index b0bb3303c3..7ed279e8d6 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "copy-webpack-plugin": "^4.5.1", "css-loader": "^0.28.11", "diacritics": "^1.3.0", + "docsearch.js": "^2.5.2", "es6-promise": "^4.2.4", "escape-html": "^1.0.3", "file-loader": "^1.1.11", diff --git a/yarn.lock b/yarn.lock index 29be7b94a9..0d87704058 100644 --- a/yarn.lock +++ b/yarn.lock @@ -106,6 +106,10 @@ acorn@^5.0.0, acorn@^5.0.3, acorn@^5.3.0, acorn@^5.4.1, acorn@^5.5.0: version "5.5.3" resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.5.3.tgz#f473dd47e0277a08e28e9bec5aeeb04751f0b8c9" +agentkeepalive@^2.2.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-2.2.0.tgz#c5d1bd4b129008f1163f236f86e5faea2026e2ef" + ajv-keywords@^2.1.0: version "2.1.1" resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-2.1.1.tgz#617997fc5f60576894c435f940d819e135b80762" @@ -139,6 +143,26 @@ ajv@^6.0.1, ajv@^6.1.0: json-schema-traverse "^0.3.0" uri-js "^3.0.2" +algoliasearch@^3.24.5: + version "3.27.0" + resolved "https://registry.yarnpkg.com/algoliasearch/-/algoliasearch-3.27.0.tgz#675b7f2d186e5785a1553369b15d47b53d4efb31" + dependencies: + agentkeepalive "^2.2.0" + debug "^2.6.8" + envify "^4.0.0" + es6-promise "^4.1.0" + events "^1.1.0" + foreach "^2.0.5" + global "^4.3.2" + inherits "^2.0.1" + isarray "^2.0.1" + load-script "^1.0.0" + object-keys "^1.0.11" + querystring-es3 "^0.2.1" + reduce "^1.0.1" + semver "^5.1.0" + tunnel-agent "^0.6.0" + align-text@^0.1.1, align-text@^0.1.3: version "0.1.4" resolved "https://registry.yarnpkg.com/align-text/-/align-text-0.1.4.tgz#0cd90a561093f35d0a99256c22b7069433fad117" @@ -329,6 +353,12 @@ atob@^2.0.0: version "2.1.0" resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.0.tgz#ab2b150e51d7b122b9efc8d7340c06b6c41076bc" +autocomplete.js@^0.29.0: + version "0.29.0" + resolved "https://registry.yarnpkg.com/autocomplete.js/-/autocomplete.js-0.29.0.tgz#0185f7375ee9daf068f7d52d794bc90dcd739fd7" + dependencies: + immediate "^3.2.3" + autoprefixer@^6.3.1: version "6.7.7" resolved "https://registry.yarnpkg.com/autoprefixer/-/autoprefixer-6.7.7.tgz#1dbd1c835658e35ce3f9984099db00585c782014" @@ -1597,6 +1627,15 @@ dir-glob@^2.0.0: arrify "^1.0.1" path-type "^3.0.0" +docsearch.js@^2.5.2: + version "2.5.2" + resolved "https://registry.yarnpkg.com/docsearch.js/-/docsearch.js-2.5.2.tgz#1a3521c92e5f252cc522c57357ef1c47b945b381" + dependencies: + algoliasearch "^3.24.5" + autocomplete.js "^0.29.0" + hogan.js "^3.0.2" + to-factory "^1.0.0" + doctrine@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-2.1.0.tgz#5cd01fc101621b42c4cd7f5d1a66243716d3f39d" @@ -1616,6 +1655,10 @@ dom-serializer@0: domelementtype "~1.1.1" entities "~1.1.1" +dom-walk@^0.1.0: + version "0.1.1" + resolved "https://registry.yarnpkg.com/dom-walk/-/dom-walk-0.1.1.tgz#672226dc74c8f799ad35307df936aba11acd6018" + domain-browser@^1.1.1: version "1.2.0" resolved "https://registry.yarnpkg.com/domain-browser/-/domain-browser-1.2.0.tgz#3d31f50191a6749dd1375a7f522e823d42e54eda" @@ -1743,6 +1786,13 @@ entities@^1.1.1, entities@~1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/entities/-/entities-1.1.1.tgz#6e5c2d0a5621b5dadaecef80b90edfb5cd7772f0" +envify@^4.0.0: + version "4.1.0" + resolved "https://registry.yarnpkg.com/envify/-/envify-4.1.0.tgz#f39ad3db9d6801b4e6b478b61028d3f0b6819f7e" + dependencies: + esprima "^4.0.0" + through "~2.3.4" + errno@^0.1.3, errno@~0.1.7: version "0.1.7" resolved "https://registry.yarnpkg.com/errno/-/errno-0.1.7.tgz#4684d71779ad39af177e3f007996f7c67c852618" @@ -1777,7 +1827,7 @@ es-to-primitive@^1.1.1: is-date-object "^1.0.1" is-symbol "^1.0.1" -es6-promise@^4.2.4: +es6-promise@^4.1.0, es6-promise@^4.2.4: version "4.2.4" resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.2.4.tgz#dc4221c2b16518760bd8c39a52d8f356fc00ed29" @@ -1905,7 +1955,7 @@ esutils@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b" -events@^1.0.0: +events@^1.0.0, events@^1.1.0: version "1.1.1" resolved "https://registry.yarnpkg.com/events/-/events-1.1.1.tgz#9ebdb7635ad099c70dcc4c2a1f5004288e8bd924" @@ -2407,6 +2457,13 @@ global-dirs@^0.1.0: dependencies: ini "^1.3.4" +global@^4.3.2: + version "4.3.2" + resolved "https://registry.yarnpkg.com/global/-/global-4.3.2.tgz#e76989268a6c74c38908b1305b10fc0e394e9d0f" + dependencies: + min-document "^2.19.0" + process "~0.5.1" + globals@^11.0.1: version "11.4.0" resolved "https://registry.yarnpkg.com/globals/-/globals-11.4.0.tgz#b85c793349561c16076a3c13549238a27945f1bc" @@ -2616,6 +2673,13 @@ hoek@4.x.x: version "4.2.1" resolved "https://registry.yarnpkg.com/hoek/-/hoek-4.2.1.tgz#9634502aa12c445dd5a7c5734b572bb8738aacbb" +hogan.js@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/hogan.js/-/hogan.js-3.0.2.tgz#4cd9e1abd4294146e7679e41d7898732b02c7bfd" + dependencies: + mkdirp "0.3.0" + nopt "1.0.10" + hosted-git-info@^2.1.4: version "2.6.0" resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.6.0.tgz#23235b29ab230c576aab0d4f13fc046b0b038222" @@ -2730,6 +2794,10 @@ ignore@^3.3.3, ignore@^3.3.5: version "3.3.7" resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.7.tgz#612289bfb3c220e186a58118618d5be8c1bab021" +immediate@^3.2.3: + version "3.2.3" + resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.2.3.tgz#d140fa8f614659bd6541233097ddaac25cdd991c" + import-lazy@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/import-lazy/-/import-lazy-2.1.0.tgz#05698e3d45c88e8d7e9d92cb0584e77f096f3e43" @@ -3108,6 +3176,10 @@ isarray@1.0.0, isarray@^1.0.0, isarray@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" +isarray@^2.0.1: + version "2.0.4" + resolved "https://registry.yarnpkg.com/isarray/-/isarray-2.0.4.tgz#38e7bcbb0f3ba1b7933c86ba1894ddfc3781bbb7" + isemail@3.x.x: version "3.1.2" resolved "https://registry.yarnpkg.com/isemail/-/isemail-3.1.2.tgz#937cf919002077999a73ea8b1951d590e84e01dd" @@ -3627,6 +3699,10 @@ load-json-file@^4.0.0: pify "^3.0.0" strip-bom "^3.0.0" +load-script@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/load-script/-/load-script-1.0.0.tgz#0491939e0bee5643ee494a7e3da3d2bac70c6ca4" + loader-runner@^2.3.0: version "2.3.0" resolved "https://registry.yarnpkg.com/loader-runner/-/loader-runner-2.3.0.tgz#f482aea82d543e07921700d5a46ef26fdac6b8a2" @@ -3948,6 +4024,12 @@ mimic-fn@^1.0.0: version "1.2.0" resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-1.2.0.tgz#820c86a39334640e99516928bd03fca88057d022" +min-document@^2.19.0: + version "2.19.0" + resolved "https://registry.yarnpkg.com/min-document/-/min-document-2.19.0.tgz#7bd282e3f5842ed295bb748cdd9f1ffa2c824685" + dependencies: + dom-walk "^0.1.0" + mini-css-extract-plugin@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/mini-css-extract-plugin/-/mini-css-extract-plugin-0.4.0.tgz#ff3bf08bee96e618e177c16ca6131bfecef707f9" @@ -4010,6 +4092,10 @@ mixin-deep@^1.2.0: for-in "^1.0.2" is-extendable "^1.0.1" +mkdirp@0.3.0: + version "0.3.0" + resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.3.0.tgz#1bbf5ab1ba827af23575143490426455f481fe1e" + mkdirp@0.5.x, "mkdirp@>=0.5 0", mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@~0.5.1: version "0.5.1" resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903" @@ -4158,6 +4244,12 @@ node-pre-gyp@^0.6.39: tar "^2.2.1" tar-pack "^3.4.0" +nopt@1.0.10: + version "1.0.10" + resolved "https://registry.yarnpkg.com/nopt/-/nopt-1.0.10.tgz#6ddd21bd2a31417b92727dd585f8a6f37608ebee" + dependencies: + abbrev "1" + nopt@^4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/nopt/-/nopt-4.0.1.tgz#d0d4685afd5415193c8c7505602d0d17cd64474d" @@ -4264,7 +4356,7 @@ object-copy@^0.1.0: define-property "^0.2.5" kind-of "^3.0.3" -object-keys@^1.0.8: +object-keys@^1.0.11, object-keys@^1.0.8, object-keys@~1.0.0: version "1.0.11" resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.0.11.tgz#c54601778ad560f1142ce0e01bcca8b56d13426d" @@ -4933,6 +5025,10 @@ process@^0.11.10: version "0.11.10" resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182" +process@~0.5.1: + version "0.5.2" + resolved "https://registry.yarnpkg.com/process/-/process-0.5.2.tgz#1638d8a8e34c2f440a91db95ab9aeb677fc185cf" + progress@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.0.tgz#8a1be366bf8fc23db2bd23f10c6fe920b4389d1f" @@ -5005,7 +5101,7 @@ query-string@^4.1.0: object-assign "^4.1.0" strict-uri-encode "^1.0.0" -querystring-es3@^0.2.0: +querystring-es3@^0.2.0, querystring-es3@^0.2.1: version "0.2.1" resolved "https://registry.yarnpkg.com/querystring-es3/-/querystring-es3-0.2.1.tgz#9ec61f79049875707d69414596fd907a4d711e73" @@ -5138,6 +5234,12 @@ reduce-function-call@^1.0.1: dependencies: balanced-match "^0.4.2" +reduce@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/reduce/-/reduce-1.0.1.tgz#14fa2e5ff1fc560703a020cbb5fbaab691565804" + dependencies: + object-keys "~1.0.0" + regenerate@^1.2.1: version "1.3.3" resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.3.3.tgz#0c336d3980553d755c39b586ae3b20aa49c82b7f" @@ -5981,7 +6083,7 @@ through2@^2.0.0, through2@^2.0.2: readable-stream "^2.1.5" xtend "~4.0.1" -through@2, "through@>=2.2.7 <3", through@^2.3.6: +through@2, "through@>=2.2.7 <3", through@^2.3.6, through@~2.3.4: version "2.3.8" resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5" @@ -6013,6 +6115,10 @@ to-arraybuffer@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/to-arraybuffer/-/to-arraybuffer-1.0.1.tgz#7d229b1fcc637e466ca081180836a7aabff83f43" +to-factory@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/to-factory/-/to-factory-1.0.0.tgz#8738af8bd97120ad1d4047972ada5563bf9479b1" + to-object-path@^0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/to-object-path/-/to-object-path-0.3.0.tgz#297588b7b0e7e0ac08e04e672f85c1f4999e17af"