1
0
silverbullet/plugs/directive/command.ts

106 lines
3.6 KiB
TypeScript
Raw Normal View History

2022-11-19 14:22:43 +00:00
import { editor, markdown, system } from "$sb/silverbullet-syscall/mod.ts";
import { nodeAtPos } from "$sb/lib/tree.ts";
import { replaceAsync } from "$sb/lib/util.ts";
import { directiveRegex, renderDirectives } from "./directives.ts";
2022-11-24 11:04:00 +00:00
import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
2022-11-24 15:08:51 +00:00
export async function updateDirectivesOnPageCommand(arg: any) {
// If `arg` is a string, it's triggered automatically via an event, not explicitly via a command
const explicitCall = typeof arg !== "string";
2022-11-19 14:22:43 +00:00
const pageName = await editor.getCurrentPage();
const text = await editor.getText();
const tree = await markdown.parseMarkdown(text);
2022-11-24 11:04:00 +00:00
const metaData = extractFrontmatter(tree, ["$disableDirectives"]);
2022-11-19 14:22:43 +00:00
if (metaData.$disableDirectives) {
// Not updating, directives disabled
return;
}
2022-11-19 14:22:43 +00:00
2022-11-24 15:08:51 +00:00
// If this page is shared ($share) via collab: disable directives as well
// due to security concerns
if (metaData.$share) {
for (const uri of metaData.$share) {
if (uri.startsWith("collab:")) {
if (explicitCall) {
await editor.flashNotification(
"Directives are disabled for 'collab' pages (safety reasons).",
"error",
);
}
return;
}
}
}
2022-11-19 14:22:43 +00:00
// Collect all directives and their body replacements
2022-11-23 09:05:01 +00:00
const replacements: { fullMatch: string; text?: string }[] = [];
2022-11-19 14:22:43 +00:00
await replaceAsync(
text,
directiveRegex,
async (fullMatch, startInst, _type, _arg, _body, endInst, index) => {
2022-11-23 09:05:01 +00:00
const replacement: { fullMatch: string; text?: string } = { fullMatch };
2022-11-19 14:22:43 +00:00
// Pushing to the replacement array
const currentNode = nodeAtPos(tree, index + 1);
if (currentNode?.type !== "CommentBlock") {
// If not a comment block, it's likely a code block, ignore
2022-11-25 15:01:05 +00:00
// console.log("Not comment block, ignoring", fullMatch);
2022-11-19 14:22:43 +00:00
return fullMatch;
}
2022-11-25 12:47:10 +00:00
replacements.push(replacement);
2022-11-19 14:22:43 +00:00
try {
const replacementText = await system.invokeFunction(
"server",
"serverRenderDirective",
pageName,
fullMatch,
);
replacement.text = replacementText;
return replacementText;
} catch (e: any) {
return `${startInst}\n**ERROR:** ${e.message}\n${endInst}`;
}
},
);
// Iterate again and replace the bodies. Iterating again (not using previous positions)
// because text may have changed in the mean time (directive processing may take some time)
// Hypothetically in the mean time directives in text may have been changed/swapped, in which
// case this will break. This would be a rare edge case, however.
2022-11-23 09:05:01 +00:00
for (const replacement of replacements) {
// Fetch the text every time, because dispatch() will have been made changes
const text = await editor.getText();
// Determine the current position
const index = text.indexOf(replacement.fullMatch);
// This may happen if the query itself, or the user is editing inside the directive block (WHY!?)
if (index === -1) {
console.warn(
"Could not find directive in text, skipping",
replacement.fullMatch,
);
continue;
}
2022-11-24 15:08:51 +00:00
const from = index, to = index + replacement.fullMatch.length;
if (text.substring(from, to) === replacement.text) {
// No change, skip
continue;
}
2022-11-23 09:05:01 +00:00
await editor.dispatch({
changes: {
2022-11-24 15:08:51 +00:00
from,
to,
2022-11-23 09:05:01 +00:00
insert: replacement.text,
},
});
}
}
// Called from client, running on server
2022-11-19 14:22:43 +00:00
// The text passed here is going to be a single directive block (not a full page)
export function serverRenderDirective(
pageName: string,
2022-11-19 14:22:43 +00:00
text: string,
): Promise<string> {
return renderDirectives(pageName, text);
}