This commit is contained in:
Zef Hemel
2023-01-23 18:52:17 +01:00
parent 53bf098579
commit e7728c2d2a
10 changed files with 102 additions and 27 deletions
+1
View File
@@ -7,6 +7,7 @@ export type AppEvent =
| "minieditor:complete"
| "page:load"
| "editor:init"
| "editor:modeswitch"
| "plugs:loaded";
export type QueryProviderEvent = {
+27 -14
View File
@@ -2,13 +2,13 @@ import { findNodeOfType, traverseTree } from "$sb/lib/tree.ts";
import { markdown, space } from "$sb/silverbullet-syscall/mod.ts";
import * as YAML from "yaml";
export async function readYamlPage(
export async function readCodeBlockPage(
pageName: string,
allowedLanguages = ["yaml"],
): Promise<any> {
allowedLanguages?: string[],
): Promise<string | undefined> {
const text = await space.readPage(pageName);
const tree = await markdown.parseMarkdown(text);
let data: any = {};
let codeText: string | undefined;
traverseTree(tree, (t): boolean => {
// Find a fenced code block
@@ -16,10 +16,13 @@ export async function readYamlPage(
return false;
}
const codeInfoNode = findNodeOfType(t, "CodeInfo");
if (!codeInfoNode) {
if (allowedLanguages && !codeInfoNode) {
return false;
}
if (!allowedLanguages.includes(codeInfoNode.children![0].text!)) {
if (
allowedLanguages &&
!allowedLanguages.includes(codeInfoNode!.children![0].text!)
) {
return false;
}
const codeTextNode = findNodeOfType(t, "CodeText");
@@ -27,17 +30,27 @@ export async function readYamlPage(
// Honestly, this shouldn't happen
return false;
}
const codeText = codeTextNode.children![0].text!;
try {
data = YAML.parse(codeText);
} catch (e: any) {
console.error("YAML Page parser error", e);
throw new Error(`YAML Error: ${e.message}`);
}
codeText = codeTextNode.children![0].text!;
return true;
});
return data;
return codeText;
}
export async function readYamlPage(
pageName: string,
allowedLanguages = ["yaml"],
): Promise<any> {
const codeText = await readCodeBlockPage(pageName, allowedLanguages);
if (codeText === undefined) {
return undefined;
}
try {
return YAML.parse(codeText);
} catch (e: any) {
console.error("YAML Page parser error", e);
throw new Error(`YAML Error: ${e.message}`);
}
}
export async function writeYamlPage(
+5
View File
@@ -123,3 +123,8 @@ export function getUiOption(key: string): Promise<any> {
export function setUiOption(key: string, value: any): Promise<void> {
return syscall("editor.setUiOption", key, value);
}
// Vim specific
export function vimEx(exCommand: string): Promise<any> {
return syscall("editor.vimEx", exCommand);
}