-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
216 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> {{@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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters