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,
|
2023-10-03 12:16:33 +00:00
|
|
|
collectNodesOfType,
|
2022-11-24 11:04:00 +00:00
|
|
|
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";
|
|
|
|
|
2023-11-06 08:14:16 +00:00
|
|
|
export type FrontMatter = { tags: string[] } & Record<string, any>;
|
|
|
|
|
2023-11-09 08:26:44 +00:00
|
|
|
export type FrontmatterExtractOptions = {
|
|
|
|
removeKeys?: string[];
|
|
|
|
removeTags?: string[] | true;
|
|
|
|
removeFrontmatterSection?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Extracts front matter from a markdown document
|
2022-11-24 11:04:00 +00:00
|
|
|
// 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,
|
2023-11-09 08:26:44 +00:00
|
|
|
options: FrontmatterExtractOptions = {},
|
2023-11-06 08:14:16 +00:00
|
|
|
): Promise<FrontMatter> {
|
|
|
|
let data: FrontMatter = {
|
|
|
|
tags: [],
|
|
|
|
};
|
2022-11-24 11:04:00 +00:00
|
|
|
addParentPointers(tree);
|
2023-10-03 12:16:33 +00:00
|
|
|
let paragraphCounter = 0;
|
2022-11-24 11:04:00 +00:00
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
await replaceNodesMatchingAsync(tree, async (t) => {
|
2023-11-06 08:14:16 +00:00
|
|
|
// Find tags in the first paragraph to attach to the page
|
2023-10-03 12:16:33 +00:00
|
|
|
if (t.type === "Paragraph") {
|
|
|
|
paragraphCounter++;
|
|
|
|
// Only attach hashtags in the first paragraph to the page
|
|
|
|
if (paragraphCounter !== 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
collectNodesOfType(t, "Hashtag").forEach((h) => {
|
|
|
|
const tagname = h.children![0].text!.substring(1);
|
2023-11-06 08:14:16 +00:00
|
|
|
if (!data.tags.includes(tagname)) {
|
2022-11-24 11:04:00 +00:00
|
|
|
data.tags.push(tagname);
|
|
|
|
}
|
2023-11-09 08:26:44 +00:00
|
|
|
if (
|
|
|
|
options.removeTags === true || options.removeTags?.includes(tagname)
|
|
|
|
) {
|
|
|
|
// Ugly hack to remove the hashtag
|
|
|
|
h.children![0].text = "";
|
|
|
|
}
|
2023-10-03 12:16:33 +00:00
|
|
|
});
|
2022-11-24 11:04:00 +00:00
|
|
|
}
|
|
|
|
// 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 };
|
2023-11-06 08:14:16 +00:00
|
|
|
// Make sure we have a tags array
|
|
|
|
if (!data.tags) {
|
|
|
|
data.tags = [];
|
|
|
|
}
|
|
|
|
// Normalize tags to an array and support a "tag1, tag2" notation
|
|
|
|
if (typeof data.tags === "string") {
|
|
|
|
data.tags = (data.tags as string).split(/,\s*/);
|
|
|
|
}
|
2023-11-09 08:26:44 +00:00
|
|
|
if (options.removeKeys && options.removeKeys.length > 0) {
|
2022-11-24 11:04:00 +00:00
|
|
|
let removedOne = false;
|
|
|
|
|
2023-11-09 08:26:44 +00:00
|
|
|
for (const key of options.removeKeys) {
|
2022-11-24 11:04:00 +00:00
|
|
|
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
|
2023-11-09 08:26:44 +00:00
|
|
|
if (
|
|
|
|
Object.keys(newData).length === 0 || options.removeFrontmatterSection
|
|
|
|
) {
|
2022-11-24 11:04:00 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch (e: any) {
|
2023-08-01 19:35:19 +00:00
|
|
|
console.warn("Could not parse frontmatter", e.message);
|
2022-11-24 11:04:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2023-11-13 14:49:21 +00:00
|
|
|
data: string | 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-11-13 14:49:21 +00:00
|
|
|
let frontmatterText = "";
|
|
|
|
if (typeof data === "string") {
|
|
|
|
frontmatterText = yamlText + data + "\n";
|
|
|
|
} else {
|
|
|
|
const parsedYaml = await YAML.parse(yamlText) as any;
|
|
|
|
const newData = { ...parsedYaml, ...data };
|
|
|
|
frontmatterText = await YAML.stringify(newData);
|
|
|
|
}
|
2022-11-24 11:04:00 +00:00
|
|
|
// Patch inline
|
|
|
|
dispatchData = {
|
|
|
|
changes: {
|
|
|
|
from: bodyNode.from,
|
|
|
|
to: bodyNode.to,
|
2023-11-13 14:49:21 +00:00
|
|
|
insert: frontmatterText,
|
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
|
2023-11-13 14:49:21 +00:00
|
|
|
let frontmatterText = "";
|
|
|
|
if (typeof data === "string") {
|
|
|
|
frontmatterText = data + "\n";
|
|
|
|
} else {
|
|
|
|
frontmatterText = await YAML.stringify(data);
|
|
|
|
}
|
|
|
|
const fullFrontmatterText = "---\n" + frontmatterText +
|
|
|
|
"---\n";
|
2022-11-24 11:04:00 +00:00
|
|
|
dispatchData = {
|
|
|
|
changes: {
|
|
|
|
from: 0,
|
|
|
|
to: 0,
|
2023-11-13 14:49:21 +00:00
|
|
|
insert: fullFrontmatterText,
|
2022-11-24 11:04:00 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return dispatchData;
|
|
|
|
}
|