matter-json
Version:
JSON front-matter parser and combiner. Minimal and perfect
129 lines (127 loc) • 3.91 kB
JavaScript
/* *******************************************************
* matter-json
*
* @license
*
* Apache-2.0
*
* Copyright 2014-2025 Alex Stevovich
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @meta
*
* package_name: matter-json
* file_name: gen/index.cjs
* purpose: Core functionality and exports combined.
*
* @system
*
* generated_on: 2025-03-15T03:23:33.684Z
* certified_version: 1.0.0
* file_uuid: 08ec44c1-3a6e-46bf-ae2c-248a4f6fa552
* file_size: 3860 bytes
* file_hash: 82b478cb1d877a6b965bf06544c98b226ccf892db5f25b250307170180059a7e
* mast_hash: b057e206d254643fbc73357e4bcb657ae1e3015f7a4ccdca6ee5f4e9120a9398
* generated_by: preamble on npm!
*
* [Preamble Metadata]
********************************************************/
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var index_exports = {};
__export(index_exports, {
default: () => index_default,
parse: () => parse,
serialize: () => serialize,
validate: () => validate
});
module.exports = __toCommonJS(index_exports);
const FRONT_MATTER_REGEX = /^---\s*\n([\s\S]+?)\n---\s*(?:\n([\s\S]*))?$/;
function parse(text) {
if (typeof text !== "string") {
throw new TypeError(
`parse expected a string, but received ${typeof text}.`
);
}
const match = text.match(FRONT_MATTER_REGEX);
if (!match) {
throw new Error(
`Invalid front matter format. Ensure it is wrapped with "---" and is valid JSON.`
);
}
try {
const data = JSON.parse(match[1]);
return { data, content: match[2] || "" };
} catch (error) {
throw new Error(`JSON Parsing Error: ${error.message}`);
}
}
function serialize(data, content, indentation = 2) {
if (typeof data !== "object" || data === null) {
throw new TypeError(
`serialize expected an object, but received ${typeof data}.`
);
}
if (typeof content !== "string") {
throw new TypeError(
`serialize expected a string, but received ${typeof content}.`
);
}
try {
const frontMatter = JSON.stringify(data, null, indentation).trim();
return `---
${frontMatter}
---
${content}`;
} catch (error) {
throw new Error(`JSON Serialization Error: ${error.message}`);
}
}
function validate(text) {
if (typeof text !== "string") {
return false;
}
const match = text.match(FRONT_MATTER_REGEX);
if (!match) {
return false;
}
try {
JSON.parse(match[1]);
return true;
} catch {
return false;
}
}
var index_default = { parse, serialize, validate };
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
parse,
serialize,
validate
});