desirae.js/lib/transform-core.js

211 lines
6.6 KiB
JavaScript
Raw Normal View History

2015-01-23 09:52:41 +00:00
/*jshint -W054 */
2020-11-09 03:01:26 +00:00
(function (exports) {
"use strict";
2015-01-23 09:52:41 +00:00
2020-11-09 03:01:26 +00:00
var cores = {},
Desi = exports.Desirae || require("desirae").Desirae,
path = exports.path || require("path");
2015-01-23 09:52:41 +00:00
cores.lint = function (desi, env, collection, entity) {
// TODO splice
//desi.content.collections = desi.content.collections.filter(function (entity) {
2020-11-09 03:01:26 +00:00
// TODO throw for any files that don't have a registered renderer
if (!entity.yml) {
if (!desi.config.empty_frontmatter) {
throw new Error(
"no frontmatter for " +
(entity.path || entity.name) +
"." +
"Set `config.yml.empty_frontmatter: include|skip` to ignore this error."
);
}
2015-01-23 09:52:41 +00:00
2020-11-09 03:01:26 +00:00
if ("include" === desi.config.empty_frontmatter) {
entity.yml = {};
} else if ("skip" === desi.config.empty_frontmatter) {
return false;
} else {
throw new Error(
"unrecognize option " +
desi.config.empty_frontmatter +
" for `config.yml.empty_frontmatter: include|skip`."
);
2015-01-23 09:52:41 +00:00
}
2020-11-09 03:01:26 +00:00
}
2015-01-23 09:52:41 +00:00
2020-11-09 03:01:26 +00:00
if (!entity.body || !entity.body.trim()) {
if (!desi.config.empty_body) {
throw new Error(
"empty content file " +
(entity.path || entity.name) +
". Set `config.yml.empty_body: include|skip` to ignore this error."
);
}
2015-01-23 09:52:41 +00:00
2020-11-09 03:01:26 +00:00
if ("include" === desi.config.empty_body) {
entity.body = "";
} else if ("skip" === desi.config.empty_body) {
return false;
} else {
throw new Error(
"unrecognize option " +
desi.config.empty_frontmatter +
" for `config.yml.empty_body: include|skip`."
);
2015-01-23 09:52:41 +00:00
}
2020-11-09 03:01:26 +00:00
}
2015-01-23 09:52:41 +00:00
2020-11-09 03:01:26 +00:00
return true;
2015-01-23 09:52:41 +00:00
//});
};
cores.root = function (desi, env, collection, entity) {
entity.yml = entity.yml || {};
2020-11-09 03:01:26 +00:00
entity.layout = entity.yml.layout || "__page__";
2015-01-23 09:52:41 +00:00
// _root is not subject to the same permalink rules as collections,
// so we just go ahead and define that here
2015-01-23 10:38:52 +00:00
if (/^index\.\w+$/.test(entity.path)) {
2020-11-09 03:01:26 +00:00
entity.permalink = "/";
2015-01-23 10:38:52 +00:00
} else {
2020-11-09 03:01:26 +00:00
entity.permalink =
entity.yml.permalink || entity.path.replace(/\.\w+$/, "/");
2015-01-23 10:59:09 +00:00
entity.redirects = entity.redirects || [];
2020-11-09 03:01:26 +00:00
entity.redirects.push(entity.permalink.replace(/\/$/, "/index.html"));
2015-01-23 10:38:52 +00:00
}
2015-01-23 09:52:41 +00:00
};
cores.normalize = function (desi, env, collection, entity) {
2020-11-09 03:01:26 +00:00
entity.title =
entity.yml.title || Desi.firstCap(entity.name.replace(/\.\w+$/, ""));
entity.date = entity.yml.date;
2015-01-23 09:52:41 +00:00
if (!entity.date) {
// TODO tell YAML parser to keep the date a string
2020-11-09 03:01:26 +00:00
entity.date = new Date(
entity.yml.created_at ||
entity.yml.time ||
entity.yml.updated_at ||
entity.createdDate ||
entity.lastModifiedDate
2015-01-23 09:52:41 +00:00
).toISOString();
}
2020-11-09 03:01:26 +00:00
if ("object" === typeof entity.date) {
2015-01-23 09:52:41 +00:00
entity.date = entity.date.toISOString();
}
2020-11-09 03:01:26 +00:00
entity.updated_at = entity.yml.updated_at || entity.lastModifiedDate;
entity.published_at = Desi.fromLocaleDate(
entity.date || entity.lastModifiedDate
);
entity.year = entity.published_at.year;
entity.month = entity.published_at.month;
entity.day = entity.published_at.day;
entity.hour = entity.published_at.hour;
entity.twelve_hour = entity.published_at.twelve_hour;
entity.meridian = entity.published_at.meridian;
entity.minute = entity.published_at.minute;
2015-01-23 09:52:41 +00:00
// let's just agree that that's too far
//entity.second = entity.published_at.second;
2020-11-09 03:01:26 +00:00
entity.slug = Desi.slugify(entity.title);
entity.slug_path = Desi.slugifyPath(entity.relativePath);
entity.slugPath = Desi.slugifyPath(entity.relativePath);
2015-01-23 10:59:09 +00:00
// TODO type checking like below
2020-11-09 03:01:26 +00:00
entity.redirects =
(Array.isArray(entity.yml.redirects) && entity.yml.redirects) || [];
2015-01-23 09:52:41 +00:00
// categories
if (Array.isArray(entity.yml.categories)) {
2020-11-09 03:01:26 +00:00
entity.categories = entity.yml.categories;
} else if ("string" === typeof entity.yml.categories) {
entity.categories = [entity.yml.categories];
} else if ("string" === typeof entity.yml.category) {
entity.categories = [entity.yml.category];
} else {
entity.categories = [];
2015-01-23 09:52:41 +00:00
}
// tags
if (Array.isArray(entity.yml.tags)) {
2020-11-09 03:01:26 +00:00
entity.tags = entity.yml.tags;
} else if ("string" === typeof entity.yml.tags) {
entity.tags = [entity.yml.tags];
} else {
entity.tags = [];
2015-01-23 09:52:41 +00:00
}
2020-11-09 03:01:26 +00:00
entity.permalink = entity.permalink || entity.yml.permalink;
2015-01-23 09:52:41 +00:00
if (!entity.permalink) {
// try the fallback_permalink first (because we're looking at files that don't have yml)
// then try the normal permalink (because :filename -> :title and whatnot, so it'll work)
2020-11-09 03:01:26 +00:00
entity.permalink = Desi.permalinkify(
desi,
collection.fallback_permalink || collection.permalink,
entity
);
2015-01-23 09:52:41 +00:00
}
2020-11-09 03:01:26 +00:00
2015-01-23 09:52:41 +00:00
/*
if (!/\.x?html?$/.test(entity.permalink)) {
entity.htmllink = path.join(entity.permalink, 'index.html');
}
*/
// relative to the site
2020-11-09 03:01:26 +00:00
entity.relative_file = path
.join(env.base_path, entity.permalink)
.replace(/\/$/, "/index.html");
entity.relative_href = path
.join(env.base_path, entity.permalink)
.replace(/\/index\.html$/, "/");
entity.relative_link = entity.relative_href;
entity.url =
env.base_url +
path.join(env.base_path, entity.permalink).replace(/\/index\.html$/, "/");
entity.canonical_url =
env.base_url +
path.join(env.base_path, entity.permalink).replace(/\/index\.html$/, "/");
entity.production_url =
desi.site.base_url +
path
.join(desi.site.base_path, entity.permalink)
.replace(/\/index\.html$/, "/");
entity.relative_url = path
.join(env.base_path, entity.permalink)
.replace(/\/index\.html$/, "/");
if (
env.explicitIndexes ||
env.explicitIndices ||
env.explicit_indexes ||
env.explicit_indices
) {
2015-01-23 09:52:41 +00:00
// NOTE: file_url is NOT replaced
2020-11-09 03:01:26 +00:00
["url", "canonical_url", "production_url", "relative_url"].forEach(
function (url) {
entity[url] = entity[url].replace(/\/$/, "/index.html");
}
);
2015-01-23 09:52:41 +00:00
}
2015-01-23 11:07:40 +00:00
// i.e. bootstrap, hero page, darkly
2020-11-09 03:01:26 +00:00
entity.theme = entity.theme || entity.yml.theme;
entity.layout = entity.layout || entity.yml.layout;
entity.swatch = entity.swatch || entity.yml.swatch;
2015-01-23 09:52:41 +00:00
};
cores.disqus = function (desi, env, collection, entity) {
2020-11-09 03:01:26 +00:00
var yml = entity.yml;
2015-01-23 09:52:41 +00:00
if (yml.uuid) {
entity.disqus_identifier = yml.uuid;
}
entity.disqus_url = entity.production_url;
};
exports.DesiraeTransformCore = cores.DesiraeTransformCore = cores;
2020-11-09 03:01:26 +00:00
})(("undefined" !== typeof exports && exports) || window);