silverbullet/plug-api/lib/resolve.test.ts

112 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-07-30 17:30:01 +08:00
import {
cleanPageRef,
2023-07-30 17:30:01 +08:00
federatedPathToUrl,
2023-12-20 00:55:11 +08:00
resolveAttachmentPath,
2023-07-30 17:30:01 +08:00
resolvePath,
rewritePageRefs,
} from "$sb/lib/resolve.ts";
import { assertEquals } from "$lib/test_deps.ts";
import { ParseTree, renderToText } from "$lib/tree.ts";
import { parse } from "$common/markdown_parser/parse_tree.ts";
import { extendedMarkdownLanguage } from "$common/markdown_parser/parser.ts";
2023-07-30 05:41:37 +08:00
Deno.test("Test URL resolver", () => {
assertEquals(resolvePath("test", "some page"), "some page");
assertEquals(
resolvePath("!silverbullet.md", "some page"),
"!silverbullet.md/some page",
);
assertEquals(
resolvePath("!silverbullet.md/some/deep/path", "some page"),
"!silverbullet.md/some page",
);
assertEquals(resolvePath("!bla/bla", "!bla/bla2"), "!bla/bla2");
assertEquals(
resolvePath("!silverbullet.md", "test/image.png", true),
"https://silverbullet.md/test/image.png",
);
2023-07-30 17:30:01 +08:00
assertEquals(
resolvePath("!silverbullet.md", "bla@123"),
"!silverbullet.md/bla@123",
);
assertEquals(resolvePath("somewhere", "bla@123"), "bla@123");
assertEquals(
federatedPathToUrl("!silverbullet.md"),
"https://silverbullet.md",
);
assertEquals(
federatedPathToUrl("!silverbullet.md/index"),
"https://silverbullet.md/index",
);
assertEquals(cleanPageRef("hello"), "hello");
assertEquals(cleanPageRef("[[hello]]"), "hello");
2023-07-30 17:30:01 +08:00
});
Deno.test("Test rewritePageRefs", () => {
let tree = parseMarkdown(`
This is a [[local link]] and [[local link|with alias]].
\`\`\`query
page render [[template/page]]
\`\`\`
2023-07-30 17:30:01 +08:00
\`\`\`template
page: "[[template/use-template]]"
\`\`\`
2023-07-30 17:30:01 +08:00
`);
rewritePageRefs(tree, "!silverbullet.md");
let rewrittenText = renderToText(tree);
assertEquals(
rewrittenText,
`
This is a [[!silverbullet.md/local link]] and [[!silverbullet.md/local link|with alias]].
\`\`\`query
page render [[!silverbullet.md/template/page]]
\`\`\`
2023-07-30 17:30:01 +08:00
\`\`\`template
page: "[[!silverbullet.md/template/use-template]]"
\`\`\`
2023-07-30 17:30:01 +08:00
`,
);
tree = parseMarkdown(
`This is a [[local link]] and [[local link|with alias]].`,
);
// Now test the default case without federated links
rewritePageRefs(tree, "index");
rewrittenText = renderToText(tree);
assertEquals(
rewrittenText,
`This is a [[local link]] and [[local link|with alias]].`,
);
2023-12-20 00:55:11 +08:00
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"),
);
2023-07-30 05:41:37 +08:00
});
function parseMarkdown(text: string): ParseTree {
return parse(extendedMarkdownLanguage, text);
}