More ways to define tags in frontmatter
This commit is contained in:
@@ -2,7 +2,7 @@ import type { CompleteEvent } from "$sb/app_event.ts";
|
||||
import { events } from "$sb/syscalls.ts";
|
||||
import { queryObjects } from "./api.ts";
|
||||
import { ObjectValue, QueryExpression } from "$sb/types.ts";
|
||||
import { determineTags } from "./cheap_yaml.ts";
|
||||
import { determineTags } from "../../plug-api/lib/cheap_yaml.ts";
|
||||
|
||||
export type AttributeObject = ObjectValue<{
|
||||
name: string;
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
import { assertEquals } from "../../test_deps.ts";
|
||||
import { determineTags } from "./cheap_yaml.ts";
|
||||
|
||||
Deno.test("cheap yaml", () => {
|
||||
assertEquals([], determineTags(""));
|
||||
assertEquals([], determineTags("hank: bla"));
|
||||
assertEquals(["template"], determineTags("tags: template"));
|
||||
assertEquals(["bla", "template"], determineTags("tags: bla,template"));
|
||||
assertEquals(["bla", "template"], determineTags("tags:\n- bla\n- template"));
|
||||
});
|
||||
@@ -1,34 +0,0 @@
|
||||
const yamlKvRegex = /^\s*(\w+):\s*(.*)/;
|
||||
const yamlListItemRegex = /^\s*-\s+(.+)/;
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
tags.push(...value.split(/,\s*/));
|
||||
}
|
||||
} else {
|
||||
inTagsSection = false;
|
||||
}
|
||||
}
|
||||
const yamlListem = yamlListItemRegex.exec(line);
|
||||
if (yamlListem && inTagsSection) {
|
||||
tags.push(yamlListem[1]);
|
||||
}
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
+17
-6
@@ -62,17 +62,28 @@ export async function tagComplete(completeEvent: CompleteEvent) {
|
||||
}
|
||||
const tagPrefix = match[0].substring(1);
|
||||
let parent = "page";
|
||||
if (taskPrefixRegex.test(completeEvent.linePrefix)) {
|
||||
parent = "task";
|
||||
} else if (itemPrefixRegex.test(completeEvent.linePrefix)) {
|
||||
parent = "item";
|
||||
if (!completeEvent.parentNodes.find((n) => n.startsWith("FrontMatter:"))) {
|
||||
if (taskPrefixRegex.test(completeEvent.linePrefix)) {
|
||||
parent = "task";
|
||||
} else if (itemPrefixRegex.test(completeEvent.linePrefix)) {
|
||||
parent = "item";
|
||||
}
|
||||
}
|
||||
|
||||
// Query all tags
|
||||
const allTags = await queryObjects<TagObject>("tag", {
|
||||
// Query all tags with a matching parent
|
||||
const allTags: any[] = await queryObjects<TagObject>("tag", {
|
||||
filter: ["=", ["attr", "parent"], ["string", parent]],
|
||||
select: [{ name: "name" }],
|
||||
distinct: true,
|
||||
});
|
||||
|
||||
if (parent === "page") {
|
||||
// Also add template, even though that would otherwise not appear because has "builtin" as a parent
|
||||
allTags.push({
|
||||
name: "template",
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
from: completeEvent.pos - tagPrefix.length,
|
||||
options: allTags.map((tag) => ({
|
||||
|
||||
+5
-26
@@ -1,6 +1,6 @@
|
||||
import { determineTags } from "$sb/lib/cheap_yaml.ts";
|
||||
|
||||
const frontMatterRegex = /^---\n(([^\n]|\n)*?)---\n/;
|
||||
const yamlKvRegex = /^\s*(\w+):\s*(.*)/;
|
||||
const yamlListItemRegex = /^\s*-\s+(.+)/;
|
||||
|
||||
/**
|
||||
* Quick and dirty way to check if a page is a template or not
|
||||
@@ -13,30 +13,9 @@ export function isTemplate(pageText: string): boolean {
|
||||
if (frontmatter) {
|
||||
pageText = pageText.slice(frontmatter[0].length);
|
||||
const frontmatterText = frontmatter[1];
|
||||
const lines = frontmatterText.split("\n");
|
||||
let inTagsSection = false;
|
||||
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.split(/,\s*/).includes("template")) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
inTagsSection = false;
|
||||
}
|
||||
}
|
||||
const yamlListem = yamlListItemRegex.exec(line);
|
||||
if (yamlListem && inTagsSection) {
|
||||
// List item is 'template'? Yay!
|
||||
if (yamlListem[1] === "template") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
const tags = determineTags(frontmatterText);
|
||||
if (tags.includes("template")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// Or if the page text starts with a #template tag
|
||||
|
||||
Reference in New Issue
Block a user