-
Notifications
You must be signed in to change notification settings - Fork 15
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
0 parents
commit 682d0ab
Showing
4 changed files
with
78 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
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 @@ | ||
hexo.extend.tag.register('recommended_posts', require('./lib/recommender')(hexo), {async: true}); |
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,56 @@ | ||
var rp = require('request-promise'); | ||
|
||
module.exports = function (hexo) { | ||
return function (args) { | ||
var log = hexo.log; | ||
var config = hexo.config; | ||
var recommended_posts_config = config.recommended_posts; | ||
|
||
var server = recommended_posts_config.server; | ||
var timeoutInMillis = recommended_posts_config.timeout; | ||
var internal = parseInt(recommended_posts_config.internalLinks); | ||
var external = parseInt(recommended_posts_config.externalLinks); | ||
|
||
var post = this; | ||
|
||
if (post.tags.data.length == 0) { | ||
log.warn('To have better recommendation, specify tags for ' + post.title); | ||
} | ||
|
||
return rp({ | ||
method: 'POST', | ||
baseUrl: server, | ||
uri: 'posts', | ||
body: { | ||
permalink: post.permalink, | ||
title: post.title, | ||
tags: post.tags.data.map(tag => tag.name) | ||
}, | ||
json: true, | ||
timeout: timeoutInMillis | ||
}).then(function (body) { | ||
log.info('Post ' + post.title + ' uploaded to recommendation server') | ||
}).then(function () { | ||
return rp({ | ||
baseUrl: server, | ||
uri: 'recommendation', | ||
qs: { | ||
permalink: post.permalink, | ||
internal: internal, | ||
external: external | ||
}, | ||
json: true, | ||
timeoutInMillis: timeoutInMillis | ||
}) | ||
}).then(function (links) { | ||
if (links.length == 0) { | ||
log.warn("No posts recommended by the server for " + post.title); | ||
} else { | ||
var items = links.map(link => '<li><a href="' + link.permalink + '">' + link.title + '</a></li>'); | ||
return '<div class="recommended_posts"><ul>' + items.join("\n") + '</ul></div>'; | ||
} | ||
}).catch(function (err) { | ||
log.warn("Unable to fetch recommendation from server " + err.message); | ||
}); | ||
} | ||
} |
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,20 @@ | ||
{ | ||
"name": "hexo-recommended-posts", | ||
"version": "0.0.1", | ||
"description": "Recommended Posts Plugin for Hexo", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [ | ||
"recommendation", | ||
"hexo", | ||
"plugin" | ||
], | ||
"author": "Hui Wang", | ||
"license": "ISC", | ||
"dependencies": { | ||
"request": "^2.83.0", | ||
"request-promise": "^4.2.2" | ||
} | ||
} |