2023-05-23 18:53:53 +00:00
|
|
|
import { YAML } from "$sb/plugos-syscall/mod.ts";
|
2022-11-24 11:04:00 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
addParentPointers,
|
|
|
|
findNodeOfType,
|
|
|
|
ParseTree,
|
|
|
|
renderToText,
|
2023-05-23 18:53:53 +00:00
|
|
|
replaceNodesMatchingAsync,
|
|
|
|
traverseTreeAsync,
|
2022-11-24 11:04:00 +00:00
|
|
|
} from "$sb/lib/tree.ts";
|
|
|
|
|
|
|
|
// Extracts front matter (or legacy "meta" code blocks) from a markdown document
|
|
|
|
// optionally removes certain keys from the front matter
|
2023-05-23 18:53:53 +00:00
|
|
|
export async function extractFrontmatter(
|
2022-11-24 11:04:00 +00:00
|
|
|
tree: ParseTree,
|
|
|
|
removeKeys: string[] = [],
|
2023-05-23 18:53:53 +00:00
|
|
|
): Promise<any> {
|
2022-11-24 11:04:00 +00:00
|
|
|
let data: any = {};
|
|
|
|
addParentPointers(tree);
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
await replaceNodesMatchingAsync(tree, async (t) => {
|
2022-11-24 11:04:00 +00:00
|
|
|
// Find top-level hash tags
|
|
|
|
if (t.type === "Hashtag") {
|
|
|
|
// Check if if nested directly into a Paragraph
|
|
|
|
if (t.parent && t.parent.type === "Paragraph") {
|
|
|
|
const tagname = t.children![0].text!.substring(1);
|
|
|
|
if (!data.tags) {
|
|
|
|
data.tags = [];
|
|
|
|
}
|
2023-01-15 10:14:21 +00:00
|
|
|
if (Array.isArray(data.tags) && !data.tags.includes(tagname)) {
|
2022-11-24 11:04:00 +00:00
|
|
|
data.tags.push(tagname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Find FrontMatter and parse it
|
|
|
|
if (t.type === "FrontMatter") {
|
2022-11-24 15:08:51 +00:00
|
|
|
const yamlNode = t.children![1].children![0];
|
|
|
|
const yamlText = renderToText(yamlNode);
|
2022-11-24 11:04:00 +00:00
|
|
|
try {
|
2023-05-23 18:53:53 +00:00
|
|
|
const parsedData: any = await YAML.parse(yamlText);
|
2022-11-24 11:04:00 +00:00
|
|
|
const newData = { ...parsedData };
|
|
|
|
data = { ...data, ...parsedData };
|
|
|
|
if (removeKeys.length > 0) {
|
|
|
|
let removedOne = false;
|
|
|
|
|
|
|
|
for (const key of removeKeys) {
|
|
|
|
if (key in newData) {
|
|
|
|
delete newData[key];
|
|
|
|
removedOne = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (removedOne) {
|
2023-05-23 18:53:53 +00:00
|
|
|
yamlNode.text = await YAML.stringify(newData);
|
2022-11-24 11:04:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If nothing is left, let's just delete this whole block
|
|
|
|
if (Object.keys(newData).length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error("Could not parse frontmatter", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find a fenced code block with `meta` as the language type
|
|
|
|
if (t.type !== "FencedCode") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const codeInfoNode = findNodeOfType(t, "CodeInfo");
|
|
|
|
if (!codeInfoNode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (codeInfoNode.children![0].text !== "meta") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const codeTextNode = findNodeOfType(t, "CodeText");
|
|
|
|
if (!codeTextNode) {
|
|
|
|
// Honestly, this shouldn't happen
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const codeText = codeTextNode.children![0].text!;
|
|
|
|
const parsedData: any = YAML.parse(codeText);
|
|
|
|
const newData = { ...parsedData };
|
|
|
|
data = { ...data, ...parsedData };
|
|
|
|
if (removeKeys.length > 0) {
|
|
|
|
let removedOne = false;
|
|
|
|
for (const key of removeKeys) {
|
|
|
|
if (key in newData) {
|
|
|
|
delete newData[key];
|
|
|
|
removedOne = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (removedOne) {
|
2023-05-23 18:53:53 +00:00
|
|
|
codeTextNode.children![0].text = (await YAML.stringify(newData)).trim();
|
2022-11-24 11:04:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If nothing is left, let's just delete this whole block
|
|
|
|
if (Object.keys(newData).length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (data.name) {
|
|
|
|
data.displayName = data.name;
|
|
|
|
delete data.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates the front matter of a markdown document and returns the text as a rendered string
|
2023-05-23 18:53:53 +00:00
|
|
|
export async function prepareFrontmatterDispatch(
|
2022-11-24 11:04:00 +00:00
|
|
|
tree: ParseTree,
|
|
|
|
data: Record<string, any>,
|
2023-05-23 18:53:53 +00:00
|
|
|
): Promise<any> {
|
2022-11-24 11:04:00 +00:00
|
|
|
let dispatchData: any = null;
|
2023-05-23 18:53:53 +00:00
|
|
|
await traverseTreeAsync(tree, async (t) => {
|
2022-11-24 11:04:00 +00:00
|
|
|
// Find FrontMatter and parse it
|
|
|
|
if (t.type === "FrontMatter") {
|
|
|
|
const bodyNode = t.children![1].children![0];
|
|
|
|
const yamlText = renderToText(bodyNode);
|
|
|
|
|
|
|
|
try {
|
2023-05-23 18:53:53 +00:00
|
|
|
const parsedYaml = await YAML.parse(yamlText) as any;
|
2022-11-24 11:04:00 +00:00
|
|
|
const newData = { ...parsedYaml, ...data };
|
|
|
|
// Patch inline
|
|
|
|
dispatchData = {
|
|
|
|
changes: {
|
|
|
|
from: bodyNode.from,
|
|
|
|
to: bodyNode.to,
|
2023-05-23 18:53:53 +00:00
|
|
|
insert: await YAML.stringify(newData),
|
2022-11-24 11:04:00 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error("Error parsing YAML", e);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
if (!dispatchData) {
|
|
|
|
// If we didn't find frontmatter, let's add it
|
|
|
|
dispatchData = {
|
|
|
|
changes: {
|
|
|
|
from: 0,
|
|
|
|
to: 0,
|
2023-05-23 18:53:53 +00:00
|
|
|
insert: "---\n" + await YAML.stringify(data) +
|
2022-11-24 11:04:00 +00:00
|
|
|
"---\n",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return dispatchData;
|
|
|
|
}
|