Skip to content
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

Refactor search services #1012

Merged
merged 15 commits into from
Jul 26, 2019
2 changes: 2 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,8 @@ local_search:
top_n_per_article: 1
# Unescape html strings to the readable one.
unescape: false
# Preload the search data when the page loads.
preload: false

# Swiftype Search API Key
swiftype_key:
Expand Down
10 changes: 8 additions & 2 deletions gulpfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ yaml = require('js-yaml')

gulp.task 'lint', ->
return gulp.src([
'./source/js/utils.js',
'./source/js/motion.js',
'./source/js/affix.js',
'./source/js/algolia-search.js',
'./source/js/exturl.js',
'./source/js/js.cookie.js',
'./source/js/local-search.js',
'./source/js/motion.js',
'./source/js/next-boot.js',
'./source/js/post-details.js',
'./source/js/scroll-cookie.js',
'./source/js/scrollspy.js',
'./source/js/utils.js',
'./source/js/schemes/muse.js',
'./source/js/schemes/pisces.js'
]).pipe jshint()
Expand Down
9 changes: 7 additions & 2 deletions layout/_partials/head/head.swig
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,20 @@
mediumzoom: {{ theme.mediumzoom }},
lazyload: {{ theme.lazyload }},
pangu: {{ theme.pangu }},
tabs: {{ theme.tabs.enable }},
motion: {{ theme.motion | json_encode }},
algolia: {
applicationID: '{{ config.algolia.applicationID }}',
apiKey: '{{ config.algolia.apiKey }}',
indexName: '{{ config.algolia.indexName }}',
hits: {{ theme.algolia_search.hits | json_encode }},
labels: {{ theme.algolia_search.labels | json_encode }}
},
localsearch: {{ theme.local_search | json_encode }},
search: {
root: '{{ config.root }}',
path: '{{ config.search.path }}'
},
tabs: {{ theme.tabs.enable }},
motion: {{ theme.motion | json_encode }},
translation: {
copy_button: '{{__("post.copy_button")}}',
copy_success: '{{__("post.copy_success")}}',
Expand Down
12 changes: 4 additions & 8 deletions layout/_partials/header/menu.swig
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@
{%- endif -%}
{%- endfor %}

{% set hasSearch = theme.swiftype_key || theme.algolia_search.enable || theme.local_search.enable %}
{% set hasSearch = theme.algolia_search.enable || theme.local_search.enable %}
{% if hasSearch %}
<li class="menu-item menu-item-search">
{% if theme.swiftype_key %}
<a href="javascript:;" class="st-search-show-outputs">
{% elif theme.local_search.enable || theme.algolia_search.enable %}
<a href="javascript:;" class="popup-trigger">
{% endif %}
{% if theme.menu_settings.icons %}
<i class="menu-item-icon fa fa-search fa-fw"></i> <br/>{#
<a href="javascript:;" class="popup-trigger">
{% if theme.menu_settings.icons %}
<i class="menu-item-icon fa fa-search fa-fw"></i> <br/>{#
#}{% endif %}{#
#}{{ __('menu.search') }}{#
#}</a>
Expand Down
2 changes: 0 additions & 2 deletions layout/_partials/search/index.swig
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{% if theme.algolia_search.enable %}
{% include 'algolia-search.swig' %}
{% elif theme.swiftype_key %}
{% include 'swiftype.swig' %}
{% elif theme.local_search.enable %}
{% include 'localsearch.swig' %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move layout/_partials/search/* to layout/_third-party/search/*?
I don't think it's necessary to have two layouts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jQuery is loaded at the end of the body. Local search depends on jQuery. Put the script here will cause an $ not defined error. Maybe we can open another PR to move jQuery to <head>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can open another PR to move jQuery to <head>

Don't do this.

我的意思是将原本在菜单那的搜索弹出框布局移到底部,与脚本合并,弹出框与其所在的位置没有关联,两个文件变一个,更便于维护

I mean to move the search popup layout that was originally in the menu to the bottom, merge with the script, the popup is not associated with its location, and the two files become one, which is easier to maintain.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

两个文件变一个,更便于维护

但是将DOM和<script>分离更加优美。在layout中,<script>只应存在于_scripts_third-party中,各种DOM存在于_partials中。就像评论系统,使用inject.comment向评论区写入DOM,使用inject.bodyend写入脚本。它们应该是分开的。

还是需要开一个新的PR,处理这样的情况。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
这是因为comment的位置比较尴尬,不能在bodyEnd也不是postBodyEnd 😂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外一个问题是各种share,也是同时存在DOM元素和脚本。真蛋疼..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

share完全不知道怎么搞,在我的基于next的主题里是全删了,反正我也不用,哈哈😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所以这咋办呀

Copy link
Member

@jiangtj jiangtj Jul 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不知道,那就暂时不管share咯,以后想到怎么处理了在弄。

这是我的搜索重构 jiangtj/hexo-theme-cake#11
加menu注入点,试了把localsearch提取出去,等GitHub包管理出了,可以这样搞啊,反正localsearch本来就要装个hexo插件:joy:

等你这个合并,我修改下插件直接CDN引用localsearch.js,美滋滋

{% endif %}
29 changes: 13 additions & 16 deletions layout/_third-party/search/algolia-search.swig
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
{% if theme.algolia_search.enable %}

{# S: Include Algolia instantsearch.js library #}
{% set algolia_instant_css = url_for(theme.vendors._internal + '/algolia-instant-search/instantsearch.min.css') %}
{% if theme.vendors.algolia_instant_css %}
{% set algolia_instant_css = theme.vendors.algolia_instant_css %}
{% endif %}
<link rel="stylesheet" href="{{ algolia_instant_css }}"/>

{% set algolia_instant_js = url_for(theme.vendors._internal + '/algolia-instant-search/instantsearch.min.js') %}
{% if theme.vendors.algolia_instant_js %}
{% set algolia_instant_js = theme.vendors.algolia_instant_js %}
{% endif %}
<script src="{{ algolia_instant_js }}"></script>
{# E: Include Algolia instantsearch.js library #}
{# S: Include Algolia instantsearch.js library #}
{% set algolia_instant_css = url_for(theme.vendors._internal + '/algolia-instant-search/instantsearch.min.css') %}
{% if theme.vendors.algolia_instant_css %}
{% set algolia_instant_css = theme.vendors.algolia_instant_css %}
{% endif %}
<link rel="stylesheet" href="{{ algolia_instant_css }}"/>

<script src="{{ url_for(theme.js) }}/algolia-search.js?v={{ version }}"></script>
{% set algolia_instant_js = url_for(theme.vendors._internal + '/algolia-instant-search/instantsearch.min.js') %}
{% if theme.vendors.algolia_instant_js %}
{% set algolia_instant_js = theme.vendors.algolia_instant_js %}
{% endif %}
<script src="{{ algolia_instant_js }}"></script>
{# E: Include Algolia instantsearch.js library #}

<script src="{{ url_for(theme.js) }}/algolia-search.js?v={{ version }}"></script>
9 changes: 7 additions & 2 deletions layout/_third-party/search/index.swig
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
{% include 'localsearch.swig' %}
{% include 'algolia-search.swig' %}
{% if theme.algolia_search.enable %}
{% include 'algolia-search.swig' %}
{% elif theme.swiftype_key %}
{% include 'swiftype.swig' %}
{% elif theme.local_search.enable %}
{% include 'localsearch.swig' %}
{% endif %}
Loading