akashacms-blog-podcast
Advanced tools
Comparing version 0.4.4 to 0.4.5
490
index.js
/** | ||
* Copyright 2015 David Herron | ||
* | ||
* | ||
* This file is part of AkashaCMS-embeddables (http://akashacms.com/). | ||
@@ -19,150 +19,220 @@ * | ||
var path = require('path'); | ||
var util = require('util'); | ||
var url = require('url'); | ||
var async = require('async'); | ||
'use strict'; | ||
var logger; | ||
var akasha; | ||
var config; | ||
const path = require('path'); | ||
const util = require('util'); | ||
const url = require('url'); | ||
const async = require('async'); | ||
const akasha = require('akasharender'); | ||
const co = require('co'); | ||
/** | ||
* prepareConfig - Simplify the configuration object by filling in defaults | ||
* that make sense for blogPodcast sites. | ||
*/ | ||
module.exports.prepareConfig = function(akashacms, config) { | ||
if (!config) { | ||
config = {}; | ||
const log = require('debug')('akasha:blog-podcast-plugin'); | ||
const error = require('debug')('akasha:error-blog-podcast-plugin'); | ||
module.exports = class BlogPodcastPlugin extends akasha.Plugin { | ||
constructor() { | ||
super("akashacms-blog-podcast"); | ||
} | ||
configure(config) { | ||
this._config = config; | ||
config.addPartialsDir(path.join(__dirname, 'partials')); | ||
config.addMahabhuta(mahabhuta); | ||
} | ||
addBlogPodcast(config, name, blogPodcast) { | ||
if (!config.blogPodcast) config.blogPodcast = []; | ||
config.blogPodcast[name] = blogPodcast; | ||
return config; | ||
} | ||
onSiteRendered(config) { | ||
// console.log(`blog-podcast onSiteRendered ${util.inspect(config.blogPodcast)}`); | ||
return co(function* () { | ||
for (var blogcfgkey in config.blogPodcast) { | ||
var blogcfg = config.blogPodcast[blogcfgkey]; | ||
// console.log(`blog-podcast blogcfg ${util.inspect(blogcfg)}`); | ||
var documents = yield findBlogDocs(config, undefined, blogcfg); | ||
var count = 0; | ||
var documents2 = documents.filter(doc => { | ||
if (typeof maxEntries === "undefined" | ||
|| (typeof maxEntries !== "undefined" && count++ < maxEntries)) { | ||
return true; | ||
} else return false; | ||
}); | ||
// log('blog-news-river documents2 '+ util.inspect(documents2)); | ||
var rssitems = documents2.map(doc => { | ||
return { | ||
title: doc.metadata.title, | ||
description: doc.metadata.teaser ? doc.metadata.teaser : "", | ||
url: config.root_url +'/'+ doc.renderpath, | ||
date: doc.metadata.publicationDate ? doc.metadata.publicationDate : doc.stat.mtime | ||
}; | ||
}); | ||
// console.log(`GENERATE RSS ${config.renderDestination + blogcfg.rssurl} ${util.inspect(rssitems)}`); | ||
yield akasha.generateRSS(config, blogcfg, { | ||
feed_url: config.renderDestination + blogcfg.rssurl, | ||
pubDate: new Date() | ||
}, | ||
rssitems, blogcfg.rssurl); | ||
} | ||
}); | ||
} | ||
config = akashacms.prepareConfig(config); | ||
// If no config function, then set up a default set of plugins | ||
if (!config.config) { | ||
config.config = function(akasha) { | ||
akasha.registerPlugins([ | ||
{ name: 'akashacms-breadcrumbs', plugin: require('akashacms-breadcrumbs') }, | ||
{ name: 'akashacms-embeddables', plugin: require('akashacms-embeddables') }, | ||
{ name: 'akashacms-blog-podcast', plugin: require('akashacms-blog-podcast') }, | ||
{ name: 'akashacms-social-buttons', plugin: require('akashacms-social-buttons') }, | ||
{ name: 'akashacms-base', plugin: require('akashacms-base') } | ||
]); | ||
}; | ||
} | ||
return config; | ||
}; | ||
} | ||
/** | ||
* startup - Simplify configuration for a Blog using Grunt | ||
* | ||
blogPodcast: { | ||
"news": { | ||
rss: { | ||
title: "AkashaCMS News", | ||
description: "Announcements and news about the AkashaCMS content management system", | ||
site_url: "http://akashacms.com/news/index.html", | ||
image_url: "http://akashacms.com/logo.gif", | ||
managingEditor: 'David Herron', | ||
webMaster: 'David Herron', | ||
copyright: '2015 David Herron', | ||
language: 'en', | ||
categories: [ "Node.js", "Content Management System", "HTML5", "Static website generator" ] | ||
}, | ||
rssurl: "/news/rss.xml", | ||
matchers: { | ||
layouts: [ "blog.html.ejs" ], | ||
path: /^news\// | ||
} | ||
}, | ||
"howto": { | ||
rss: { | ||
title: "AkashaCMS Tutorials", | ||
description: "Tutorials about using the AkashaCMS content management system", | ||
site_url: "http://akashacms.com/howto/index.html", | ||
image_url: "http://akashacms.com/logo.gif", | ||
managingEditor: 'David Herron', | ||
webMaster: 'David Herron', | ||
copyright: '2015 David Herron', | ||
language: 'en', | ||
categories: [ "Node.js", "Content Management System", "HTML5", "HTML5", "Static website generator" ] | ||
}, | ||
rssurl: "/howto/rss.xml", | ||
matchers: { | ||
layouts: [ "blog.html.ejs" ], | ||
path: /^howto\// | ||
} | ||
} | ||
}, | ||
* | ||
*/ | ||
module.exports.startup = function(akashacms, config) { | ||
module.exports.prepareConfig(akashacms, config); | ||
// Now that we've prepared the config object, call akashacms.config | ||
akashacms.config(config); | ||
}; | ||
function findBlogDocs(config, metadata, blogcfg) { | ||
/** | ||
* Add ourselves to the config data. | ||
**/ | ||
module.exports.config = function(_akasha, _config) { | ||
akasha = _akasha; | ||
config = _config; | ||
logger = akasha.getLogger("blog-podcast"); | ||
config.root_partials.push(path.join(__dirname, 'partials')); | ||
return module.exports; | ||
return akasha.documentSearch(config, { | ||
// rootPath: docDirPath, | ||
pathmatch: blogcfg.matchers.path ? blogcfg.matchers.path : undefined, | ||
renderers: [ akasha.HTMLRenderer ], | ||
layouts: blogcfg.matchers.layouts ? blogcfg.matchers.layouts : undefined, | ||
rootPath: blogcfg.rootPath ? blogcfg.rootPath : undefined | ||
}) | ||
.then(documents => { | ||
// console.log('findBlogDocs '+ util.inspect(documents)); | ||
documents.sort(function(a, b) { | ||
var aPublicationDate = Date.parse( | ||
a.metadata.publicationDate ? a.metadata.publicationDate : a.stat.mtime | ||
); | ||
var bPublicationDate = Date.parse( | ||
b.metadata.publicationDate ? b.metadata.publicationDate : b.stat.mtime | ||
); | ||
if (aPublicationDate < bPublicationDate) return -1; | ||
else if (aPublicationDate === bPublicationDate) return 0; | ||
else return 1; | ||
}); | ||
documents.reverse(); | ||
return documents; | ||
}) | ||
.then(documents => { | ||
// log(util.inspect(documents)); | ||
return documents; | ||
}); | ||
}; | ||
var findBlogDocs = function(config, metadata, blogcfg) { | ||
var documents = akasha.findMatchingDocuments(blogcfg.matchers); | ||
documents.sort(function(a, b) { | ||
var aPublicationDate = Date.parse( | ||
a.frontmatter.yaml.publicationDate | ||
? a.frontmatter.yaml.publicationDate | ||
: a.stat.mtime | ||
); | ||
var bPublicationDate = Date.parse( | ||
b.frontmatter.yaml.publicationDate | ||
? b.frontmatter.yaml.publicationDate | ||
: b.stat.mtime | ||
); | ||
if (aPublicationDate < bPublicationDate) return -1; | ||
else if (aPublicationDate === bPublicationDate) return 0; | ||
else return 1; | ||
function findBlogIndexes(config, metadata, blogcfg) { | ||
if (!blogcfg.indexmatchers) return Promise.resolve([]); | ||
return akasha.documentSearch(config, { | ||
pathmatch: blogcfg.indexmatchers.path ? blogcfg.indexmatchers.path : undefined, | ||
renderers: [ akasha.HTMLRenderer ], | ||
layouts: blogcfg.indexmatchers.layouts ? blogcfg.indexmatchers.layouts : undefined, | ||
rootPath: blogcfg.rootPath ? blogcfg.rootPath : undefined | ||
}); | ||
documents.reverse(); | ||
return documents; | ||
}; | ||
} | ||
module.exports.mahabhuta = [ | ||
var mahabhuta = [ | ||
function($, metadata, dirty, done) { | ||
var elements = []; | ||
var documents, blogcfg; | ||
$('blog-news-river').each(function(i, elem) { elements.push(elem); }); | ||
if (elements.length > 0) { | ||
blogcfg = config.blogPodcast[metadata.blogtag]; | ||
if (!blogcfg) { | ||
return done(new Error('No blog configuration found for blogtag '+ metadata.blogtag)); | ||
} else { | ||
documents = findBlogDocs(config, metadata, blogcfg); | ||
async.eachSeries(elements, (element, next) => { | ||
var blogtag = $(element).attr("blogtag"); | ||
if (!blogtag) { | ||
blogtag = metadata.blogtag; | ||
} | ||
// documents = findBlogDocs(config, metadata, blogcfg); | ||
} | ||
async.eachSeries(elements, function(element, next) { | ||
if (! metadata.blogtag) { | ||
next(new Error("no blogtag")); | ||
} else if (! config.blogPodcast.hasOwnProperty(metadata.blogtag)) { | ||
next(new Error("no blogPodcast item for "+ metadata.blogtag)); | ||
if (!blogtag) {// no blog tag, skip? error? | ||
error("NO BLOG TAG in blog-news-river"+ metadata.document.path); | ||
return done(new Error("NO BLOG TAG in blog-news-river"+ metadata.document.path)); | ||
} | ||
var maxEntries = $(element).attr('maxentries'); | ||
// console.log(element.name +' '+ metadata.blogtag); | ||
var rssitems = []; | ||
var documents2 = []; | ||
var count = 0; | ||
for (var q = 0; q < documents.length; q++, count++) { | ||
var doc = documents[q]; | ||
// console.log('count='+ count +' maxEntries='+ maxEntries); | ||
if (typeof maxEntries === "undefined" | ||
|| (typeof maxEntries !== "undefined" && count < maxEntries)) { | ||
rssitems.push({ | ||
title: doc.frontmatter.yaml.title, | ||
description: doc.frontmatter.yaml.teaser ? doc.frontmatter.yaml.teaser : "", | ||
url: config.root_url +'/'+ doc.renderedFileName, | ||
date: doc.frontmatter.yaml.publicationDate | ||
? doc.frontmatter.yaml.publicationDate | ||
: doc.stat.mtime | ||
}); | ||
documents2.push(doc); | ||
} // else console.log('skipped'); | ||
// log('blog-news-river '+ blogtag +' '+ metadata.document.path); | ||
var blogcfg = metadata.config.blogPodcast[blogtag]; | ||
if (!blogcfg) return done(new Error('No blog configuration found for blogtag '+ blogtag)); | ||
var _blogcfg = {}; | ||
for (var key in blogcfg) { | ||
_blogcfg[key] = blogcfg[key]; | ||
} | ||
var feedRenderTo = blogcfg.rssurl; | ||
akasha.generateRSS(blogcfg.rss, { | ||
feed_url: config.root_url + feedRenderTo, | ||
pubDate: new Date() | ||
}, | ||
rssitems, feedRenderTo, function(err) { | ||
if (err) logger.error(err); | ||
}); | ||
akasha.partial("blog-news-river.html.ejs", { | ||
documents: documents2, | ||
feedUrl: feedRenderTo | ||
}, | ||
function(err, htmlRiver) { | ||
if (err) next(err); | ||
else { | ||
var maxEntries = $(element).attr('maxentries'); | ||
var template = $(element).attr("template"); | ||
if (!template) template = "blog-news-river.html.ejs"; | ||
var rootPath = $(element).attr('root-path'); | ||
if (rootPath) { | ||
_blogcfg.rootPath = rootPath; | ||
} | ||
var docRootPath = $(element).attr('doc-root-path'); | ||
if (docRootPath) { | ||
_blogcfg.rootPath = path.dirname(docRootPath); | ||
} | ||
findBlogDocs(metadata.config, metadata, _blogcfg) | ||
.then(documents => { | ||
// log('blog-news-river documents '+ util.inspect(documents)); | ||
var count = 0; | ||
var documents2 = documents.filter(doc => { | ||
if (typeof maxEntries === "undefined" | ||
|| (typeof maxEntries !== "undefined" && count++ < maxEntries)) { | ||
return true; | ||
} else return false; | ||
}); | ||
// log('blog-news-river documents2 '+ util.inspect(documents2)); | ||
akasha.partial(metadata.config, template, { | ||
documents: documents2, | ||
feedUrl: _blogcfg.rssurl | ||
}) | ||
.then(htmlRiver => { | ||
$(element).replaceWith(htmlRiver); | ||
next(); | ||
} | ||
}); | ||
}, | ||
function(err) { | ||
}) | ||
.catch(err => { next(err); }); | ||
}) | ||
.catch(err => { done(err); }); | ||
}, | ||
function(err) { | ||
if (err) done(err); | ||
@@ -172,53 +242,119 @@ else done(); | ||
}, | ||
function($, metadata, dirty, done) { | ||
var elements = []; | ||
var documents; | ||
akasha.readDocumentEntry(metadata.documentPath, function(err, docEntry) { | ||
var blogcfg = config.blogPodcast[metadata.blogtag]; | ||
$('blog-next-prev').each(function(i, elem) { elements.push(elem); }); | ||
if (elements.length > 0) { | ||
if (!blogcfg) { | ||
return done(new Error('No blog configuration found for blogtag '+ metadata.blogtag)); | ||
} else { | ||
documents = findBlogDocs(config, metadata, blogcfg); | ||
} | ||
$('blog-news-index').each(function(i, elem) { elements.push(elem); }); | ||
async.eachSeries(elements, (element, next) => { | ||
var blogtag = $(element).attr("blogtag"); | ||
if (!blogtag) { | ||
blogtag = metadata.blogtag; | ||
} | ||
async.eachSeries(elements, function(element, next) { | ||
// what's the current document | ||
// find it within documents | ||
var docIndex = -1; | ||
for (var j = 0; j < documents.length; j++) { | ||
if (documents[j].path === docEntry.path) { | ||
docIndex = j; | ||
} | ||
} | ||
if (docIndex >= 0) { | ||
var prevDoc = docIndex === 0 ? documents[documents.length - 1] : documents[docIndex - 1]; | ||
var nextDoc = docIndex === documents.length - 1 ? documents[0] : documents[docIndex + 1]; | ||
akasha.partial('blog-next-prev.html.ejs', { | ||
prevDoc: prevDoc, nextDoc: nextDoc, thisDoc: docEntry, documents: documents | ||
}, function(err, html) { | ||
if (err) next(err); | ||
else { | ||
$(element).replaceWith(html); | ||
next(); | ||
} | ||
}); | ||
} else { | ||
next(new Error('did not find document in blog')); | ||
} | ||
// prevDoc = | ||
// nextDoc = | ||
// akasha.partial('blog-next-prev.html.ejs', { | ||
// prevDoc: prevDoc, nextDoc: nextDoc | ||
// }) | ||
// next(); | ||
}, | ||
function(err) { | ||
if (err) done(err); | ||
else done(); | ||
}); | ||
if (!blogtag) {// no blog tag, skip? error? | ||
error("NO BLOG TAG in blog-news-index"+ metadata.document.path); | ||
return done(new Error("NO BLOG TAG in blog-news-index"+ metadata.document.path)); | ||
} | ||
var blogcfg = metadata.config.blogPodcast[blogtag]; | ||
if (!blogcfg) return done(new Error('No blog configuration found for blogtag '+ blogtag)); | ||
var template = $(element).attr("template"); | ||
if (!template) template = "blog-news-indexes.html.ejs"; | ||
findBlogIndexes(metadata.config, metadata, blogcfg) | ||
.then(indexDocuments => { | ||
if (indexDocuments.length <= 0) return next(); | ||
return akasha.partial(metadata.config, template, { indexDocuments }) | ||
.then(htmlIndexes => { | ||
$(element).replaceWith(htmlIndexes); | ||
next(); | ||
}); | ||
}) | ||
.catch(err => { next(err); }); | ||
}, | ||
function(err) { | ||
if (err) done(err); | ||
else done(); | ||
}); | ||
}, | ||
function($, metadata, dirty, done) { | ||
var elements = []; | ||
$('blog-rss-icon').each(function(i, elem) { elements.push(elem); }); | ||
async.eachSeries(elements, (element, next) => { | ||
var blogtag = $(element).attr("blogtag"); | ||
if (!blogtag) { | ||
blogtag = metadata.blogtag; | ||
} | ||
if (!blogtag) {// no blog tag, skip? error? | ||
error("NO BLOG TAG in blog-rss-icon"+ metadata.document.path); | ||
return done(new Error("NO BLOG TAG in blog-rss-icon"+ metadata.document.path)); | ||
} | ||
var blogcfg = metadata.config.blogPodcast[blogtag]; | ||
if (!blogcfg) return done(new Error('No blog configuration found for blogtag '+ blogtag)); | ||
akasha.partial(metadata.config, "blog-rss-icon.html.ejs", { | ||
feedUrl: blogcfg.rssurl | ||
}) | ||
.then(htmlIcon => { | ||
$(element).replaceWith(htmlIcon); | ||
next(); | ||
}) | ||
.catch(err => { next(err); }); | ||
}, | ||
function(err) { | ||
if (err) done(err); | ||
else done(); | ||
}); | ||
}, | ||
function($, metadata, dirty, done) { | ||
if (! metadata.blogtag) {return done(); } | ||
if (!metadata.config.blogPodcast) { return done(); } | ||
var blogcfg = metadata.config.blogPodcast[metadata.blogtag]; | ||
if (!blogcfg) return done(new Error('No blog configuration found for blogtag '+ metadata.blogtag +' in '+ metadata.document.path)); | ||
if (! metadata.config.blogPodcast.hasOwnProperty(metadata.blogtag)) { | ||
return done(new Error("no blogPodcast item for "+ metadata.blogtag)); | ||
} | ||
// log('blog-next-prev '+ metadata.document.path +' '+ util.inspect(blogcfg)); | ||
var elements = []; | ||
$('blog-next-prev').each(function(i, elem) { elements.push(elem); }); | ||
if (elements.length > 0) { | ||
// log('blog-next-prev'); | ||
findBlogDocs(metadata.config, metadata, blogcfg) | ||
.then(documents => { | ||
async.eachSeries(elements, | ||
(element, next) => { | ||
var docIndex = -1; | ||
for (var j = 0; docIndex === -1 && j < documents.length; j++) { | ||
// log(`blog-next-prev ${documents[j].docpath} === ${metadata.document.path}`); | ||
if (documents[j].docpath === metadata.document.path) { | ||
docIndex = j; | ||
} | ||
} | ||
if (docIndex >= 0) { | ||
var prevDoc = docIndex === 0 ? documents[documents.length - 1] : documents[docIndex - 1]; | ||
var nextDoc = docIndex === documents.length - 1 ? documents[0] : documents[docIndex + 1]; | ||
akasha.partial(metadata.config, 'blog-next-prev.html.ejs', { | ||
prevDoc, nextDoc | ||
}) | ||
.then(html => { | ||
$(element).replaceWith(html); | ||
next(); | ||
}) | ||
.catch(err => { next(err); }); | ||
} else { | ||
next(new Error('did not find document '+ metadata.document.path +' in blog')); | ||
} | ||
}, | ||
err => { | ||
if (err) done(err); | ||
else done(); | ||
}); | ||
}) | ||
.catch(err => { done(err); }); | ||
} else done(); | ||
} | ||
]; | ||
]; |
@@ -14,3 +14,3 @@ { | ||
}, | ||
"version": "0.4.4", | ||
"version": "0.4.5", | ||
"engines": { | ||
@@ -20,4 +20,6 @@ "node": ">=0.10.1" | ||
"dependencies": { | ||
"async": "*" | ||
"async": "*", | ||
"debug": "^2.2.0", | ||
"co": "*" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15143
8
320
3
3
1
+ Addedco@*
+ Addeddebug@^2.2.0
+ Addedco@4.6.0(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addedms@2.0.0(transitive)