2023-12-22 12:59:16 +00:00
|
|
|
const yamlKvRegex = /^\s*(\w+):\s*["']?([^'"]*)["']?$/;
|
|
|
|
const yamlListItemRegex = /^\s*-\s+["']?([^'"]+)["']?$/;
|
2023-12-21 17:37:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cheap YAML parser to determine tags (ugly, regex based but fast)
|
|
|
|
* @param yamlText
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export function determineTags(yamlText: string): string[] {
|
|
|
|
const lines = yamlText.split("\n");
|
|
|
|
let inTagsSection = false;
|
|
|
|
const tags: string[] = [];
|
|
|
|
for (const line of lines) {
|
|
|
|
const yamlKv = yamlKvRegex.exec(line);
|
|
|
|
if (yamlKv) {
|
|
|
|
const [key, value] = yamlKv.slice(1);
|
|
|
|
// Looking for a 'tags' key
|
|
|
|
if (key === "tags") {
|
|
|
|
inTagsSection = true;
|
|
|
|
// 'template' there? Yay!
|
|
|
|
if (value) {
|
2023-12-22 12:59:16 +00:00
|
|
|
tags.push(
|
|
|
|
...value.split(/,\s*|\s+/).map((t) => t.replace(/^#/, "")),
|
|
|
|
);
|
2023-12-21 17:37:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inTagsSection = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const yamlListem = yamlListItemRegex.exec(line);
|
|
|
|
if (yamlListem && inTagsSection) {
|
2023-12-22 12:59:16 +00:00
|
|
|
tags.push(yamlListem[1].replace(/^#/, ""));
|
2023-12-21 17:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tags;
|
|
|
|
}
|