decap-cms-core
Version:
Decap CMS core application, see decap-cms package for the main distribution.
162 lines (159 loc) • 7.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FrontmatterInfer = exports.FrontmatterFormatter = void 0;
exports.frontmatterJSON = frontmatterJSON;
exports.frontmatterTOML = frontmatterTOML;
exports.frontmatterYAML = frontmatterYAML;
exports.getFormatOpts = getFormatOpts;
var _grayMatter = _interopRequireDefault(require("gray-matter"));
var _toml = _interopRequireDefault(require("./toml"));
var _yaml = _interopRequireDefault(require("./yaml"));
var _json = _interopRequireDefault(require("./json"));
const _excluded = ["body"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
const Languages = {
YAML: 'yaml',
TOML: 'toml',
JSON: 'json'
};
const parsers = {
toml: {
parse: input => _toml.default.fromFile(input),
stringify: (metadata, opts) => {
const {
sortedKeys
} = opts || {};
return _toml.default.toFile(metadata, sortedKeys);
}
},
json: {
parse: input => {
let JSONinput = input.trim();
// Fix JSON if leading and trailing brackets were trimmed.
if (JSONinput.slice(0, 1) !== '{') {
JSONinput = '{' + JSONinput + '}';
}
return _json.default.fromFile(JSONinput);
},
stringify: metadata => {
let JSONoutput = _json.default.toFile(metadata).trim();
// Trim leading and trailing brackets.
if (JSONoutput.slice(0, 1) === '{' && JSONoutput.slice(-1) === '}') {
JSONoutput = JSONoutput.slice(1, -1);
}
return JSONoutput;
}
},
yaml: {
parse: input => _yaml.default.fromFile(input),
stringify: (metadata, opts) => {
const {
sortedKeys,
comments
} = opts || {};
return _yaml.default.toFile(metadata, sortedKeys, comments);
}
}
};
function inferFrontmatterFormat(str) {
const lineEnd = str.indexOf('\n');
const firstLine = str.slice(0, lineEnd !== -1 ? lineEnd : 0).trim();
if (firstLine.length > 3 && firstLine.slice(0, 3) === '---') {
// No need to infer, `gray-matter` will handle things like `---toml` for us.
return;
}
switch (firstLine) {
case '---':
return getFormatOpts(Languages.YAML);
case '+++':
return getFormatOpts(Languages.TOML);
case '{':
return getFormatOpts(Languages.JSON);
default:
console.warn('Unrecognized front-matter format.');
}
}
function getFormatOpts(format, customDelimiter) {
if (!format) {
return undefined;
}
const formats = {
yaml: {
language: Languages.YAML,
delimiters: '---'
},
toml: {
language: Languages.TOML,
delimiters: '+++'
},
json: {
language: Languages.JSON,
delimiters: ['{', '}']
}
};
const {
language,
delimiters
} = formats[format];
return {
language,
delimiters: customDelimiter || delimiters
};
}
class FrontmatterFormatter {
constructor(format, customDelimiter) {
_defineProperty(this, "format", void 0);
this.format = getFormatOpts(format, customDelimiter);
}
fromFile(content) {
const format = this.format || inferFrontmatterFormat(content);
const result = (0, _grayMatter.default)(content, _objectSpread({
engines: parsers
}, format));
// in the absent of a body when serializing an entry we use an empty one
// when calling `toFile`, so we don't want to add it when parsing.
return _objectSpread(_objectSpread({}, result.data), result.content.trim() && {
body: result.content
});
}
toFile(data, sortedKeys, comments) {
const {
body = ''
} = data,
meta = _objectWithoutProperties(data, _excluded);
// Stringify to YAML if the format was not set
const format = this.format || getFormatOpts(Languages.YAML);
// gray-matter always adds a line break at the end which trips our
// change detection logic
// https://github.com/jonschlinkert/gray-matter/issues/96
const trimLastLineBreak = body.slice(-1) !== '\n';
const file = _grayMatter.default.stringify(body, meta, _objectSpread({
engines: parsers,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore `sortedKeys` is not recognized by gray-matter, so it gets passed through to the parser
sortedKeys,
comments
}, format));
return trimLastLineBreak && file.slice(-1) === '\n' ? file.slice(0, -1) : file;
}
}
exports.FrontmatterFormatter = FrontmatterFormatter;
const FrontmatterInfer = exports.FrontmatterInfer = new FrontmatterFormatter();
function frontmatterYAML(customDelimiter) {
return new FrontmatterFormatter(Languages.YAML, customDelimiter);
}
function frontmatterTOML(customDelimiter) {
return new FrontmatterFormatter(Languages.TOML, customDelimiter);
}
function frontmatterJSON(customDelimiter) {
return new FrontmatterFormatter(Languages.JSON, customDelimiter);
}