Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 38 additions & 26 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,29 @@ var _pick = require('lodash.pick');

function filterHTMLTags(str) {
return str ? str
.replace(/\<(?!img|br).*?\>/g, "")
.replace(/\r?\n|\r/g, '')
.replace(/<img(.*)>/g, ' [Figure] ') : null
.replace(/\<(?!img|br).*?\>/g, "")
.replace(/\r?\n|\r/g, '')
.replace(/<img(.*)>/g, ' [Figure] ') : null
}

function fetchCovers(str) {
var temp,
imgURLs = [],
rex = /<img[^>]+src="?([^"\s]+)"(.*)>/g;
while ( temp = rex.exec( str ) ) {
imgURLs.push( temp[1] );
while (temp = rex.exec(str)) {
imgURLs.push(temp[1]);
}
return imgURLs.length > 0 ? imgURLs : null;
}

function fetchCover(str) {
var covers = fetchCovers(str)
return covers ? covers[0] : null;
return covers ? covers[0] : null;
}

module.exports = function (cfg, site) {
module.exports = function(cfg, site) {

var restful = cfg.hasOwnProperty('restful') ? cfg.restful :
{
var restful = cfg.hasOwnProperty('restful') ? cfg.restful : {
site: true,
posts_size: 10,
posts_props: {
Expand All @@ -49,21 +50,22 @@ module.exports = function (cfg, site) {
use_tag_slug: false,
post: true,
pages: false,
simple_post_map: false
},

posts = site.posts.sort('-date').filter(function (post) {
posts = site.posts.sort('-date').filter(function(post) {
return post.published;
}),

posts_props = (function () {
posts_props = (function() {
var props = restful.posts_props;

return function (name, val) {
return function(name, val) {
return props[name] ? (typeof val === 'function' ? val() : val) : null;
}
})(),

postMap = function (post) {
postMap = function(post) {
return {
title: posts_props('title', post.title),
slug: posts_props('slug', post.slug),
Expand All @@ -77,8 +79,8 @@ module.exports = function (cfg, site) {
cover: posts_props('cover', post.cover || fetchCover(post.content)),
content: posts_props('content', post.content),
raw: posts_props('raw', post.raw),
categories: posts_props('categories', function () {
return post.categories.map(function (cat) {
categories: posts_props('categories', function() {
return post.categories.map(function(cat) {
const name = (
cfg.restful.use_category_slug && cat.slug
) ? cat.slug : cat.name;
Expand All @@ -88,8 +90,8 @@ module.exports = function (cfg, site) {
};
});
}),
tags: posts_props('tags', function () {
return post.tags.map(function (tag) {
tags: posts_props('tags', function() {
return post.tags.map(function(tag) {
const name = (
cfg.restful.use_tag_slug && tag.slug
) ? tag.slug : tag.name;
Expand All @@ -101,9 +103,16 @@ module.exports = function (cfg, site) {
})
};
},
simplepostMap = function(post) {
return {
title: posts_props('title', post.title),
slug: posts_props('slug', post.slug),
path: posts_props('path', 'api/articles/' + post.slug + '.json'),

cateReduce = function (cates, kind) {
return cates.reduce(function (result, item) {
};
},
cateReduce = function(cates, kind) {
return cates.reduce(function(result, item) {
if (!item.length) return result;

let use_slug = null;
Expand All @@ -117,28 +126,31 @@ module.exports = function (cfg, site) {
}

const name = (use_slug && item.slug) ? item.slug : item.name;
let simple_post_map = cfg.restful.simple_post_map;
// Select whether to generate content node
let postlist = simple_post_map ? item.posts.map(simplepostMap) : item.posts.map(postMap);

return result.concat(pagination(item.path, posts, {
perPage: 0,
data: {
name: name,
path: 'api/' + kind + '/' + name + '.json',
postlist: item.posts.map(postMap)
postlist: postlist
}

}));
}, []);
},

catesMap = function (item) {
catesMap = function(item) {
return {
name: item.data.name,
path: item.data.path,
count: item.data.postlist.length
};
},

cateMap = function (item) {
cateMap = function(item) {
var itemData = item.data;
return {
path: itemData.path,
Expand Down Expand Up @@ -226,7 +238,7 @@ module.exports = function (cfg, site) {
}

if (restful.post) {
apiData = apiData.concat(posts.map(function (post) {
apiData = apiData.concat(posts.map(function(post) {
var path = 'api/articles/' + post.slug + '.json';
return {
path: path,
Expand All @@ -242,13 +254,13 @@ module.exports = function (cfg, site) {
keywords: cfg.keyword,
content: post.content,
more: post.more,
categories: post.categories.map(function (cat) {
categories: post.categories.map(function(cat) {
return {
name: cat.name,
path: 'api/categories/' + cat.name + '.json'
};
}),
tags: post.tags.map(function (tag) {
tags: post.tags.map(function(tag) {
return {
name: tag.name,
path: 'api/tags/' + tag.name + '.json'
Expand All @@ -260,7 +272,7 @@ module.exports = function (cfg, site) {
}

if (restful.pages) {
apiData = apiData.concat(site.pages.data.map(function (page) {
apiData = apiData.concat(site.pages.data.map(function(page) {
var safe_title = page.title.replace(/[^a-z0-9]/gi, '-').toLowerCase()
var path = 'api/pages/' + safe_title + '.json';

Expand Down