Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

akashacms-base

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

akashacms-base - npm Package Compare versions

Comparing version 0.4.6 to 0.6.0

guide/index.html.md

716

index.js
/**
*
* Copyright 2014-2015 David Herron
* Copyright 2014-2017 David Herron
*

@@ -20,32 +20,113 @@ * This file is part of AkashaCMS (http://akashacms.com/).

var path = require('path');
var util = require('util');
var url = require('url');
var async = require('async');
'use strict';
var akasha;
var config;
var logger;
const fs = require('fs');
const path = require('path');
const util = require('util');
const url = require('url');
const async = require('async');
const co = require('co');
const akasha = require('akasharender');
const mahabhuta = require('mahabhuta');
const smap = require('sightmap');
module.exports.config = function(_akasha, _config) {
akasha = _akasha;
config = _config;
logger = akasha.getLogger("base");
if (!config.builtin) config.builtin = {};
if (!config.builtin.suppress) config.builtin.suppress = {};
if (!(config.builtin && config.builtin.suppress && config.builtin.suppress.partials)) {
config.root_partials.push(path.join(__dirname, 'partials'));
}
if (!(config.builtin && config.builtin.suppress && config.builtin.suppress.layouts)) {
config.root_layouts.push(path.join(__dirname, 'layout'));
}
if (!(config.builtin && config.builtin.suppress && config.builtin.suppress.assets)) {
config.root_assets.push(path.join(__dirname, 'assets'));
}
return module.exports;
};
const log = require('debug')('akasha:base-plugin');
const error = require('debug')('akasha:error-base-plugin');
const pluginName = "akashacms-base";
module.exports = class BasePlugin extends akasha.Plugin {
constructor() {
super(pluginName);
}
configure(config) {
config.addPartialsDir(path.join(__dirname, 'partials'));
config.addLayoutsDir(path.join(__dirname, 'layout'));
config.addAssetsDir(path.join(__dirname, 'assets'));
config.addMahabhuta(module.exports.mahabhuta);
config.pluginData(pluginName).linkRelTags = [];
}
doHeaderMetaSync(config, metadata) {
return akasha.partialSync(config, "ak_headermeta.html.ejs", fixHeaderMeta(metadata));
}
addLinkRelTag(config, lrTag) {
config.pluginData(pluginName).linkRelTags.push(lrTag);
return this;
}
doGoogleSitemap(metadata) {
// TBD This is extracted from the Mahabhuta tag, need to extract these parameters
// from somewhere.
// http://microformats.org/wiki/rel-sitemap
var href = undefined; // $element.attr("href");
if (!href) href = "/sitemap.xml";
// var title = $element.attr("title");
// if (!title) title = "Sitemap";
return `<link rel="sitemap" type="application/xml" title="${metadata.title}" href="${href}" />`;
// return `<xml-sitemap title="${metadata.title}" href="/sitemap.xml" />`; // akasha.partialSync(this._config, 'ak_sitemap.html.ejs', metadata);
}
generateSitemap(config, doit) {
config.pluginData(pluginName).generateSitemapFlag = doit;
return this;
}
onSiteRendered(config) {
if (!config.pluginData(pluginName).generateSitemapFlag) {
return Promise.resolve("skipped");
}
return co(function* () {
var rendered_files = [];
var documents = yield akasha.documentSearch(config, {
renderers: [ akasha.HTMLRenderer ]
});
for (let doc of documents) {
var fDate = new Date(doc.stat.mtime);
var mm = fDate.getMonth() + 1;
if (mm < 10) {
mm = "0" + mm.toString();
} else {
mm = mm.toString();
}
var dd = fDate.getDate();
if (dd < 10) {
dd = "0" + dd.toString();
} else {
dd = dd.toString();
}
var baseURL = url.parse(config.root_url);
baseURL.pathname = doc.renderpath;
rendered_files.push({
loc: baseURL.format(),
priority: 0.5,
lastmod: fDate.getUTCFullYear() +"-"+ mm +"-"+ dd
})
}
smap(rendered_files);
yield new Promise((resolve, reject) => {
smap(function(xml) {
fs.writeFile(path.join(config.renderDestination, "sitemap.xml"), xml, 'utf8', function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
});
return "okay";
})
}
}
module.exports.mahabhuta = new mahabhuta.MahafuncArray("akashacms-base", {});
var fixHeaderMeta = function(metadata) {

@@ -88,256 +169,105 @@ var data = {};

var akDoHeaderMeta = function(metadata, done) {
akasha.partial("ak_headermeta.html.ejs", fixHeaderMeta(metadata), done);
var akDoHeaderMeta = function(metadata) {
return akasha.partial(metadata.config, "ak_headermeta.html.ejs", fixHeaderMeta(metadata));
};
module.exports.doHeaderMetaSync = function(metadata) {
return akasha.partialSync("ak_headermeta.html.ejs", fixHeaderMeta(metadata));
};
/* TOO SILLY */
class PageTitleElement extends mahabhuta.CustomElement {
get elementName() { return "ak-page-title"; }
process($element, metadata, dirty) {
return Promise.reject(new Error("ak-page-title deprecated"));
}
}
module.exports.mahabhuta.addMahafunc(new PageTitleElement()); /* */
module.exports.mahabhuta = [
function($, metadata, dirty, done) {
logger.trace('ak-page-title');
var titles = [];
$('ak-page-title').each(function(i, elem) { titles.push(elem); });
async.eachSeries(titles,
function(titleTag, next) {
var title;
if (typeof metadata.pagetitle !== "undefined") {
title = metadata.pagetitle;
} else if (typeof metadata.title !== "undefined") {
title = metadata.title;
} else title = "";
akasha.partial("ak_titletag.html.ejs", {
title: title
}, function(err, rendered) {
if (err) { logger.error(err); next(err); }
else { logger.trace('ak-title replace'); $(titleTag).replaceWith(rendered); next(); }
});
},
function(err) {
if (err) {
logger.error('ak-page-title Errored with '+ util.inspect(err));
done(err);
} else done();
});
},
function($, metadata, dirty, done) {
logger.trace('ak-header-metatags');
var metas = [];
$('ak-header-metatags').each(function(i, elem) { metas.push(elem); });
async.eachSeries(metas,
function(meta, next) {
akDoHeaderMeta(metadata, function(err, rendered) {
if (err) {
logger.error('ak-header-metatags ERROR '+ util.inspect(err));
next(err);
} else {
$(meta).replaceWith(rendered);
next();
}
});
},
function(err) {
if (err) {
logger.error('ak-header-metatags Errored with '+ util.inspect(err));
done(err);
} else done();
});
},
function($, metadata, dirty, done) {
logger.trace('ak-header-linkreltags');
var elements = [];
$('ak-header-linkreltags').each(function(i, elem) { elements.push(elem); });
async.eachSeries(elements,
function(element, next) {
if (config.akBase && config.akBase.linkRelTags) {
config.akBase.linkRelTags.forEach(function(lrtag) {
akasha.partial("ak_linkreltag.html.ejs", {
relationship: lrtag.relationship,
url: lrtag.url
}, function(err, rendered) {
if (err) { logger.error(err); next(err); }
else { $(element).replaceWith(rendered); next(); }
});
});
} else {
$(element).remove();
next();
class HeaderMetatagsElement extends mahabhuta.CustomElement {
get elementName() { return "ak-header-metatags"; }
process($element, metadata, dirty, done) {
return akDoHeaderMeta(metadata);
}
}
module.exports.mahabhuta.addMahafunc(new HeaderMetatagsElement());
/* Moved to Mahabhuta */
class XMLSitemap extends mahabhuta.CustomElement {
get elementName() { return "ak-sitemapxml"; }
process($element, metadata, dirty, done) {
return Promise.reject(new Error("ak-sitemapxml deprecated"));
}
}
module.exports.mahabhuta.addMahafunc(new XMLSitemap()); /* */
function doLinkRelTag(config, lrtag) {
return akasha.partial(metadata.config, "ak_linkreltag.html.ejs", {
relationship: lrtag.relationship,
url: lrtag.url
});
}
class LinkRelTagsElement extends mahabhuta.CustomElement {
get elementName() { return "ak-header-linkreltags"; }
process($element, metadata, dirty) {
return co(function* () {
var ret = "";
if (metadata.config.pluginData(pluginName).linkRelTags.length > 0) {
for (var lrtag of metadata.config.pluginData(pluginName).linkRelTags) {
ret += yield doLinkRelTag(metadata.config, lrtag);
}
},
function(err) {
if (err) {
logger.error('ak-header-linkreltags Errored with '+ util.inspect(err));
done(err);
} else done();
});
},
function($, metadata, dirty, done) {
logger.trace('ak-header-canonical-url');
}
if (metadata.config.pluginData(pluginName).linkRelTags.length > 0) {
for (var lrtag of metadata.config.pluginData(pluginName).linkRelTags) {
ret += yield doLinkRelTag(metadata.config, lrtag);
}
}
return ret;
});
}
}
module.exports.mahabhuta.addMahafunc(new LinkRelTagsElement());
class CanonicalURLElement extends mahabhuta.CustomElement {
get elementName() { return "ak-header-canonical-url"; }
process($element, metadata, dirty) {
return doLinkRelTag(metadata.config, {
relationship: "canonical",
url: metadata.rendered_url
});
}
}
module.exports.mahabhuta.addMahafunc(new CanonicalURLElement());
module.exports.mahabhuta.addMahafunc(
function($, metadata, dirty, done) {
var elements = [];
$('ak-header-canonical-url').each(function(i, elem) { elements.push(elem); });
async.eachSeries(elements,
function(element, next) {
if (typeof metadata.rendered_url !== "undefined") {
akasha.partial("ak_linkreltag.html.ejs", {
relationship: "canonical",
url: metadata.rendered_url
}, function(err, rendered) {
if (err) { logger.error(err); next(err); }
else { $(element).replaceWith(rendered); next(); }
});
}
else {
$(element).remove();
next();
}
},
function(err) {
if (err) {
logger.error('ak-header-canonical-url Errored with '+ util.inspect(err));
done(err);
} else done();
$('ak-siteverification').each((i, elem) => { elements.push(elem); });
if (elements.length <= 0) return done();
return done(new Error("ak-siteverification deprecated, use site-verification instead"));
});
class GoogleAnalyticsElement extends mahabhuta.CustomElement {
get elementName() { return "ak-google-analytics"; }
process($element, metadata, dirty) {
return Promise.reject("ak-google-analytics deprecated")
}
}
module.exports.mahabhuta.addMahafunc(new GoogleAnalyticsElement());
class PublicationDateElement extends mahabhuta.CustomElement {
get elementName() { return "publication-date"; }
process($element, metadata, dirty, done) {
if (metadata.publicationDate) {
return akasha.partial(metadata.config, "ak_publdate.html.ejs", {
publicationDate: metadata.publicationDate
});
},
} else return Promise.resolve("");
}
}
module.exports.mahabhuta.addMahafunc(new PublicationDateElement());
module.exports.mahabhuta.addMahafunc(
function($, metadata, dirty, done) {
logger.trace('ak-siteverification');
var elements = [];
$('ak-siteverification').each(function(i, elem) { elements.push(elem); });
async.eachSeries(elements,
function(element, next) {
if (typeof config.google.siteVerification !== "undefined") {
akasha.partial("ak_siteverification.html.ejs",
{ googleSiteVerification: config.google.siteVerification },
function(err, html) {
if (err) next(err);
else {
$(element).replaceWith(html);
next();
}
});
} else {
$(element).remove();
next();
}
},
function(err) {
if (err) {
logger.error('ak-siteverification Errored with '+ util.inspect(err));
done(err);
} else done();
});
},
function($, metadata, dirty, done) {
logger.trace('ak-google-analytics');
var elements = [];
$('ak-google-analytics').each(function(i, elem) { elements.push(elem); });
async.eachSeries(elements,
function(element, next) {
if (typeof config.google.analyticsAccount !== "undefined" && typeof config.google.analyticsDomain !== "undefined") {
akasha.partial("ak_googleAnalytics.html.ejs", {
googleAnalyticsAccount: config.google.analyticsAccount,
googleAnalyticsDomain: config.google.analyticsDomain
}, function(err, html) {
if (err) next(err);
else {
$(element).replaceWith(html);
next();
}
});
}
else {
$(element).remove();
next();
}
},
function(err) {
if (err) {
logger.error('ak-google-analytics Errored with '+ util.inspect(err));
done(err);
} else done();
});
},
function($, metadata, dirty, done) {
logger.trace('ak-sitemapxml');
var elements = [];
$('ak-sitemapxml').each(function(i, elem) { elements.push(elem); });
async.eachSeries(elements,
function(element, next) {
akasha.partial("ak_sitemap.html.ejs", { }, function(err, html) {
if (err) {
next(err);
} else {
$(element).replaceWith(html);
next();
}
});
},
function(err) {
if (err) {
logger.error('ak-sitemapxml Errored with '+ util.inspect(err));
done(err);
} else done();
});
},
function($, metadata, dirty, done) {
logger.trace('rss-header-meta');
if ($('html head').get(0)) {
var rssheadermeta = [];
$('rss-header-meta').each(function(i, elem){ rssheadermeta.push(elem); });
async.eachSeries(rssheadermeta,
function(rssmeta, next) {
var href = $(rssmeta).attr('href');
if (href) {
$('head').append(
'<link rel="alternate" type="application/rss+xml" href="'+href+'" />'
);
} else logger.error('no href= tag in rss-header-meta ... skipped');
$(rssmeta).remove();
next();
},
function(err) {
if (err) done(err);
else done();
});
} else done();
},
function($, metadata, dirty, done) {
logger.trace('publication-date');
var elements = [];
$('publication-date').each(function(i, elem) { elements.push(elem); });
async.eachSeries(elements,
function(element, next) {
logger.trace(metadata.publicationDate);
if (metadata.publicationDate) {
akasha.partial("ak_publdate.html.ejs", {
publicationDate: metadata.publicationDate
},
function(err, html) {
if (err) { logger.error(err); next(err); }
else { $(element).replaceWith(html); next(); }
});
} else next();
}, function(err) {
if (err) { logger.error(err); done(err); }
else { logger.trace('END publication-date'); done(); }
});
},
function($, metadata, dirty, done) {
logger.trace('author-link');
if (config.authorship) {
if (metadata.config.authorship) {
var auname;
if (!metadata.authorname && config.authorship.defaultAuthorName) {
auname = config.authorship.defaultAuthorName;
if (!metadata.authorname && metadata.config.authorship.defaultAuthorName) {
auname = metadata.config.authorship.defaultAuthorName;
} else if (metadata.authorname) {

@@ -349,8 +279,10 @@ auname = metadata.authorname;

$('author-link').each(function(i, elem) { elements.push(elem); });
if (elements.length <= 0) return done();
log('author-link');
async.eachSeries(elements,
function(element, next) {
var author;
for (var i in config.authorship.authors) {
if (config.authorship.authors[i].name === auname) {
author = config.authorship.authors[i];
for (var i in metadata.config.authorship.authors) {
if (metadata.config.authorship.authors[i].name === auname) {
author = metadata.config.authorship.authors[i];
break;

@@ -360,128 +292,95 @@ }

if (author) {
akasha.partial("ak_authorship.html.ejs", {
fullname: author.fullname,
authorship: author.authorship
},
function(err, html) {
if (err) { logger.error(err); next(err); }
else { $(element).replaceWith(html); next(); }
});
akasha.partial(metadata.config, "ak_authorship.html.ejs", {
fullname: author.fullname,
authorship: author.authorship
})
.then(html => {
$(element).replaceWith(html);
next();
})
.catch(err => { next(err); });
} else {
logger.warn('no author data found for '+ auname);
log('no author data found for '+ auname);
next();
}
}, function(err) {
if (err) { logger.error(err); done(err); }
else { logger.trace('END author-link'); done(); }
if (err) { error(err); done(err); }
else { done(); }
});
} else done();
} else done();
},
/**
* These next two tags / functions are a two-step process for extracting image
* references and listing them as meta og:image tags.
*
* In phase 1 <open-graph-promote-images> should be put in a template, to trigger
* the code below. It simply adds the metaog-promote class to any image found
* in the content, and then the <open-graph-promote-images> tag is removed.
* That class triggers phase 2.
*
* In phase 2 - triggered only when there is "html head" present in the DOM -
* we take img.metaog-promote images and insert a
* <meta name="og:image" content="...">
* tag into the <head> section for each one.
*/
function($, metadata, dirty, done) {
logger.trace('open-graph-promote-images');
var elements = [];
$('open-graph-promote-images').each(function(i,elem){ elements.push(elem); });
async.eachSeries(elements,
function(element, next) {
$(element).remove();
var imgz = [];
var selector = $(element).attr('root')
? ($(element).attr('root') +' img')
: 'img';
$(selector).each(function(i, elem) { imgz.push(elem); });
async.eachSeries(imgz,
function(img, next2) {
var imgurl = $(img).attr('src');
if (imgurl.match(/\/img\/extlink.png/)
|| imgurl.match(/\/img\/rss_button.png/)
|| imgurl.match(/\/img\/rss_button.gif/)) {
// Ignore these images
} else {
$(img).addClass('metaog-promote');
}
next2();
}, function(err) {
if (err) next(err);
else next();
});
}, function(err) {
if (err) { logger.error(err); done(err); }
else { logger.trace('END open-graph-promote-images'); done(); }
});
},
/** Handle phase 2 of promoting image href's as og:image meta tags. */
function($, metadata, dirty, done) {
logger.trace('img.metaog-promote');
if ($('html head').get(0)) {
var elements = [];
$('img.metaog-promote').each(function(i,elem) {
elements.push(elem);
});
async.eachSeries(elements,
function(element, next) {
$(element).removeClass('metaog-promote');
var href = $(element).attr('src');
if (href && href.length > 0) {
var pHref = url.parse(href);
// In case this is a site-relative URL, fix it up
// to have the full URL.
if (! pHref.host) {
if (pHref.path.match(/^\//)) {
href = config.root_url +'/'+ href;
} else {
var pRendered = url.parse(metadata.rendered_url);
var dirRender = path.dirname(pRendered.path);
var pRootUrl = url.parse(config.root_url);
pRootUrl.pathname = dirRender +'/'+ href;
href = url.format(pRootUrl);
}
}
}
akasha.partial('ak_metatag.html.ejs', {
tagname: 'og:image',
tagcontent: href
}, function(err, txt) {
if (err) { logger.error(err); next(err); }
else { $('head').append(txt); next(); }
});
}, function(err) {
if (err) { logger.error(err); done(err); }
else { logger.trace('END img.metaog-promote'); done(); }
});
} else done();
},
});
class OpenGraphPromoteImages extends mahabhuta.Munger {
get selector() { return "html head open-graph-promote-images"; }
process($, $link, metadata, dirty) {
return co(function* () {
var imgcount = 0;
var selector = $link.attr('root')
? ($link.attr('root') +' img')
: 'img';
var imgz = [];
$(selector).each(function(i, elem) { imgz.push(elem); });
// console.log(`${metadata.rendered_url} image selector ${selector} - gave ${imgz.length} images`);
for (let img of imgz) {
let href = $(img).attr('src');
// console.log(`${metadata.rendered_url} image ${href}`);
if (href.match(/\/img\/extlink.png$/)
|| href.match(/\/img\/rss_button.png$/)
|| href.match(/\/img\/rss_button.gif$/)) {
// Ignore these images
} else {
if (href && href.length > 0) {
let pHref = url.parse(href);
// In case this is a site-relative URL, fix it up
// to have the full URL.
if (! pHref.host) {
if (pHref.path.match(/^\//)) {
href = metadata.config.root_url + href;
} else {
let pRendered = url.parse(metadata.rendered_url);
let dirRender = path.dirname(pRendered.path);
let pRootUrl = url.parse(metadata.config.root_url);
pRootUrl.pathname = dirRender +'/'+ href;
href = url.format(pRootUrl);
}
}
}
if ($(`meta[content="${href}"]`).get(0) === undefined) {
let txt = yield akasha.partial(metadata.config, 'ak_metatag.html.ejs', {
tagname: 'og:image',
tagcontent: href
});
if (txt) {
// console.log(`${metadata.rendered_url} appending image meta ${txt}`);
imgcount++;
$('head').append(txt);
}
}
}
}
if (imgcount > 0) {
// console.log(`${metadata.rendered_url} removing open-graph-promote-images ${imgcount}`);
$link.remove();
}
});
}
}
module.exports.mahabhuta.addMahafunc(new OpenGraphPromoteImages());
module.exports.mahabhuta.addMahafunc(
function($, metadata, dirty, done) {
logger.trace('a modifications');
var links = [];
$('html body a').each(function(i, elem) { links.push(elem); });
$('html body a').each((i, elem) => { links.push(elem); });
if (links.length <= 0) return done();
log('a modifications');
async.eachSeries(links,
function(link, next) {
(link, next) => {
setImmediate(function() {
var href = $(link).attr('href');
/*var text = $(link).text();
var rel = $(link).attr('rel');
var lclass = $(link).attr('class');
var id = $(link).attr('id');
var name = $(link).attr('name');
var title = $(link).attr('title');*/
// The potential exists to manipulate links to local documents

@@ -492,14 +391,14 @@ // Such as what's done with the linkto tag above.

// Potential for complete link checking service right here
if (href && href !== '#') {
var uHref = url.parse(href, true, true);
if (uHref.protocol || uHref.slashes) {
// It's a link to somewhere else
// look at domain in whitelist and blacklist
var donofollow = false;
if (config.nofollow && config.nofollow.blacklist) {
config.nofollow.blacklist.forEach(function(re) {
if (metadata.config.nofollow && metadata.config.nofollow.blacklist) {
metadata.config.nofollow.blacklist.forEach(function(re) {
if (uHref.hostname.match(re)) {

@@ -510,4 +409,4 @@ donofollow = true;

}
if (config.nofollow && config.nofollow.whitelist) {
config.nofollow.whitelist.forEach(function(re) {
if (metadata.config.nofollow && metadata.config.nofollow.whitelist) {
metadata.config.nofollow.whitelist.forEach(function(re) {
if (uHref.hostname.match(re)) {

@@ -518,12 +417,13 @@ donofollow = false;

}
if (donofollow && !$(link).attr('rel')) {
$(link).attr('rel', 'nofollow');
}
if (! config.builtin.suppress.extlink
/* TODO
if (! metadata.config.builtin.suppress.extlink
&& $(link).find("img.ak-extlink-icon").length <= 0) {
$(link).append('<img class="ak-extlink-icon" src="/img/extlink.png"/>');
}
} */
next();

@@ -540,2 +440,3 @@ } else {

}
/* TODO
var docEntry = akasha.findDocumentForUrlpath(href);

@@ -555,3 +456,3 @@ if (docEntry) {

}
}
} */
next();

@@ -562,7 +463,6 @@ }

},
function(err) {
err => {
if (err) done(err);
else done();
});
}
];
});

@@ -29,9 +29,12 @@ {

},
"version": "0.4.6",
"version": "0.6.0",
"engines": {
"node": ">=0.10.1"
"node": ">=6.1"
},
"dependencies": {
"async": ">=0.1.22"
"async": ">=0.1.22",
"co": "*",
"debug": "^2.2.0",
"sightmap": "*"
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc