silverbullet/plugs/editor/navigate.ts

175 lines
4.8 KiB
TypeScript
Raw Normal View History

2024-02-29 22:23:05 +08:00
import type { ClickEvent } from "../../plug-api/types.ts";
import { extractHashtag } from "../../plug-api/lib/tags.ts";
import {
editor,
markdown,
system,
} from "@silverbulletmd/silverbullet/syscalls";
import {
addParentPointers,
2022-12-29 19:53:42 +08:00
findNodeOfType,
findParentMatching,
nodeAtPos,
2024-07-30 23:33:33 +08:00
type ParseTree,
} from "@silverbulletmd/silverbullet/lib/tree";
import {
isLocalPath,
resolvePath,
} from "@silverbulletmd/silverbullet/lib/resolve";
import {
looksLikePathWithExtension,
parsePageRef,
} from "@silverbulletmd/silverbullet/lib/page_ref";
2024-01-25 17:17:42 +08:00
import { tagPrefix } from "../index/constants.ts";
2022-03-29 23:02:28 +08:00
async function actionClickOrActionEnter(
mdTree: ParseTree | null,
inNewWindow = false,
) {
if (!mdTree) {
return;
}
const navigationNodeFinder = (t: ParseTree) =>
2022-12-19 20:42:20 +08:00
[
"WikiLink",
"Link",
"Image",
"URL",
"NakedURL",
"Link",
"CommandLink",
"PageRef",
2024-01-25 17:17:42 +08:00
"Hashtag",
2022-12-19 20:42:20 +08:00
]
2022-12-19 20:16:22 +08:00
.includes(
t.type!,
);
if (!navigationNodeFinder(mdTree)) {
mdTree = findParentMatching(mdTree, navigationNodeFinder);
if (!mdTree) {
return;
}
}
const currentPage = await editor.getCurrentPage();
switch (mdTree.type) {
case "WikiLink": {
2024-05-28 02:33:41 +08:00
const link = mdTree.children![1]!.children![0].text!;
// Assume is attachment if it has extension
if (looksLikePathWithExtension(link)) {
2024-05-28 02:33:41 +08:00
const attachmentPath = resolvePath(
currentPage,
"/" + decodeURI(link),
);
return editor.openUrl(attachmentPath);
} else {
const pageRef = parsePageRef(link);
pageRef.page = resolvePath(currentPage, "/" + pageRef.page);
if (!pageRef.page) {
pageRef.page = currentPage;
}
// This is an explicit navigate, move to the top
if (pageRef.pos === undefined) {
pageRef.pos = 0;
}
2024-07-03 02:13:41 +08:00
return editor.navigate(pageRef, false, inNewWindow);
2022-08-30 16:44:20 +08:00
}
}
2022-12-19 20:42:20 +08:00
case "PageRef": {
const pageName = parsePageRef(mdTree.children![0].text!).page;
2024-05-28 02:33:41 +08:00
return editor.navigate({ page: pageName, pos: 0 }, false, inNewWindow);
2022-12-19 20:42:20 +08:00
}
case "NakedURL":
2024-06-07 14:31:29 +08:00
case "URL":
2024-05-28 02:33:41 +08:00
return editor.openUrl(mdTree.children![0].text!);
2022-12-19 20:16:22 +08:00
case "Image":
case "Link": {
2022-12-29 19:53:42 +08:00
const urlNode = findNodeOfType(mdTree, "URL");
if (!urlNode) {
return;
}
const url = urlNode.children![0].text!;
2022-08-29 21:40:38 +08:00
if (url.length <= 1) {
2022-10-14 21:11:33 +08:00
return editor.flashNotification("Empty link, ignoring", "error");
2022-08-29 21:40:38 +08:00
}
2024-05-28 02:33:41 +08:00
if (isLocalPath(url)) {
if (/\.[a-zA-Z0-9>]+$/.test(url)) {
return editor.openUrl(
resolvePath(currentPage, decodeURI(url)),
);
} else {
return editor.navigate(
parsePageRef(resolvePath(currentPage, decodeURI(url))),
2024-07-03 02:13:41 +08:00
false,
inNewWindow,
2024-05-28 02:33:41 +08:00
);
}
} else {
2024-05-28 02:33:41 +08:00
return editor.openUrl(url);
}
}
case "CommandLink": {
const commandName = mdTree.children![1]!.children![0].text!;
const argsNode = findNodeOfType(mdTree, "CommandLinkArgs");
2024-07-30 21:17:34 +08:00
const argsText = argsNode?.children?.[0]?.text;
// Assume the arguments are can be parsed as the innards of a valid JSON list
try {
const args = argsText ? JSON.parse(`[${argsText}]`) : [];
await system.invokeCommand(commandName, args);
2023-12-20 00:55:11 +08:00
} catch (e: any) {
await editor.flashNotification(
`Error parsing command link arguments: ${e.message}`,
"error",
);
}
break;
}
2024-01-25 17:17:42 +08:00
case "Hashtag": {
const hashtag = extractHashtag(mdTree.children![0].text!);
2024-01-25 17:17:42 +08:00
await editor.navigate(
{ page: `${tagPrefix}${hashtag}`, pos: 0 },
false,
inNewWindow,
);
break;
}
2022-02-27 00:50:50 +08:00
}
}
export async function linkNavigate() {
2022-10-14 21:11:33 +08:00
const mdTree = await markdown.parseMarkdown(await editor.getText());
const newNode = nodeAtPos(mdTree, await editor.getCursor());
addParentPointers(mdTree);
await actionClickOrActionEnter(newNode);
}
2022-02-27 00:50:50 +08:00
export async function clickNavigate(event: ClickEvent) {
// Navigate by default, don't navigate when Alt is held
if (event.altKey) {
2022-04-12 02:34:09 +08:00
return;
2022-02-27 00:50:50 +08:00
}
2022-10-14 21:11:33 +08:00
const mdTree = await markdown.parseMarkdown(await editor.getText());
addParentPointers(mdTree);
const newNode = nodeAtPos(mdTree, event.pos);
await actionClickOrActionEnter(newNode, event.ctrlKey || event.metaKey);
2022-02-27 00:50:50 +08:00
}
export async function navigateCommand(cmdDef: any) {
await editor.navigate({ page: cmdDef.page, pos: 0 });
}
2024-01-25 18:42:36 +08:00
export async function navigateToPage(_cmdDef: any, pageName: string) {
await editor.navigate({ page: pageName, pos: 0 });
}
export async function navigateToURL(_cmdDef: any, url: string) {
await editor.openUrl(url, false);
}
export async function navigateBack() {
await editor.goHistory(-1);
}
export async function navigateForward() {
await editor.goHistory(1);
}