Fixes #319
This commit is contained in:
+27
-14
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user