2015-01-05 19:23:26 +00:00
|
|
|
/*jshint -W054 */
|
2020-11-09 03:01:26 +00:00
|
|
|
(function (exports) {
|
|
|
|
"use strict";
|
2015-01-05 19:23:26 +00:00
|
|
|
|
2015-01-15 05:36:59 +00:00
|
|
|
function create(Desi) {
|
2020-11-09 03:01:26 +00:00
|
|
|
Desi.YAML = {};
|
|
|
|
Desi.YAML.parse = (exports.jsyaml || require("js-yaml")).load;
|
|
|
|
Desi.YAML.stringify = (exports.jsyaml || require("js-yaml")).dump;
|
2015-01-15 05:36:59 +00:00
|
|
|
|
|
|
|
function readFrontMatter(text) {
|
2020-11-09 03:01:26 +00:00
|
|
|
var lines,
|
|
|
|
line,
|
|
|
|
padIndent = "",
|
|
|
|
ymllines = [];
|
2015-01-15 05:36:59 +00:00
|
|
|
lines = text.split(/\n/);
|
|
|
|
line = lines.shift();
|
2015-01-05 19:23:26 +00:00
|
|
|
|
2015-01-15 05:36:59 +00:00
|
|
|
if (!line.match(/^---\s*$/)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-05 19:23:26 +00:00
|
|
|
|
2015-01-15 05:36:59 +00:00
|
|
|
// our yaml parser can't handle objects
|
|
|
|
// that start without indentation, so
|
|
|
|
// we can add it if this is the case
|
|
|
|
if (lines[0] && lines[0].match(/^\S/)) {
|
2020-11-09 03:01:26 +00:00
|
|
|
padIndent = "";
|
2015-01-15 05:36:59 +00:00
|
|
|
}
|
2015-01-05 19:23:26 +00:00
|
|
|
|
2015-01-15 05:36:59 +00:00
|
|
|
while (true) {
|
|
|
|
line = lines.shift();
|
2015-01-05 19:23:26 +00:00
|
|
|
|
2015-01-15 05:36:59 +00:00
|
|
|
// premature end-of-file (unsupported yaml)
|
2020-11-09 03:01:26 +00:00
|
|
|
if (!line && "" !== line) {
|
2015-01-15 05:36:59 +00:00
|
|
|
ymllines = [];
|
|
|
|
break;
|
|
|
|
}
|
2015-01-05 19:23:26 +00:00
|
|
|
|
2015-01-15 05:36:59 +00:00
|
|
|
// end-of-yaml front-matter
|
|
|
|
if (line.match(/^---\s*$/)) {
|
|
|
|
break;
|
|
|
|
}
|
2015-01-05 19:23:26 +00:00
|
|
|
|
2015-01-15 05:36:59 +00:00
|
|
|
if (line) {
|
|
|
|
// supported yaml
|
2020-11-09 03:01:26 +00:00
|
|
|
ymllines.push(padIndent + line);
|
2015-01-15 05:36:59 +00:00
|
|
|
}
|
2015-01-05 19:23:26 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 05:36:59 +00:00
|
|
|
// XXX can't be sorted because arrays get messed up
|
|
|
|
//ymllines.sort();
|
|
|
|
if (ymllines) {
|
2020-11-09 03:01:26 +00:00
|
|
|
return "---\n" + ymllines.join("\n");
|
2015-01-05 19:23:26 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 05:36:59 +00:00
|
|
|
return;
|
2015-01-05 19:23:26 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 05:36:59 +00:00
|
|
|
function separateText(text, fm) {
|
2020-11-09 03:01:26 +00:00
|
|
|
var len, yml;
|
2015-01-15 05:36:59 +00:00
|
|
|
|
|
|
|
yml = readFrontMatter(fm);
|
|
|
|
// strip frontmatter from text, if any
|
|
|
|
// including trailing '---' (which is accounted for by the added '\n')
|
|
|
|
if (yml) {
|
|
|
|
len = fm.split(/\n/).length + 1;
|
|
|
|
} else {
|
|
|
|
len = 0;
|
|
|
|
}
|
2015-01-05 19:23:26 +00:00
|
|
|
|
2020-11-09 03:01:26 +00:00
|
|
|
return text.split(/\n/).slice(len).join("\n");
|
2015-01-05 19:23:26 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 05:36:59 +00:00
|
|
|
function parseText(text) {
|
2020-11-09 03:01:26 +00:00
|
|
|
var fm = readFrontMatter(text),
|
|
|
|
body = fm && separateText(text, fm),
|
|
|
|
yml;
|
2015-01-15 05:36:59 +00:00
|
|
|
|
|
|
|
if (fm) {
|
|
|
|
try {
|
|
|
|
yml = Desi.YAML.parse(fm);
|
2020-11-09 03:01:26 +00:00
|
|
|
} catch (e) {
|
2015-01-15 05:36:59 +00:00
|
|
|
//
|
|
|
|
}
|
2015-01-07 02:20:50 +00:00
|
|
|
}
|
2015-01-15 05:36:59 +00:00
|
|
|
|
|
|
|
return {
|
2020-11-09 03:01:26 +00:00
|
|
|
yml: yml,
|
|
|
|
frontmatter: fm,
|
|
|
|
body: body,
|
2015-01-15 05:36:59 +00:00
|
|
|
};
|
2015-01-05 19:23:26 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 05:36:59 +00:00
|
|
|
exports.Frontmatter = {};
|
|
|
|
exports.Frontmatter.Frontmatter = exports.Frontmatter;
|
|
|
|
exports.Frontmatter.readText = readFrontMatter;
|
|
|
|
exports.Frontmatter.separateText = separateText;
|
|
|
|
exports.Frontmatter.parse = parseText;
|
|
|
|
exports.Frontmatter.YAML = Desi.YAML;
|
|
|
|
Desi.Frontmatter = exports.Frontmatter;
|
|
|
|
|
|
|
|
return exports;
|
2015-01-05 19:23:26 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 05:36:59 +00:00
|
|
|
if (exports.Desirae) {
|
|
|
|
create(exports.Desirae);
|
|
|
|
} else {
|
|
|
|
exports.create = create;
|
|
|
|
}
|
2020-11-09 03:01:26 +00:00
|
|
|
})(("undefined" !== typeof exports && exports) || window);
|