2023-07-30 09:30:01 +00:00
|
|
|
import { findNodeOfType, ParseTree, traverseTree } from "$sb/lib/tree.ts";
|
|
|
|
|
2023-07-29 21:41:37 +00:00
|
|
|
export function resolvePath(
|
|
|
|
currentPage: string,
|
|
|
|
pathToResolve: string,
|
|
|
|
fullUrl = false,
|
|
|
|
): string {
|
2023-07-30 06:56:44 +00:00
|
|
|
if (isFederationPath(currentPage) && !isFederationPath(pathToResolve)) {
|
2023-07-29 21:41:37 +00:00
|
|
|
let domainPart = currentPage.split("/")[0];
|
|
|
|
if (fullUrl) {
|
2023-07-30 09:30:01 +00:00
|
|
|
domainPart = federatedPathToUrl(domainPart);
|
2023-07-29 21:41:37 +00:00
|
|
|
}
|
|
|
|
return `${domainPart}/${pathToResolve}`;
|
|
|
|
} else {
|
|
|
|
return pathToResolve;
|
|
|
|
}
|
|
|
|
}
|
2023-07-30 06:56:44 +00:00
|
|
|
|
2023-12-19 16:55:11 +00:00
|
|
|
export function resolveAttachmentPath(
|
|
|
|
currentPage: string,
|
|
|
|
pathToResolve: string,
|
|
|
|
): string {
|
|
|
|
const folder = folderName(currentPage);
|
|
|
|
if (folder && !pathToResolve.startsWith("/")) {
|
|
|
|
pathToResolve = folder + "/" + pathToResolve;
|
|
|
|
}
|
|
|
|
if (pathToResolve.startsWith("/")) {
|
|
|
|
pathToResolve = pathToResolve.slice(1);
|
|
|
|
}
|
|
|
|
return federatedPathToUrl(resolvePath(currentPage, pathToResolve));
|
|
|
|
}
|
|
|
|
|
2023-07-30 09:30:01 +00:00
|
|
|
export function federatedPathToUrl(path: string): string {
|
2023-12-19 16:55:11 +00:00
|
|
|
if (!path.startsWith("!")) {
|
|
|
|
return path;
|
|
|
|
}
|
2023-07-30 09:30:01 +00:00
|
|
|
path = path.substring(1);
|
|
|
|
if (path.startsWith("localhost")) {
|
|
|
|
path = "http://" + path;
|
|
|
|
} else {
|
|
|
|
path = "https://" + path;
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2023-07-30 06:56:44 +00:00
|
|
|
export function isFederationPath(path: string) {
|
|
|
|
return path.startsWith("!");
|
|
|
|
}
|
2023-07-30 09:30:01 +00:00
|
|
|
|
2023-07-30 17:31:04 +00:00
|
|
|
export function rewritePageRefs(tree: ParseTree, containerPageName: string) {
|
2023-07-30 09:30:01 +00:00
|
|
|
traverseTree(tree, (n): boolean => {
|
2023-11-15 15:14:15 +00:00
|
|
|
if (n.type === "FencedCode") {
|
|
|
|
const codeInfo = findNodeOfType(n, "CodeInfo");
|
|
|
|
if (!codeInfo) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!["query", "template"].includes(codeInfo.children![0].text!)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const codeText = findNodeOfType(n, "CodeText");
|
|
|
|
if (!codeText) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
let bodyText = codeText.children![0].text!;
|
|
|
|
bodyText = rewritePageRefsInString(bodyText, containerPageName);
|
|
|
|
codeText.children![0].text = bodyText;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-07-30 09:30:01 +00:00
|
|
|
if (n.type === "WikiLinkPage") {
|
2023-07-30 17:31:04 +00:00
|
|
|
n.children![0].text = resolvePath(
|
|
|
|
containerPageName,
|
|
|
|
n.children![0].text!,
|
|
|
|
);
|
2023-07-30 09:30:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2023-08-17 18:27:05 +00:00
|
|
|
|
2023-11-15 15:14:15 +00:00
|
|
|
export function rewritePageRefsInString(
|
|
|
|
bodyText: string,
|
|
|
|
containerPageName: string,
|
|
|
|
) {
|
|
|
|
return bodyText.replaceAll(/\[\[(.+)\]\]/g, (_match, pageRefName) => {
|
|
|
|
return `[[${resolvePath(containerPageName, pageRefName)}]]`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-17 18:27:05 +00:00
|
|
|
export function cleanPageRef(pageRef: string) {
|
|
|
|
if (pageRef.startsWith("[[") && pageRef.endsWith("]]")) {
|
|
|
|
return pageRef.slice(2, -2);
|
|
|
|
} else {
|
|
|
|
return pageRef;
|
|
|
|
}
|
|
|
|
}
|
2023-12-19 16:55:11 +00:00
|
|
|
|
|
|
|
export function folderName(path: string) {
|
|
|
|
return path.split("/").slice(0, -1).join("/");
|
|
|
|
}
|