Cleanup and federation prep
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { editor, markdown, space } from "$sb/silverbullet-syscall/mod.ts";
|
||||
import { editor, markdown, space, sync } from "$sb/silverbullet-syscall/mod.ts";
|
||||
import {
|
||||
removeParentPointers,
|
||||
renderToText,
|
||||
@@ -7,15 +7,29 @@ import {
|
||||
import { renderDirectives } from "./directives.ts";
|
||||
import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
|
||||
import { PageMeta } from "../../web/types.ts";
|
||||
import { isFederationPath } from "$sb/lib/resolve.ts";
|
||||
|
||||
export async function updateDirectivesOnPageCommand() {
|
||||
// If `arg` is a string, it's triggered automatically via an event, not explicitly via a command
|
||||
const pageMeta = await space.getPageMeta(await editor.getCurrentPage());
|
||||
const currentPage = await editor.getCurrentPage();
|
||||
const pageMeta = await space.getPageMeta(currentPage);
|
||||
const text = await editor.getText();
|
||||
const tree = await markdown.parseMarkdown(text);
|
||||
const metaData = await extractFrontmatter(tree, ["$disableDirectives"]);
|
||||
|
||||
if (isFederationPath(currentPage)) {
|
||||
console.info("Current page is a federation page, not updating directives.");
|
||||
}
|
||||
|
||||
if (metaData.$disableDirectives) {
|
||||
// Not updating, directives disabled
|
||||
console.info("Directives disabled in page meta, not updating them.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(await sync.hasInitialSyncCompleted())) {
|
||||
console.info(
|
||||
"Initial sync hasn't completed yet, not updating directives.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -96,7 +110,6 @@ export async function updateDirectivesOnPageCommand() {
|
||||
}
|
||||
}
|
||||
|
||||
// Pure server driven implementation of directive updating
|
||||
export async function updateDirectives(
|
||||
pageMeta: PageMeta,
|
||||
text: string,
|
||||
|
||||
@@ -35,14 +35,6 @@ export async function directiveDispatcher(
|
||||
const directiveStartText = renderToText(directiveStart).trim();
|
||||
const directiveEndText = renderToText(directiveEnd).trim();
|
||||
|
||||
if (!(await sync.hasInitialSyncCompleted())) {
|
||||
console.info(
|
||||
"Initial sync hasn't completed yet, not updating directives.",
|
||||
);
|
||||
// Render the query directive as-is
|
||||
return renderToText(directiveTree);
|
||||
}
|
||||
|
||||
if (directiveStart.children!.length === 1) {
|
||||
// Everything not #query
|
||||
const match = directiveStartRegex.exec(directiveStart.children![0].text!);
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import { queryRegex } from "$sb/lib/query.ts";
|
||||
import { ParseTree, renderToText } from "$sb/lib/tree.ts";
|
||||
import {
|
||||
findNodeOfType,
|
||||
ParseTree,
|
||||
renderToText,
|
||||
traverseTree,
|
||||
} from "$sb/lib/tree.ts";
|
||||
import { markdown, space } from "$sb/silverbullet-syscall/mod.ts";
|
||||
import Handlebars from "handlebars";
|
||||
|
||||
@@ -9,6 +14,7 @@ import { directiveRegex } from "./directives.ts";
|
||||
import { updateDirectives } from "./command.ts";
|
||||
import { buildHandebarOptions } from "./util.ts";
|
||||
import { PageMeta } from "../../web/types.ts";
|
||||
import { resolvePath } from "$sb/lib/resolve.ts";
|
||||
|
||||
const templateRegex = /\[\[([^\]]+)\]\]\s*(.*)\s*/;
|
||||
|
||||
@@ -24,7 +30,7 @@ export async function templateDirectiveRenderer(
|
||||
if (!match) {
|
||||
throw new Error(`Invalid template directive: ${arg}`);
|
||||
}
|
||||
const template = match[1];
|
||||
let templatePath = match[1];
|
||||
const args = match[2];
|
||||
let parsedArgs = {};
|
||||
if (args) {
|
||||
@@ -39,20 +45,29 @@ export async function templateDirectiveRenderer(
|
||||
}
|
||||
}
|
||||
let templateText = "";
|
||||
if (template.startsWith("http://") || template.startsWith("https://")) {
|
||||
if (
|
||||
templatePath.startsWith("http://") || templatePath.startsWith("https://")
|
||||
) {
|
||||
try {
|
||||
const req = await fetch(template);
|
||||
const req = await fetch(templatePath);
|
||||
templateText = await req.text();
|
||||
} catch (e: any) {
|
||||
templateText = `ERROR: ${e.message}`;
|
||||
}
|
||||
} else {
|
||||
templateText = await space.readPage(template);
|
||||
templatePath = resolvePath(pageMeta.name, templatePath);
|
||||
templateText = await space.readPage(templatePath);
|
||||
}
|
||||
const tree = await markdown.parseMarkdown(templateText);
|
||||
await extractFrontmatter(tree, [], true); // Remove entire frontmatter section, if any
|
||||
|
||||
// Resolve paths in the template
|
||||
rewritePageRefs(tree, templatePath);
|
||||
|
||||
let newBody = renderToText(tree);
|
||||
|
||||
// console.log("Rewritten template:", newBody);
|
||||
|
||||
// if it's a template injection (not a literal "include")
|
||||
if (directive === "use") {
|
||||
const templateFn = Handlebars.compile(
|
||||
@@ -67,6 +82,40 @@ export async function templateDirectiveRenderer(
|
||||
return newBody.trim();
|
||||
}
|
||||
|
||||
function rewritePageRefs(tree: ParseTree, templatePath: string) {
|
||||
traverseTree(tree, (n): boolean => {
|
||||
if (n.type === "DirectiveStart") {
|
||||
const pageRef = findNodeOfType(n, "PageRef")!;
|
||||
if (pageRef) {
|
||||
const pageRefName = pageRef.children![0].text!.slice(2, -2);
|
||||
pageRef.children![0].text = `[[${
|
||||
resolvePath(templatePath, pageRefName)
|
||||
}]]`;
|
||||
}
|
||||
const directiveText = n.children![0].text;
|
||||
// #use or #import
|
||||
if (directiveText) {
|
||||
const match = /\[\[(.+)\]\]/.exec(directiveText);
|
||||
if (match) {
|
||||
const pageRefName = match[1];
|
||||
n.children![0].text = directiveText.replace(
|
||||
match[0],
|
||||
`[[${resolvePath(templatePath, pageRefName)}]]`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
if (n.type === "WikiLinkPage") {
|
||||
n.children![0].text = resolvePath(templatePath, n.children![0].text!);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
export function cleanTemplateInstantiations(text: string) {
|
||||
return text.replaceAll(directiveRegex, (
|
||||
_fullMatch,
|
||||
|
||||
Reference in New Issue
Block a user