Robustness and federation sync

This commit is contained in:
Zef Hemel
2023-07-30 11:30:01 +02:00
parent afa160d2c2
commit b584e2ef7e
11 changed files with 264 additions and 110 deletions
+47 -6
View File
@@ -1,3 +1,5 @@
import { findNodeOfType, ParseTree, traverseTree } from "$sb/lib/tree.ts";
export function resolvePath(
currentPage: string,
pathToResolve: string,
@@ -6,12 +8,7 @@ export function resolvePath(
if (isFederationPath(currentPage) && !isFederationPath(pathToResolve)) {
let domainPart = currentPage.split("/")[0];
if (fullUrl) {
domainPart = domainPart.substring(1);
if (domainPart.startsWith("localhost")) {
domainPart = "http://" + domainPart;
} else {
domainPart = "https://" + domainPart;
}
domainPart = federatedPathToUrl(domainPart);
}
return `${domainPart}/${pathToResolve}`;
} else {
@@ -19,6 +16,50 @@ export function resolvePath(
}
}
export function federatedPathToUrl(path: string): string {
path = path.substring(1);
if (path.startsWith("localhost")) {
path = "http://" + path;
} else {
path = "https://" + path;
}
return path;
}
export function isFederationPath(path: string) {
return path.startsWith("!");
}
export 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;
});
}