2022-11-01 14:01:28 +00:00
|
|
|
import { editor, space, system } from "$sb/silverbullet-syscall/mod.ts";
|
2022-10-28 14:17:40 +00:00
|
|
|
import { renderDirectives } from "./directives.ts";
|
|
|
|
|
|
|
|
export async function updateDirectivesOnPageCommand() {
|
|
|
|
const currentPage = await editor.getCurrentPage();
|
|
|
|
await editor.save();
|
|
|
|
if (
|
2022-11-01 14:01:28 +00:00
|
|
|
await system.invokeFunction(
|
2022-10-28 14:17:40 +00:00
|
|
|
"server",
|
|
|
|
"updateDirectivesOnPage",
|
|
|
|
currentPage,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
await editor.reloadPage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called from client, running on server
|
|
|
|
export async function updateDirectivesOnPage(
|
|
|
|
pageName: string,
|
|
|
|
): Promise<boolean> {
|
|
|
|
let text = "";
|
|
|
|
try {
|
|
|
|
text = await space.readPage(pageName);
|
|
|
|
} catch {
|
|
|
|
console.warn(
|
|
|
|
"Could not read page",
|
|
|
|
pageName,
|
|
|
|
"perhaps it doesn't yet exist",
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const newText = await renderDirectives(pageName, text);
|
|
|
|
if (text !== newText) {
|
|
|
|
await space.writePage(pageName, newText);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|