bloggify-markdown-adapter
Version:
Markdown adapter for Bloggify.
338 lines (311 loc) • 13.3 kB
JavaScript
;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var BloggifyAdapter = require("bloggify-adapter"),
mdify = require("mdify"),
ul = require("ul"),
sameTime = require("same-time"),
bindy = require("bindy"),
fs = require("fs"),
slugify = require("slugly"),
mkdirp = require("mkdirp"),
path = require("path"),
mapO = require("map-o");
module.exports = function (_BloggifyAdapter) {
_inherits(BloggifyMarkdownAdapter, _BloggifyAdapter);
_createClass(BloggifyMarkdownAdapter, null, [{
key: "init",
value: function init(config) {
Bloggify.adapter = new BloggifyMarkdownAdapter(config);
return Bloggify.adapter._theme;
}
}]);
function BloggifyMarkdownAdapter(options) {
_classCallCheck(this, BloggifyMarkdownAdapter);
var _this = _possibleConstructorReturn(this, (BloggifyMarkdownAdapter.__proto__ || Object.getPrototypeOf(BloggifyMarkdownAdapter)).call(this, options));
mapO(_this.options.paths, function (val) {
return path.resolve(Bloggify.options.root, val);
});
_this.paths = _this.options.paths;
mkdirp.sync(_this.paths.articles);
mkdirp.sync(_this.paths.pages);
fs.watch(_this.paths.articles, _this._refreshCache.bind(_this));
Bloggify.on("ready", function () {
_this._refreshCache();
});
_this.parseOpts = _this.parse = _this.options.parse;
return _this;
}
_createClass(BloggifyMarkdownAdapter, [{
key: "_refreshCache",
value: function _refreshCache() {
var _this2 = this;
this.bloggify.log("Refreshing the content cache.");
this.cache = {
articles: {},
pages: {},
articleIds: null,
pageSlugs: null,
articles_list: null,
pages_list: null
};
this.getArticleIds(function (err, ids) {
if (err) {
return _this2.bloggify.log(err);
}
_this2.cache.articleIds = ids;
_this2.getArticles({
ids: ids
}, function (err, articles) {
if (err) {
return _this2.bloggify.log(err);
}
_this2.cache.articles_list = articles;
_this2.cache.articles = {};
articles.forEach(function (c) {
return _this2.cache.articles[c.id] = c;
});
});
});
this.getAllPageSlugs(function (err, slugs) {
if (err) {
return _this2.bloggify.log(err);
}
_this2.cache.pageSlugs = slugs;
_this2.getPages(function (err, pages) {
if (err) {
return _this2.bloggify.log(err);
}
_this2.pages_list = pages;
_this2.pages = {};
pages.forEach(function (c) {
return _this2.pages[c.slug] = c;
});
});
});
}
}, {
key: "getArticlePath",
value: function getArticlePath(id) {
return this.paths.articles + "/" + id + ".md";
}
}, {
key: "getPagePath",
value: function getPagePath(slug) {
return this.paths.pages + "/" + slug + ".md";
}
}, {
key: "getArticleById",
value: function getArticleById(id, cb) {
var _this3 = this;
if (this.cache.articles[id]) {
return cb(null, this.cache.articles[id]);
}
mdify.parseFile(this.getArticlePath(id), this.parseOpts, function (err, data) {
if (err) {
return cb(err);
}
data.id = id;
data.slug = data.metadata.slug || slugify(data.metadata.title, { lower: true });
data.path = _this3.options.routes.articles + "/" + data.id + (data.slug ? "-" + data.slug : "");
data.content = data.markdown;
data.summary_html = data.html.split("\n").slice(0, _this3.options.display.html_summary_paragraphs).join("\n");
data.image = data.metadata.image || (data.markdown.match(/!\[.*\]\((.*)\)/m) || [])[1] || "";
data.description = data.metadata.description || data.html.split("\n")[0].replace(/(<([^>]+)>)/ig, "");
cb(null, data);
});
}
}, {
key: "getArticleIds",
value: function getArticleIds(cb) {
if (this.cache.articleIds) {
return cb(null, this.cache.articleIds);
}
fs.readdir(this.paths.articles, function (err, articles) {
if (err) {
return cb(err);
}
var allIds = articles.map(function (c) {
return parseInt(c);
}).filter(Boolean).sort(function (a, b) {
return a < b ? -1 : 1;
});
cb(null, allIds);
});
}
}, {
key: "getArticles",
value: function getArticles(options, cb) {
var _this4 = this;
if (typeof options === "function") {
cb = options;
options = {};
}
options = ul.merge(options, {
skip: 0,
per_page: 3
});
if (options.page) {
var pageNumber = options.page - 1;
options.skip = pageNumber * options.per_page;
}
this.getArticleIds(function (err, allIds) {
var count = allIds.length,
pageInfo = {
hasNewer: false,
hasOlder: false,
count: count
};
var rangeIds = [];
if (options.ids) {
rangeIds = options.ids;
} else {
rangeIds = allIds.slice(-options.page * options.per_page, -options.skip || undefined);
pageInfo.hasNewer = allIds.indexOf(rangeIds[rangeIds.length - 1]) < allIds.length - 1;
pageInfo.hasOlder = allIds.indexOf(rangeIds[0]) > 0;
rangeIds.reverse();
}
if (!rangeIds.length) {
return cb(null, [], pageInfo);
}
sameTime(bindy(rangeIds, function (cId, cb) {
_this4.getArticleById(cId, function (err, data) {
return cb(null, err ? null : data);
});
}), function (err, articles) {
if (err) {
return cb(err);
}
articles = articles.filter(Boolean);
if (options.filter) {
articles = articles.filter(options.filter);
}
cb(null, articles, pageInfo);
});
});
}
}, {
key: "getPageBySlug",
value: function getPageBySlug(slug, cb) {
var _this5 = this;
if (this.cache.pages[slug]) {
return cb(null, this.cache.pages[slug]);
}
mdify.parseFile(this.getPagePath(slug), this.parseOpts, function (err, data) {
if (err) {
return cb(err);
}
data.slug = slug;
data.metadata = data.metadata || 1;
data.path = data.metadata.path || "/" + data.slug;
if (data.slug === _this5.options.routes.home) {
data.path = "/";
}
if (data.metadata.html === true) {
data.html = data.markdown;
}
data.summary_html = data.html.split("\n").slice(0, _this5.options.display.html_summary_paragraphs).join("\n");
data.summary_html = data.html.split("\n").slice(0, _this5.options.display.html_summary_paragraphs).join("\n");
data.image = data.metadata.image || (data.markdown.match(/!\[.*\]\((.*)\)/m) || [])[1] || "";
data.summary = data.metadata.summary || (data.html.split("\n")[0] || "").replace(/(<([^>]+)>)/ig, "");
if (data.slug === _this5.options.homePath) {
data.path = "/";
}
cb(null, data);
});
}
}, {
key: "getPageSlugs",
value: function getPageSlugs(query, cb) {
if (this.cache.pageSlugs) {
return cb(null, this.cache.pageSlugs);
}
fs.readdir("" + this.paths.pages, function (err, data) {
if (err) {
return cb(err);
}
cb(null, data.filter(function (c) {
return c.endsWith(".md");
}).map(function (c) {
return c.replace(/\.md$/g, "");
}));
});
}
}, {
key: "getAllPageSlugs",
value: function getAllPageSlugs(cb) {
this.getPageSlugs({}, cb);
}
}, {
key: "getPages",
value: function getPages(query, cb) {
var _this6 = this;
if (!cb) {
return this.getAllPages(query);
}
this.getPageSlugs(query, function (err, slugs) {
if (err) {
return cb(err);
}
sameTime(bindy(slugs, function (slug, cb) {
_this6.getPageBySlug(slug, function (err, data) {
cb(null, err ? null : data);
});
}), function (err, pages) {
if (err) {
return cb(err);
}
pages = pages.filter(Boolean);
pages.sort(function (a, b) {
return a.metadata.order < b.metadata.order ? -1 : 1;
});
cb(null, pages);
});
});
}
}, {
key: "getAllPages",
value: function getAllPages(cb) {
return this.getPages({}, cb);
}
}, {
key: "contentToHtml",
value: function contentToHtml(content) {
return mdify.parse(content, this.parseOpts).html;
}
}, {
key: "getNextArticleId",
value: function getNextArticleId(cb) {
this.getArticleIds(function (err, allIds) {
if (err) {
return cb(err);
}
cb(null, Math.max.apply(null, allIds) + 1);
});
}
}, {
key: "createArticle",
value: function createArticle(title, content, custom, cb) {
var _this7 = this;
this.getNextArticleId(function (err, nextId) {
_this7.saveArticle(nextId, title, content, custom, cb);
});
}
}, {
key: "saveArticle",
value: function saveArticle(id, title, content, custom, cb) {
var metadata = custom || {};
metadata.title = title;
metadata.date = metadata.date || new Date();
mdify.writeFile(this.getArticlePath(id), metadata, content, cb);
}
}, {
key: "deleteArticle",
value: function deleteArticle(id, cb) {
fs.unlink(this.getArticlePath(id), cb);
}
}]);
return BloggifyMarkdownAdapter;
}(BloggifyAdapter);