@loom-io/front-matter-converter
Version:
Convert front matter yaml and json into json from loom-io files
52 lines (51 loc) • 1.88 kB
JavaScript
import * as YAML from "yaml";
import { getFrontMatterConverter, hasFrontMatter, } from "./utils.js";
export async function writeToFile(file, matter = "", content = "") {
const emptyLineBeforeContent = content.length === 0 ? "" : "\n";
await file.write(`---\n${ensureNewLine(matter)}---\n${emptyLineBeforeContent}${content}`);
}
export async function stringifyJson(file, data) {
if (data == null) {
return "";
}
const reader = await file.reader();
const lineReader = await reader.firstLine();
if (lineReader && (await hasFrontMatter(lineReader))) {
const converter = await getFrontMatterConverter(lineReader);
return converter.stringify(data);
}
else {
return YAML.stringify(data);
}
}
export function ensureNewLine(content) {
if (content && content.length > 0 && content[content.length - 1] !== "\n") {
return `${content}\n`;
}
return content;
}
export async function unify(file, content = null) {
if (content == null) {
await writeToFile(file);
return;
}
else if (typeof content === "string") {
await writeToFile(file, undefined, content);
return;
}
else if (typeof content === "object") {
if ((Object.keys(content).length === 1 &&
("data" in content || "content" in content)) ||
(Object.keys(content).length === 2 &&
"data" in content &&
"content" in content)) {
const dataString = "data" in content ? await stringifyJson(file, content.data) : "";
const contentString = "content" in content ? String(content.content) : "";
await writeToFile(file, dataString, contentString);
}
else {
const dataString = await stringifyJson(file, content);
await writeToFile(file, dataString);
}
}
}