@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
58 lines • 1.75 kB
JavaScript
const parseToObject = (input) => {
let normalizedInput = input.trim().replace(/^\[/, "[").replace(/^\{/, "{").replace(/\]$/, "]").replace(/\}$/, "}").replace(/([\w\d_]+)\s*:/g, '"$1":').replace(/:\s*([a-zA-Z_][\w\d_]*)/g, ': "$1"');
normalizedInput = normalizedInput.replace(
/\[([^\[\]]+?)\]/g,
(_match, arrayContent) => {
const newContent = arrayContent.split(",").map((item) => {
const trimmed = item.trim();
if (trimmed.startsWith('"') && trimmed.endsWith('"') || !isNaN(Number(trimmed))) {
return trimmed;
}
return `"${trimmed}"`;
}).join(", ");
return `[${newContent}]`;
}
);
return JSON.parse(normalizedInput);
};
const getMarkdownMetadata = (markdown) => {
try {
const lines = markdown.split(/\r?\n/);
const firstNonEmptyLine = lines.find((line) => line.trim() !== "");
if (!firstNonEmptyLine || firstNonEmptyLine.trim() !== "---") {
return {};
}
const metadata = {};
let inMetadataBlock = false;
for (const line of lines) {
const trimmedLine = line.trim();
if (trimmedLine === "---") {
if (!inMetadataBlock) {
inMetadataBlock = true;
continue;
} else {
break;
}
}
if (inMetadataBlock) {
const match = line.match(/^([^:]+)\s*:\s*(.*)$/);
if (match) {
const key = match[1].trim();
const value = match[2].trim();
try {
metadata[key] = parseToObject(value);
} catch (e) {
metadata[key] = value;
}
}
}
}
return metadata;
} catch (e) {
return void 0;
}
};
export {
getMarkdownMetadata
};
//# sourceMappingURL=getMarkdownMetadata.mjs.map