More fixes related to #363

This commit is contained in:
Zef Hemel
2023-12-19 17:55:11 +01:00
parent a5c7405a35
commit 30436d97ea
9 changed files with 70 additions and 31 deletions
+19
View File
@@ -1,6 +1,7 @@
import {
cleanPageRef,
federatedPathToUrl,
resolveAttachmentPath,
resolvePath,
rewritePageRefs,
} from "$sb/lib/resolve.ts";
@@ -90,4 +91,22 @@ This is a [[!silverbullet.md/local link]] and [[!silverbullet.md/local link|with
rewrittenText,
`This is a [[local link]] and [[local link|with alias]].`,
);
assertEquals("test.jpg", resolveAttachmentPath("test", "test.jpg"));
assertEquals(
"folder/test.jpg",
resolveAttachmentPath("folder/test", "test.jpg"),
);
assertEquals(
"test.jpg",
resolveAttachmentPath("folder/test", "/test.jpg"),
);
assertEquals(
"https://silverbullet.md/something/test.jpg",
resolveAttachmentPath("!silverbullet.md/something/bla", "test.jpg"),
);
assertEquals(
"https://silverbullet.md/test.jpg",
resolveAttachmentPath("!silverbullet.md/something/bla", "/test.jpg"),
);
});
+21
View File
@@ -16,7 +16,24 @@ export function resolvePath(
}
}
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));
}
export function federatedPathToUrl(path: string): string {
if (!path.startsWith("!")) {
return path;
}
path = path.substring(1);
if (path.startsWith("localhost")) {
path = "http://" + path;
@@ -101,3 +118,7 @@ export function cleanPageRef(pageRef: string) {
return pageRef;
}
}
export function folderName(path: string) {
return path.split("/").slice(0, -1).join("/");
}