From 8eebb8dd67ac1f7e74652ddd8c98404120fc72ac Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Fri, 7 Feb 2025 15:39:51 +0100 Subject: [PATCH] Expose index.extractFrontmatter as syscall --- plugs/index/api.ts | 21 ++++++++++++++++++++- plugs/index/index.plug.yaml | 6 ++++++ web/hooks/syscall.ts | 2 +- website/API/index.md | 22 ++++++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/plugs/index/api.ts b/plugs/index/api.ts index b45984a4..227f6562 100644 --- a/plugs/index/api.ts +++ b/plugs/index/api.ts @@ -1,4 +1,8 @@ -import { datastore, system } from "@silverbulletmd/silverbullet/syscalls"; +import { + datastore, + markdown, + system, +} from "@silverbulletmd/silverbullet/syscalls"; import type { KV, KvKey, @@ -10,6 +14,12 @@ import type { QueryProviderEvent } from "../../plug-api/types.ts"; import { determineType, type SimpleJSONType } from "./attributes.ts"; import { ttlCache } from "$lib/memory_cache.ts"; import type { LuaCollectionQuery } from "$common/space_lua/query_collection.ts"; +import { + extractFrontmatter as extractFrontmatterFromTree, + type FrontMatter, + type FrontmatterExtractOptions, +} from "../../plug-api/lib/frontmatter.ts"; +import { renderToText } from "@silverbulletmd/silverbullet/lib/tree"; const indexKey = "idx"; const pageKey = "ridx"; @@ -245,3 +255,12 @@ export async function discoverSources() { // And concatenate all the tags from the schema .concat(Object.keys(schema.tag)); } + +export async function extractFrontmatter( + text: string, + extractOptions: FrontmatterExtractOptions = {}, +): Promise<{ frontmatter: FrontMatter; text: string }> { + const tree = await markdown.parseMarkdown(text); + const frontmatter = await extractFrontmatterFromTree(tree, extractOptions); + return { frontmatter, text: renderToText(tree) }; +} diff --git a/plugs/index/index.plug.yaml b/plugs/index/index.plug.yaml index 39e0ee8c..befc123f 100644 --- a/plugs/index/index.plug.yaml +++ b/plugs/index/index.plug.yaml @@ -23,6 +23,12 @@ functions: path: api.ts:getObjectByRef syscall: index.getObjectByRef env: server + + extractFrontmatter: + path: api.ts:extractFrontmatter + syscall: index.extractFrontmatter + + # Event handlers objectSourceProvider: path: api.ts:objectSourceProvider events: diff --git a/web/hooks/syscall.ts b/web/hooks/syscall.ts index bf22d58a..763ccdb0 100644 --- a/web/hooks/syscall.ts +++ b/web/hooks/syscall.ts @@ -32,7 +32,7 @@ export class SyscallHook implements Hook { syscalls[syscallName] = (ctx, ...args) => { // Delegate to the system to invoke the function return system.syscall(ctx, "system.invokeFunction", [ - name, + `${plug.manifest!.name}.${name}`, ...args, ]); }; diff --git a/website/API/index.md b/website/API/index.md index daae7b32..bb92a4f5 100644 --- a/website/API/index.md +++ b/website/API/index.md @@ -1,3 +1,9 @@ +--- +testattribute: 10 +--- + +#apidoc + The `index` API provides functions for interacting with SilverBullet's [[Objects]], allowing you to store and query page-associated data. ## Object Operations @@ -38,3 +44,19 @@ local task = index.getObjectByRef("my page", "mytask", "task1") if task then print("Found task: " .. task.content) end +``` + +## index.extractFrontmatter(text, extractOptions) +Extracts frontmatter from a markdown document (whose text is provided as argument), possibly cleaning it up. It also parses top-level tags consistent with SilverBullet's tag indexing system. + +It returns a table with two keys: +- `frontmatter`: A table containing the parsed frontmatter. +- `text`: The text of the document, with any changes applied requested with the `extractOptions`. + +The `extractOptions` is an optional table that can contain the following keys (which will affect the returned `text`): +- `removeKeys`: An array of keys to remove from the frontmatter. +- `removeTags`: A boolean or array of tags to remove from the frontmatter. +- `removeFrontmatterSection`: A boolean to remove the frontmatter section from the document. + +Example applied to this page: +${(index.extractFrontmatter(editor.getText())).frontmatter} \ No newline at end of file