Skip to content

Commit

Permalink
fix bug in non-HTTPS sites.
Browse files Browse the repository at this point in the history
  • Loading branch information
ddiu8081 committed May 27, 2018
1 parent 45da96b commit 52dab88
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Ghost 0.x版本默认的 Casper 十分好看,可惜1.0版本之后就改成 cm
* 2018.05.25 v1.4.1 标签云支持显示为独立页面
* 2018.05.26 v1.4.2 添加404页面
* 2018.05.26 v1.5.0 添加评论功能
* 2018.05.27 v1.5.1 修复在非https站下无法加载库文件的bug

## 下载与使用

Expand Down
144 changes: 144 additions & 0 deletions assets/js/ghostbot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/**
* GhostBot: A Simple Common Search Engine based on Ghost API
* author: Luna Shu
* Git: https://github.com/LunaYJ/GhostBot.git
* coding: https://git.coding.net/lunayj/GhostBot.git
* https://luna.fancylog.net/
*
* Forked from https://github.com/wssgcg1213/ghostbot
* Original Author: ZeroLing
* Original Author Blog: http://www.zeroling.com/
*
*/
function Ajax() {
"use strict";
var aja = {};
aja.tarUrl = '';
aja.postString = '';
aja.createAjax = function() {
var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
return xmlhttp;
}
aja.xhr = aja.createAjax();
aja.processHandler = function() {
if (aja.xhr.readyState == 4)
if (aja.xhr.status == 200)
aja.resultHandler(aja.xhr.responseText);
}
aja.get = function(tarUrl, callbackHandler) {
aja.tarUrl = tarUrl;
aja.resultHandler = callbackHandler;
aja.xhr.onreadystatechange = aja.processHandler;
aja.xhr.open('get', aja.tarUrl, true);
aja.xhr.send();
}
return aja;
}
var GhostBot = function(options) {
this.defaults = {
result_template: "<a href='{{link}}' class='searchResult'>{{title}}</a>",
info_template: "<h4>Find{{amount}}Articles.</h4>",
}
var opts = this.extend({}, this.defaults, options);
if (opts.inputbox) this.init(opts);
}
GhostBot.prototype.extend = function() {
var _arg = Array.prototype.slice.call(arguments);
for (var i = _arg.length - 1; i > 0; i--) {
var former = _arg[i - 1],
latter = _arg[i];
for (j in latter) former[j] = latter[j];
}
return _arg[0];
}
GhostBot.prototype.init = function(opts) {
var that = this;
this.result_template = opts.result_template;
this.info_template = opts.info_template;
this.target = opts.target;
this.inputbox = opts.inputbox;
this.blogData = [];
this.ajax = new Ajax();
this.loadAPI();
}
GhostBot.prototype.loadAPI = function(){
if (this.inited) return false;
var index = this.index;
var that = this;
var obj = {limit: "all", include: "tags"};
var maxLength = 120;
var blogData = [];
this.ajax.get(ghost.url.api('posts',obj), function(data) {
var searchData = JSON.parse(data).posts;
searchData.forEach(function(arrayItem){
var tag_arr = arrayItem.tags.map(function(v) {
return v.name;
})
if(arrayItem.meta_description == null) { arrayItem.meta_description = '' };
var category = tag_arr.join(", ");
if (category.length < 1){
category = "undefined";
}
var parsedData = {
id : String(arrayItem.id),
title : String(arrayItem.title),
slug : String(arrayItem.slug),
url: String(arrayItem.url),
markdown : String(arrayItem.markdown),
pubDate : String(arrayItem.created_at.split('T')[0]),
tag : category,
link : String(arrayItem.url),
description : String(arrayItem.markdown.substr(0, maxLength)),
content : arrayItem.markdown
};
blogData.push(parsedData);
});
that.items = blogData;
that.inited = true;
that.listen();
})
}
GhostBot.prototype.listen = function() {
var that = this;
this.inputbox.onkeyup = function() {
var ele = that.inputbox;
if (!ele.value) return that.target.innerHTML = '';
var _r = that.search(ele.value); //[{}, {}]
var info_parsed = that.format(that.info_template, {
amount: _r.length
});
var _HTML = info_parsed;
for (i in _r) _HTML += that.format(that.result_template, _r[i]);
that.target.innerHTML = _HTML;
}
}
GhostBot.prototype.format = function(text, obj) {
return text.replace(/{{([^{}]*)}}/g, function(a, b) {
var r = obj[b];
return typeof r === "string" || typeof r === "number" ? r : a
})
}
GhostBot.prototype.search = function(kw) {
var that = this;
var _result = [];
var _reg = new RegExp(kw.toLowerCase());
this.items.forEach(function(i) {
var content = i.markdown;
var title = i.title;
var pubDate = i.pubDate,
url = i.url,
slug = i.slug,
description = i.description;
if(_reg.test(title.toLowerCase() + content.toLowerCase() + url.toLowerCase())){
var link = i.link;
_result.push({
title: title,
link: link,
content: content,
pubData : i.pubDate,
description : i.description
})
}
});
return _result;
}
1 change: 1 addition & 0 deletions assets/js/ghostbot.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions default.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
<link rel="stylesheet" type="text/css" href="{{asset "css/typo.css"}}" />
<link rel="stylesheet" type="text/css" href="{{asset "css/moegi.css"}}" />
<link rel="stylesheet" type="text/css" href="{{asset "fonts/iconfont.css"}}">
<link rel="stylesheet" href="https://apps.bdimg.com/libs/highlight.js/9.1.0/styles/default.min.css">
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="//apps.bdimg.com/libs/highlight.js/9.1.0/styles/default.min.css">
<script src="//cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
{{!-- <script type="text/javascript" src="{{asset "ghostHunter/dist/jquery.ghosthunter.js"}}"></script> --}}
{{!-- <script type="text/javascript" src="{{asset "js/ghostbot.js"}}"></script> --}}
{{!-- <script src="https://use.typekit.net/ueu5sio.js"></script> --}}
{{!-- <script>try{Typekit.load({ async: true });}catch(e){}</script> --}}
{{! Ghost outputs important style and meta data with this tag }}
Expand All @@ -36,7 +38,7 @@
<button id="go-to-top" class="go-to-top"></button>

<script type="text/javascript" src="{{asset "js/moegi.js"}}"></script>
<script src="https://apps.bdimg.com/libs/highlight.js/9.1.0/highlight.min.js"></script>
<script src="//apps.bdimg.com/libs/highlight.js/9.1.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
{{! Ghost outputs important scripts and data with this tag }} {{ghost_foot}}
</body>
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
{
"name": "Moegi",
"description": "An elegant Ghost theme.",
"version": "1.5.0",
"version": "1.5.1",
"engines": {
"ghost": ">=1.0.0"
},
"license": "MIT",
"author": {
"email": "i@ddiu.site"
"name": "ddiu8081",
"email": "i@ddiu.site",
"url": "https://ddiu.site"
},
"config": {
"posts_per_page": 8
}
},
"homepage": "https://github.com/ddiu8081/ghost-theme-Moegi"
}
4 changes: 2 additions & 2 deletions page-archives.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<header class="post-header short-diver">
<h1 class="post-title">归档</h1>
<section class="post-meta">
{{#get "posts" as |posts postPages|}}
共有{{postPages.total}}篇文章
{{#get "posts" as |posts pages|}}
共有{{pages.total}}篇文章
{{/get}}
</section>
</header>
Expand Down
55 changes: 55 additions & 0 deletions page-search.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{{!< default}}

{{#post}}
<script src="https://cdn.bootcss.com/moment.js/2.22.1/moment.min.js"></script>

<header>
<div class="post-blog-title"><a href="{{@blog.url}}"><i class="iconfont icon-back"></i>&nbsp;{{@blog.title}}</a></div>
</header>

<main role="main">
<article class="page-archives">
<header class="post-header short-diver">
<h1 class="post-title">搜索</h1>
<section class="post-meta">
{{#get "posts" as |posts pages|}}
共有{{pages.total}}篇文章
{{/get}}
</section>
</header>

<div id="archives-list" class="typo"></div>

</article>
</main>
<script>
$.get(ghost.url.api('posts', {
limit: 'all',
order: "published_at desc"
})).done(function(data) {
var posts = data.posts;
var count = posts.length;
for (var i = 0, pre_year = 0; i < count; i++) {
//调用moment.js对时间戳进行操作
var time = moment(posts[i].published_at);
var year = time.year();
var title = posts[i].title;
var url = posts[i].url;
if (year != pre_year) {
var html = "<h2>"+year+"</h2>"+
"<ul id='year-"+year+"'></ul>"
$(html).appendTo("#archives-list");
}
var html = "<li><time>"+time.format("MM-DD")+"</time><a href='"+url+"'>"+title+"</a></li>";
$(html).appendTo("#year-"+year);
pre_year = year;
}
}).fail(function(err) {
console.log(err);
});
</script>



{{/post}}
4 changes: 2 additions & 2 deletions partials/toc.hbs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<div id="toc"></div>

{{!-- 引入 支持文件 及 Tocify 插件 --}}
<script src="https://cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdn.bootcss.com/jquery.tocify/1.9.0/javascripts/jquery.tocify.min.js"></script>
<script src="//cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="//cdn.bootcss.com/jquery.tocify/1.9.0/javascripts/jquery.tocify.min.js"></script>

{{!-- 配置及生成目录 --}}
<script>
Expand Down

0 comments on commit 52dab88

Please sign in to comment.