silverbullet/web/cm_plugins/wiki_link.ts

117 lines
3.9 KiB
TypeScript
Raw Normal View History

2024-05-28 02:33:41 +08:00
import { ClickEvent } from "$sb/types.ts";
import { syntaxTree } from "@codemirror/language";
import { Decoration } from "@codemirror/view";
2023-07-14 22:56:20 +08:00
import { Client } from "../client.ts";
2024-05-28 02:33:41 +08:00
import { decoratorStateField, isCursorInRange, LinkWidget } from "./util.ts";
2023-07-30 05:41:37 +08:00
import { resolvePath } from "$sb/lib/resolve.ts";
2024-05-28 02:33:41 +08:00
import { encodePageRef, parsePageRef } from "$sb/lib/page_ref.ts";
/**
* Plugin to hide path prefix when the cursor is not inside.
*/
2023-12-22 22:55:50 +08:00
export function cleanWikiLinkPlugin(client: Client) {
return decoratorStateField((state) => {
const widgets: any[] = [];
// let parentRange: [number, number];
2024-05-28 02:33:41 +08:00
const allKnownFiles = client.clientSystem.allKnownFiles;
syntaxTree(state).iterate({
enter: ({ type, from, to }) => {
if (type.name !== "WikiLink") {
return;
}
const text = state.sliceDoc(from, to);
2024-05-28 02:33:41 +08:00
const match = /(!?\[\[)([^\]\|]+)(?:\|([^\]]+))?(\]\])/g.exec(text);
if (!match) return;
2024-05-28 02:33:41 +08:00
const [_fullMatch, firstMark, url, alias, lastMark] = match;
if (firstMark.startsWith("!")) {
// Is inline image
return;
}
let fileExists = !client.fullSyncCompleted;
2022-11-30 18:26:47 +08:00
2024-05-28 02:33:41 +08:00
const pageRef = parsePageRef(url);
pageRef.page = resolvePath(client.currentPage, "/" + pageRef.page);
2024-01-24 21:44:39 +08:00
const lowerCasePageName = pageRef.page.toLowerCase();
2024-05-28 02:33:41 +08:00
for (const fileName of allKnownFiles) {
if (
fileName.toLowerCase().replace(/\.md$/, "") === lowerCasePageName
) {
fileExists = true;
break;
}
}
if (
2024-01-24 21:44:39 +08:00
pageRef.page === "" ||
client.plugSpaceRemotePrimitives.isLikelyHandled(pageRef.page)
) {
// Empty page name with local @anchor use or a link to a page that dynamically generated by a plug
2024-05-28 02:33:41 +08:00
fileExists = true;
}
2022-11-30 18:26:47 +08:00
if (isCursorInRange(state, [from, to])) {
// Only attach a CSS class, then get out
2024-05-28 02:33:41 +08:00
if (!fileExists) {
2022-11-27 15:48:01 +08:00
widgets.push(
Decoration.mark({
class: "sb-wiki-link-page-missing",
2024-05-28 02:33:41 +08:00
}).range(
from + firstMark.length,
to - lastMark.length,
),
2022-11-27 15:48:01 +08:00
);
}
return;
}
const pageMeta = client.ui.viewState.allPages.find(p => p.name == url);
2024-05-28 02:33:41 +08:00
const linkText = alias ||
(pageMeta?.pageDecorations.prefix ?? "") + (url.includes("/") ? url.split("/").pop()! : url);
// And replace it with a widget
widgets.push(
2024-05-28 02:33:41 +08:00
Decoration.replace({
widget: new LinkWidget(
{
text: linkText,
2024-05-28 02:33:41 +08:00
title: fileExists
2024-01-24 21:44:39 +08:00
? `Navigate to ${encodePageRef(pageRef)}`
: `Create ${pageRef.page}`,
href: `/${encodePageRef(pageRef)}`,
2024-05-28 02:33:41 +08:00
cssClass: fileExists
? "sb-wiki-link-page"
: "sb-wiki-link-page-missing",
from,
callback: (e) => {
if (e.altKey) {
// Move cursor into the link
client.editorView.dispatch({
2024-05-28 02:33:41 +08:00
selection: { anchor: from + firstMark.length },
});
client.focus();
return;
}
// Dispatch click event to navigate there without moving the cursor
const clickEvent: ClickEvent = {
2024-01-24 21:44:39 +08:00
page: client.currentPage,
ctrlKey: e.ctrlKey,
metaKey: e.metaKey,
altKey: e.altKey,
pos: from,
};
2023-12-22 22:55:50 +08:00
client.dispatchAppEvent("page:click", clickEvent).catch(
console.error,
);
},
},
),
2024-05-28 02:33:41 +08:00
}).range(from, to),
);
},
});
return Decoration.set(widgets, true);
});
}