@loom-io/front-matter-converter
Version:
Convert front matter yaml and json into json from loom-io files
41 lines (40 loc) • 1.33 kB
JavaScript
import { getFrontMatterConverter, hasFrontMatter, } from "./utils.js";
export async function readFrontMatter(line) {
const frontMatter = [];
let lineContent = await line.read("utf8");
while (lineContent !== "---") {
frontMatter.push(lineContent);
await line.next();
lineContent = await line.read("utf-8");
}
return frontMatter.join("\n");
}
export async function readContent(line) {
const content = [];
do {
const lineContent = await line.read("utf8");
if (!(content.length === 0 && (lineContent === "---" || lineContent === ""))) {
content.push(lineContent);
}
await line.next();
} while (await line.hasNext());
return content.join("\n");
}
export async function parse(file) {
const reader = await file.reader();
const lineReader = await reader.firstLine();
const result = {
data: {},
content: "",
};
if (await hasFrontMatter(lineReader)) {
const { parse } = await getFrontMatterConverter(lineReader);
await lineReader.next();
const frontMatter = await readFrontMatter(lineReader);
result.data = await parse(frontMatter);
await lineReader.next();
}
result.content = await readContent(lineReader);
reader.close();
return result;
}