-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Algolia DocSearch Integration #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 16 commits
a010d64
330d6f0
8621a6c
0758918
55826c5
b1d2d26
4769b7b
e1d6e88
8177fc6
7da0c2a
2dffc4c
ed09b5f
5c3ae19
702d676
ea8aa21
a95aa89
88f7435
c150f22
44adcd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,24 @@ module.exports = { | |
当然,仅仅只在你的网站部署后能用 SSL 的时候开启它,因为 service worker 只能在 HTTPs 的链接下注册。 | ||
::: | ||
|
||
### algolia | ||
|
||
- 类型: `Object` | ||
- 默认值: `undefined` | ||
|
||
使用 `algolia` 选项可以让你禁用掉默认的基于 headers 的搜索,从而使用 [algolia docsearch](https://github.com/algolia/docsearch)。为了使其生效,你必须提供至少 `apiKey` 和 `indexName` 这两个选项: | ||
|
||
```js | ||
module.exports = { | ||
algolia: { | ||
apiKey: '<API_KEY>', | ||
indexName: '<INDEX_NAME>' | ||
} | ||
} | ||
``` | ||
|
||
其他可用的选项可以参考 [docsearch options](https://github.com/algolia/docsearch#docsearch-options)。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "请"参考 |
||
|
||
### locales | ||
|
||
- 类型: `{ [path: string]: Object }` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<template> | ||
<form id="search-form" class="algolia-search-wrapper search-box"> | ||
<input id="algolia-search-input" class="search-query"> | ||
</form> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
props: ['options'], | ||
mounted () { | ||
this.initialize() | ||
}, | ||
methods: { | ||
initialize () { | ||
Promise.all([ | ||
import(/* webpackChunkName: "docsearch" */ 'docsearch.js/dist/cdn/docsearch.min.js'), | ||
import(/* webpackChunkName: "docsearch" */ 'docsearch.js/dist/cdn/docsearch.min.css') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 现在的 webpack 配置会把所有 css 都抽取出来,即使用 dynamic import 也没用。这样的话不论用不用 algolia 都会加载这些 css。有一个办法是在 createBaseConfig 的时候如果用户没有使用 algolia 则直接把 AlgoliaSearchBox alias 到一个空 module, 这样可以彻底避免把这个组件打包进去。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 厉害了,我还没注意这个 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 哈哈哈哈哈,突然说中文 XD |
||
]).then(([docsearch]) => { | ||
docsearch = docsearch.default | ||
docsearch(Object.assign(this.options, { | ||
debug: true, | ||
inputSelector: '#algolia-search-input' | ||
})) | ||
}) | ||
} | ||
}, | ||
watch: { | ||
options (newValue) { | ||
this.$el.innerHTML = '<input id="algolia-search-input" class="search-query">' | ||
this.initialize(newValue) | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<style lang="stylus"> | ||
@import './styles/config.styl' | ||
|
||
.algolia-search-wrapper | ||
& > span | ||
vertical-align middle | ||
.algolia-autocomplete | ||
line-height normal | ||
.ds-dropdown-menu | ||
background-color #fff | ||
border 1px solid #999 | ||
border-radius 4px | ||
font-size 16px | ||
margin 6px 0 0 | ||
padding 4px | ||
text-align left | ||
&:before | ||
border-color #999 | ||
[class*=ds-dataset-] | ||
border none | ||
padding 0 | ||
.ds-suggestions | ||
margin-top 0 | ||
.ds-suggestion | ||
border-bottom 1px solid $borderColor | ||
.algolia-docsearch-suggestion--highlight | ||
color #2c815b | ||
.algolia-docsearch-suggestion | ||
border-color $borderColor | ||
padding 0 | ||
.algolia-docsearch-suggestion--category-header | ||
padding 5px 10px | ||
margin-top 0 | ||
background $accentColor | ||
color #fff | ||
font-weight 600 | ||
.algolia-docsearch-suggestion--highlight | ||
background rgba(255, 255, 255, 0.6) | ||
.algolia-docsearch-suggestion--wrapper | ||
padding 0 | ||
.algolia-docsearch-suggestion--title | ||
font-weight 600 | ||
margin-bottom 0 | ||
color $textColor | ||
.algolia-docsearch-suggestion--subcategory-column | ||
vertical-align top | ||
padding 5px 7px 5px 5px | ||
border-color $borderColor | ||
background #f1f3f5 | ||
&:after | ||
display none | ||
.algolia-docsearch-suggestion--subcategory-column-text | ||
color #555 | ||
.algolia-docsearch-footer | ||
border-color $borderColor | ||
.ds-cursor .algolia-docsearch-suggestion--content | ||
background-color #e7edf3 !important | ||
color $textColor | ||
|
||
@media (min-width: $MQMobile) | ||
.algolia-search-wrapper | ||
.algolia-autocomplete | ||
.algolia-docsearch-suggestion | ||
.algolia-docsearch-suggestion--subcategory-column | ||
float none | ||
width 150px | ||
min-width 150px | ||
display table-cell | ||
.algolia-docsearch-suggestion--content | ||
float none | ||
display table-cell | ||
width 100% | ||
vertical-align top | ||
.ds-dropdown-menu | ||
min-width 515px !important | ||
|
||
@media (max-width: $MQMobile) | ||
.algolia-search-wrapper | ||
.ds-dropdown-menu | ||
min-width calc(100vw - 4rem) !important | ||
max-width calc(100vw - 4rem) !important | ||
.algolia-docsearch-suggestion--wrapper | ||
padding 5px 7px 5px 5px !important | ||
.algolia-docsearch-suggestion--subcategory-column | ||
padding 0 !important | ||
background white !important | ||
.algolia-docsearch-suggestion--subcategory-column-text:after | ||
content " > " | ||
font-size 10px | ||
line-height 14.4px | ||
display inline-block | ||
width 5px | ||
margin -3px 3px 0 | ||
vertical-align middle | ||
|
||
</style> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,19 +12,33 @@ | |
</span> | ||
</router-link> | ||
<div class="links"> | ||
<SearchBox v-if="$site.themeConfig.search !== false"/> | ||
<AlgoliaSearchBox v-if="isAlgoliaSearch" :options="algolia"/> | ||
<SearchBox v-if="isSearch"/> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可以用 |
||
<NavLinks class="can-hide"/> | ||
</div> | ||
</header> | ||
</template> | ||
|
||
<script> | ||
import SidebarButton from './SidebarButton.vue' | ||
import AlgoliaSearchBox from './AlgoliaSearchBox.vue' | ||
import SearchBox from './SearchBox.vue' | ||
import NavLinks from './NavLinks.vue' | ||
|
||
export default { | ||
components: { SidebarButton, NavLinks, SearchBox } | ||
components: { SidebarButton, NavLinks, SearchBox, AlgoliaSearchBox }, | ||
computed: { | ||
algolia () { | ||
return this.$themeLocaleConfig.algolia || this.$site.themeConfig.algolia || {} | ||
}, | ||
isAlgoliaSearch () { | ||
const algolia = this.algolia | ||
return algolia && algolia.apiKey && algolia.indexName | ||
}, | ||
isSearch() { | ||
return !this.isAlgoliaSearch && this.$site.themeConfig.search !== false | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...可以让你用 algolia search 取代默认的...