未验证 提交 f8439aa1 编写于 作者: 峡州仙士 提交者: GitHub

feat: 本地搜索备用的外部搜索引擎搜索(谷歌、必应) (#89)

* Update README.md

* Update README_en-US.md

* external alternative search

* external alternative search

* external alternative search

* external alternative search

* docs: update readme.md

* feat: add assist_search

* docs: add doc for assist_search & update some others

* docs: add doc for hexo-submit-urls-to-search-engine
Co-authored-by: EvanOne(文一)'s avatarliuyib <1656081615@qq.com>
上级 004e7361
......@@ -39,7 +39,7 @@
- [弥枳's Blog](http://blog2.coor.top/)
- [轻尘's Blog](https://qcxitong.xyz/)
- [impressionyang's Blog](https://impressionyang.github.io)
- [峡州仙士之页](https://cjh0613.gitee.io/blog/)
- [峡州仙士之页](https://cjh0613.github.io/blog/)
- [Cheeks's Blog](https://www.cheeks.top/)
- [lingan1996's Blog](https://www.lingan1996.top)
- [MikyMing's Blog](https://mikyming.online/)
......
......@@ -39,7 +39,7 @@ Language: [English](https://github.com/liuyib/hexo-theme-stun/blob/master/README
- [弥枳's Blog](http://blog2.coor.top/)
- [轻尘's Blog](https://qcxitong.xyz/)
- [impressionyang's Blog](https://impressionyang.github.io)
- [峡州仙士之页](https://cjh0613.gitee.io/blog/)
- [峡州仙士之页](https://cjh0613.github.io/en/)
- [Cheeks's Blog](https://www.cheeks.top/)
- [lingan1996's Blog](https://www.lingan1996.top)
- [MikyMing's Blog](https://mikyming.online/)
......
......@@ -614,7 +614,20 @@ algolia_search:
# Local Search
# See: https://github.com/wzpan/hexo-generator-search/
local_search:
enable: true
# Auxiliary search buttons
# ! -----------------------------------------------------------------------------------------
# ! This feature can only be enabled if either `algolia_search` or `local_search` is enabled.
# ! -----------------------------------------------------------------------------------------
assist_search:
enable: false
# Show the Google Search button. Use `www.google.com` to search for what you type.
google: true
# Show the Bing Search button. Use `www.bing.com` to search for what you type.
bing: true
# Show the Baidu Search button. Use `www.baidu.com` to search for what you type.
baidu: true
# ---------------------------------------------------------------
# Background config
......@@ -838,6 +851,8 @@ icon:
notetag_info: fas fa-info-circle
notetag_warning: fas fa-exclamation-circle
notetag_danger: fas fa-minus-circle
google: fab fa-google
# bing: fab fa-bing # not exist
# Set a CDN address for the vendor you want to customize.
# ! -------------------------------------------------------------------
......
# 优化建议
## 优化网站 SEO
### 移除网址中多余的 `/index.html`
TODO
### 向搜索引擎提交 URLs
为了使博客文章更好的被搜索引擎收录,推荐使用插件 [hexo-submit-urls-to-search-engine](https://github.com/cjh0613/hexo-submit-urls-to-search-engine) 来主动向搜索引擎提交 URLs。使用步骤如下:
- 安装
在 Hexo 根目录下执行指令:
```bash
$ npm install hexo-submit-urls-to-search-engine --save
```
- 使用
在 Hexo 根目录下的 _config.yml 文件中,添加以下配置项:
```yaml
hexo_submit_urls_to_search_engine:
# 提交最新的 n 个链接
count: 10
# 提交修改时间在 n 秒内的链接,单位秒。例如:
# 有两个文件 a.txt 和 b.txt,分别对应 https://yoursite.com/a.txt 和 https://yoursite.com/b.txt
# 其更新时间距当前分别为 800s 和 1000s(period 字段设为 900)
# 则 https://yoursite.com/a.txt 会被提交,https://yoursite.com/b.txt 不会
period: 900
# 提交链接的条件,可选值:count | period。不同取值的含义如下:
# count :应用上述的 count 配置项
# period:应用上述的 period 配置项
submit_condition: count
# 是否向 Google 提交,可选值:1 | 0(0:否;1:是)
google: 0
# 是否向 Bing 提交,可选值:1 | 0(0:否;1:是)
bing: 1
# 是否向 Baidu 提交,可选值:1 | 0(0:否;1:是)
baidu: 1
# 文本文档的地址,要推送的链接会保存在此文本文档里
txt_path: submit_urls.txt
# 在百度站长平台中注册的域名
baidu_host: https://yoursite.com/
# 百度自动推送 API 的 token(获取方式见插件文档:https://cjh0613.github.io/blog/20200603HexoSubmitUrlsToSearchEngine.html)
baidu_token: xxxxx
# 在必应站长平台中注册的域名
bing_host: https://yoursite.com/
# 必应自动推送 API 的 token(获取方式见插件文档:https://cjh0613.github.io/blog/20200603HexoSubmitUrlsToSearchEngine.html)
bing_token: xxxxx
# 在谷歌站长平台中注册的域名
google_host: https://yoursite.com/
# 存放 google key 的 json 文件,放于网站根目录
google_key_file: google_key.json
```
::: tip
当然你也可以使用环境变量记录私钥,这样即使源码放在公开仓库,也不会泄露秘钥。
:::
完成上述配置后,你需要在 Hexo 根目录下运行 `hexo generate` 指令,该指令会生成一个 .txt 文件来存储要推送的链接。
最后,在 Hexo 根目录下的 _config.yml 文件中,修改以下配置项,即可实现在执行 `hexo deploy` 指令时,自动推送你的链接:
```yaml
deploy:
- type: cjh_google_url_submitter
- type: cjh_bing_url_submitter
- type: cjh_baidu_url_submitter
```
如果你使用插件时**遇到了问题**或者想进行**更详细的设置**,请查看插件的[文档](https://cjh0613.github.io/blog/20200603HexoSubmitUrlsToSearchEngine.html)
## 打包、压缩资源文件 <Badge text="Disrelated" type="warning"/>
::: danger
......
......@@ -741,6 +741,33 @@ busuanzi:
$ hexo clean && hexo s
```
### 辅助搜索按钮 <Badge text="Beta" type="warning"/> <Badge text="v2.2.1"/>
该功能是使用**搜索引擎**和**你输入的关键字**,来搜索你的博客网站。
启用该功能的前提是:启用了 [Algolia 搜索](#algolia-搜索) 或 [本地搜索](#本地搜索)。使用步骤如下:
1. 启用 [Algolia 搜索](#algolia-搜索) 或 [本地搜索](#本地搜索)
2. 修改主题配置文件
```yaml
assist_search:
# 是否启用
enable: true
# 谷歌搜索的按钮。点击该按钮后,会通过 www.google.com 来搜索你输入的字符。
google: true
# 必应搜索的按钮。点击该按钮后,会通过 www.bing.com 来搜索你输入的字符。
bing: true
# 百度搜索的按钮。点击该按钮后,会通过 www.baidu.com 来搜索你输入的字符。
baidu: true
```
3. 重启 Hexo 服务器
```bash
$ hexo clean && hexo s
```
## 数学公式
想要解析页面中的数学公式,首先,你需要修改主题配置文件,启用该功能,并选择解析引擎(默认是 KaTeX 引擎):
......
......@@ -76,6 +76,14 @@ local_search:
input_placeholder: Search for Posts (Support multiple keywords)
hits_empty: We didn't find any results for the search
# Auxiliary search buttons
assist_search:
prompt_label: "Search by:"
prompt_input: Please enter characters
google: Google
bing: Bing
baidu: Baidu
# Reward
reward:
btn_text: Buy me a coffee
......
......@@ -76,6 +76,14 @@ local_search:
input_placeholder: 搜索文章(支持多关键词,请用空格分隔)
hits_empty: 没有找到任何搜索结果
# 搜索的辅助按钮
assist_search:
prompt_label: "使用搜索:"
prompt_input: 请输入字符
google: 谷歌
bing: 必应
baidu: 百度
# 打赏
reward:
btn_text: 请我喝杯咖啡~
......
......@@ -76,6 +76,14 @@ local_search:
input_placeholder: 檢索文章(支持多關鍵字,請用空格分隔)
hits_empty: 沒有找到任何檢索結果
# 搜索的輔助按鈕
assist_search:
prompt_label: "使用搜索:"
prompt_input: 請輸入字符
google: 谷歌
bing: 必應
baidu: 百度
# 打賞
reward:
btn_text: 請我喝杯咖啡~
......
......@@ -15,6 +15,19 @@
});
}
var assistSearch = 'undefined';
if (theme.assist_search.enable) {
assistSearch = [];
for (var key in theme.assist_search) {
var value = theme.assist_search[key];
if (value && key !== 'enable') {
assistSearch.push(key);
}
}
assistSearch = JSON.stringify(assistSearch);
}
var fontIcon = 'undefined';
if (theme.icon) {
fontIcon = JSON.stringify({
......@@ -146,6 +159,7 @@ script.
var CONFIG = {
root: '!{ config.root }',
algolia: !{ algolia },
assistSearch: !{ assistSearch },
fontIcon: !{ fontIcon },
sidebar: !{ sidebar },
header: !{ header },
......
......@@ -4,6 +4,8 @@ div.search-popup
span.search-close
div.search-input
include ./assist-btns.pug
div.search-results
div#algolia-hits
......
if theme.assist_search.enable
div.search-btns
= __('assist_search.prompt_label')
if theme.assist_search.google
span.search-btns-item.search-btns-item--google
i(class=theme.icon.google)
= __('assist_search.google')
if theme.assist_search.bing
span.search-btns-item.search-btns-item--bing
i(class=theme.icon.bing)
= __('assist_search.bing')
if theme.assist_search.baidu
span.search-btns-item.search-btns-item--baidu
i(class=theme.icon.baidu)
= __('assist_search.baidu')
......@@ -5,4 +5,6 @@ div.search-popup
div.search-input
input(placeholder= __('local_search.input_placeholder'))
include ./assist-btns.pug
div.search-results
......@@ -241,3 +241,35 @@ script.
Stun.utils.pjaxReloadLocalSearch();
}, false);
function safeOpenUrl(url) {
var newTab = window.open();
newTab.opener = null;
newTab.location = url;
}
function extSearch(engine) {
var engines = {
google: 'https://www.google.com/search?q=',
bing: 'https://cn.bing.com/search?q=',
baidu: 'https://www.baidu.com/s?ie=UTF-8&wd=',
};
var host = window.location.host;
var query = $('.search-input input').val().toLowerCase().trim();
var uri = engines[engine] + query + ' site:' + host;
if (query) {
safeOpenUrl(uri);
} else {
Stun.utils.popAlert('warning', '!{ __("assist_search.prompt_input") }');
}
}
var assistSearchList = window.CONFIG.assistSearch;
if (Array.isArray(assistSearchList)) {
assistSearchList.forEach(function (name) {
document.querySelector('.search-btns-item--' + name).addEventListener('click', function () {
extSearch(name);
}, false);
});
}
search-popup-padding = 1rem;
search-input-height = 2rem;
search-input-padding = 1rem;
search-input-margin = 1rem;
search-assist-height = 2rem;
.search {
&-mask {
......@@ -31,7 +32,7 @@ search-input-padding = 1rem;
}
&-input {
margin: search-input-padding 0;
margin: search-input-margin 0;
input {
border: 2px solid alpha($blue-light, .5);
......@@ -89,4 +90,23 @@ search-input-padding = 1rem;
background-color: $yellow-light;
}
}
&-btns {
height: search-assist-height;
text-align: center;
&-item {
margin: 0 5px;
border-radius: 5px;
padding: 3px 5px;
line-height: search-assist-height;
color: #fff;
background-color: $blue-lighter;
cursor: pointer;
i {
margin: 0 3px;
}
}
}
}
search-results-height = search-popup-padding * 2 + search-input-padding * 2 + search-input-height;
if (hexo-config('assist_search.enable')) {
search-results-height = search-popup-padding * 2 + search-input-margin * 2 + search-input-height + search-assist-height;
} else {
search-results-height = search-popup-padding * 2 + search-input-margin * 2 + search-input-height;
}
.search {
&-input {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册