1
0
silverbullet/plug-api/lib/frontmatter.ts

179 lines
5.2 KiB
TypeScript
Raw Normal View History

import { YAML } from "$sb/plugos-syscall/mod.ts";
2022-11-24 11:04:00 +00:00
import {
addParentPointers,
ParseTree,
renderToText,
replaceNodesMatchingAsync,
traverseTreeAsync,
2022-11-24 11:04:00 +00:00
} from "$sb/lib/tree.ts";
export type FrontMatter = { tags?: string[] } & Record<string, any>;
2023-11-06 08:14:16 +00:00
export type FrontmatterExtractOptions = {
removeKeys?: string[];
removeTags?: string[] | true;
removeFrontmatterSection?: boolean;
};
/**
* Extracts front matter from a markdown document, as well as extracting tags that are to apply to the page
* optionally removes certain keys from the front matter
* Side effect: will add parent pointers
*/
export async function extractFrontmatter(
2022-11-24 11:04:00 +00:00
tree: ParseTree,
options: FrontmatterExtractOptions = {},
2023-11-06 08:14:16 +00:00
): Promise<FrontMatter> {
let data: FrontMatter = {
tags: [],
};
const tags: string[] = [];
2022-11-24 11:04:00 +00:00
addParentPointers(tree);
await replaceNodesMatchingAsync(tree, async (t) => {
// Find tags in paragraphs directly nested under the document where the only content is tags
if (t.type === "Paragraph" && t.parent?.type === "Document") {
let onlyTags = true;
const collectedTags = new Set<string>();
for (const child of t.children!) {
if (child.text) {
if (child.text.startsWith("\n") && child.text !== "\n") {
// Multi line paragraph, cut it off here
break;
}
if (child.text.trim()) {
// Text node with actual text (not just whitespace): not a page tag line!
onlyTags = false;
break;
}
} else if (child.type === "Hashtag") {
const tagname = child.children![0].text!.substring(1);
collectedTags.add(tagname);
if (
options.removeTags === true || options.removeTags?.includes(tagname)
) {
// Ugly hack to remove the hashtag
child.children![0].text = "";
}
} else if (child.type) {
// Found something else than tags, so... nope!
onlyTags = false;
break;
}
}
if (onlyTags) {
tags.push(...collectedTags);
}
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 {
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
// support "tag1, tag2" as well as "tag1 tag2" as well as "#tag1 #tag2" notations
2023-11-06 08:14:16 +00:00
if (typeof data.tags === "string") {
tags.push(...(data.tags as string).split(/,\s*|\s+/));
2023-11-06 08:14:16 +00:00
}
if (options.removeKeys && options.removeKeys.length > 0) {
2022-11-24 11:04:00 +00:00
let removedOne = false;
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) {
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 || 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;
});
// Strip # from tags
data.tags = [...new Set([...tags.map((t) => t.replace(/^#/, ""))])];
// console.log("Extracted tags", data.tags);
2022-11-24 11:04:00 +00:00
return data;
}
// Updates the front matter of a markdown document and returns the text as a rendered string
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>,
): Promise<any> {
2022-11-24 11:04:00 +00:00
let dispatchData: any = null;
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;
}