Skip to content

Commit

Permalink
add: 添加iblog为默认案例
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongwilee committed Apr 5, 2017
1 parent 6818cfb commit 79588c7
Show file tree
Hide file tree
Showing 112 changed files with 2,081 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
config/server.json

/node_modules
npm-debug.*

.DS_Store
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- stable
- "7.6.0"
sudo: false
# uncomment this line if your project needs to run something other than `rake`:
# script: bundle exec rspec spec
2 changes: 1 addition & 1 deletion app/blog/static/js/common/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
var bindEvent = function() {
$('#search_form_wd').on('keyup', function(evt) {
var thisVal = $(this).val();
$('#search_form_wd_hidden').val(thisVal + ' site:mlsfe.biz');
$('#search_form_wd_hidden').val(thisVal + ' site:feclub.cn');
});

$('#content_post').on('click', '.post-link', function(evt) {
Expand Down
31 changes: 31 additions & 0 deletions app/iblog/controller/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const base = require('./base.js');

module.exports = {
'user': {
info: async function() {
let access_token = this.cookies.get(base.config.token_cookie);

await this.proxy(`github_api:get:user?access_token=${access_token}`);
}
},
'comments': {
create: async function() {
let access_token = this.cookies.get(base.config.token_cookie);

let issueId = parseInt(this.request.body.issues_id) || 1;

await this.proxy(`github_api:post:/repos/${base.config.owner}/${base.config.repo}/issues/${issueId}/comments`, {
headers: { 'Authorization': `token ${access_token}` }
})
},
list: async function() {
let issueId = parseInt(this.query.issues_id) || 1;

await this.proxy(`github_api:get:/repos/${base.config.owner}/${base.config.repo}/issues/${issueId}/comments`, {
headers: { 'Authorization': `token ${base.config.token}` }
})
}
}
}
221 changes: 221 additions & 0 deletions app/iblog/controller/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
const url = require('url');
const querystring = require('querystring');

/**
* 常用关键的配置
* @type {Object}
*/
const config = {
// username & repo name
owner: 'xiongwilee',
repo: 'blog',

// OAuth applications clientId & clientSecret
client_id: '4b1f5517210d01e86f0d',
client_secret: '8806946fefe51e423c18d36cf8ab0c946e68a94f',

// user token cookieName
token_cookie: 'ACCESS_TOKEN',

// Personal access tokens
token: global.config.constant.token,

// 站点信息
site: {
logo: '',
name: 'XiongWilee',
intro: '知而获智,智达高远',
title: 'XiongWilee - 知而获智,智达高远',
year: new Date().getFullYear(),
banner: 'https://img003.qufenqi.com/products/c0/72/c072f0506c961f6d1652531d60712c40.jpg',
links: [{
name: '趣店技术学院',
url: 'https://qit-team.github.io'
}, {
name: '前端俱乐部',
url: 'https://feclub.cn'
}]
}
}

/**
* 根据需求补充评论列表数据
* @param {Object} comments comments请求数据
* @return {Object} 分页及文章列表数据
*/
function getCommentsList(comments) {
if (!comments.body || !comments.headers) return;

let pageInfo = getPage(comments.headers.link);
let pageList = [];

comments.body.forEach((item) => {
pageList.push(getComments(item));
});

return {
page: pageInfo,
list: pageList
}
}

/**
* 根据需求补充评论内容数据
* @param {Object} post 评论数据
* @return {Object} 补充之后的文章数据
*/
function getComments(comments) {
if (!comments || !comments.body) return {};

Object.assign(comments, {
create_time: formatTime(comments.created_at),
update_time: formatTime(comments.updated_at)
})

return comments;
}

/**
* 根据需求补充文章列表数据
* @param {Object} issues issues请求数据
* @return {Object} 分页及文章列表数据
*/
function getPostList(issues) {
if (!issues.body || !issues.headers) return;

let pageInfo = getPage(issues.headers.link);
let pageList = [];

issues.body.forEach((item) => {
// 如果文章不存在分类,则不展示出来,该功能先取消
// if (!item.labels || item.labels.length === 0) return;

pageList.push(getPost(item));
});

return {
page: pageInfo,
list: pageList
}
}

/**
* 根据需求补充文章内容数据
* @param {Object} post 文章数据
* @return {Object} 补充之后的文章数据
*/
function getPost(post) {
if (!post || !post.body) return {};


let postQuery = getPostQuery(post.body);
let postIntro = postQuery.intro || getPostIntro(post.body);

Object.assign(post, {
intro: postIntro,
query: postQuery,
create_time: formatTime(post.created_at),
update_time: formatTime(post.updated_at)
})

return post;
}

/**
* 格式化文档时间
* @param {String} time 时间戳
* @return {String} 格式化之后的时间
*/
function formatTime(time) {
let date = new Date(time);

return (date.getFullYear() + '-' +
zeroPad(date.getMonth() + 1) + '-' +
zeroPad(date.getDate()) + ' ' +
zeroPad(date.getHours()) + ':' +
zeroPad(date.getMinutes()) + ':' +
zeroPad(date.getSeconds()));

function zeroPad(num) {
return ('0' + num).slice(-2)
}
}

/**
* 获取文章的简介,即前5行内容
* @param {String} post 文章内容
* @param {Number} line 行数,默认为10行
* @return {String} 文章简介
*/
function getPostIntro(body) {
let isBlankReg = /^\s+$/,
start = 0;
return body.split('\n').filter((item) => {
if (start < 5 && !isBlankReg.test(item)) {
start++;
return true;
}
}).join('\n')
}

/**
* 获取文章的配置参数,在文章头部通过[key]: value 的形式
* @param {String} body 文章内容
* [intro]: 文章的介绍文章的介绍文章的介绍
* @return {Object} 文章的配置参数
*/
function getPostQuery(body) {
if (!body) return {};

let result = {};
let commentReg = /^\[(\w+)\]\:([\s|\S]+)/;
body.split('\n').every((item) => {
let itemMatch = item.match(commentReg);
if (itemMatch && itemMatch.length == 3) {
result[itemMatch[1]] = itemMatch[2];
return true;
} else {
return false;
}
})

return result;
}

/**
* 通过头信息中的link字段,获取当前的分页信息
* @param {String} link 头信息中的link字段
* <https://api.github.com/repositories/11551538/issues?state=all&page=4>; rel=\"next\",
* <https://api.github.com/repositories/11551538/issues?state=all&page=32>; rel=\"last\",
* <https://api.github.com/repositories/11551538/issues?state=all&page=1>; rel=\"first\",
* <https://api.github.com/repositories/11551538/issues?state=all&page=2>; rel=\"prev\"
* @return {Object}
*/
function getPage(link) {
if (!link) return {};

let result = {};
let reg = /(<([\S]+)>)[\S\s]+\"([\w]+)\"/;
link.split(',').forEach((item) => {
let itemMatch = item.match(reg);
if (itemMatch && itemMatch.length === 4) {
let query = querystring.parse(url.parse(itemMatch[2]).query);
result[itemMatch[3]] = parseInt(query.page) || 1;
}
})

return result;
}

exports.config = config;

exports.getCommentsList = getCommentsList;
exports.getComments = getComments;
exports.getPostList = getPostList;
exports.getPost = getPost;
exports.formatTime = formatTime;
exports.getPostIntro = getPostIntro;
exports.getPostQuery = getPostQuery;
exports.getPage = getPage;

exports.__controller__ = false
28 changes: 28 additions & 0 deletions app/iblog/controller/defaultCtrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const base = require('./base.js');

module.exports = async function() {
// 获取用户github信息
await this.proxy({
ownerInfo: `github_api:user?access_token=${base.config.token}`,
repoInfo: `github_api:/repos/${base.config.owner}/${base.config.repo}`,
labelInfo: `github_api:/repos/${base.config.owner}/${base.config.repo}/labels`
}, {
headers: { Authorization: `token ${base.config.token}` }
});

// 仓储信息
this.repoInfo = this.backData.repoInfo || {};
// 仓储信息
this.labelInfo = this.backData.labelInfo || {};
// 用户信息
this.userInfo = this.backData.userInfo || {};
// 管理者信息
this.ownerInfo = this.backData.ownerInfo || {};
// 站点信息
this.siteInfo = Object.assign({
description: this.repoInfo.description
}, base.config.site)

}
31 changes: 31 additions & 0 deletions app/iblog/controller/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const base = require('./base.js');

exports.index = async function() {
await this.bindDefault();

let page = parseInt(this.query.page) || 1;

let res = await this.proxy({
issues: `github_api:/repos/${base.config.owner}/${base.config.repo}/issues?state=open&filter=created&page=${page}`
// issues: `github_api:/repos/koajs/koa/issues?state=all&page=3`
}, {
headers: { 'Authorization': `token ${base.config.token}` }
})

let postInfo = base.getPostList(res.issues);

Object.assign(postInfo.page, {
curr: page,
total: postInfo.page.last || 1
})

await this.render('home', {
ownerInfo: this.ownerInfo,
labelInfo: this.labelInfo,
siteInfo: this.siteInfo,
userInfo: this.userInfo,
postInfo: postInfo
})
}
Loading

0 comments on commit 79588c7

Please sign in to comment.