silverbullet/web/cm_plugins/wiki_link.ts

117 lines
3.7 KiB
TypeScript
Raw Normal View History

import { pageLinkRegex } from "../../common/markdown_parser/parser.ts";
import { ClickEvent } from "../../plug-api/app_event.ts";
import { Decoration, syntaxTree } from "../deps.ts";
2023-07-14 22:56:20 +08:00
import { Client } from "../client.ts";
import {
decoratorStateField,
invisibleDecoration,
isCursorInRange,
LinkWidget,
} from "./util.ts";
2023-07-30 05:41:37 +08:00
import { resolvePath } from "$sb/lib/resolve.ts";
2024-01-24 21:44:39 +08:00
import { encodePageRef, parsePageRef } from "$sb/lib/page.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];
syntaxTree(state).iterate({
enter: ({ type, from, to }) => {
if (type.name !== "WikiLink") {
return;
}
const text = state.sliceDoc(from, to);
const match = pageLinkRegex.exec(text);
if (!match) return;
const [_fullMatch, page, pipePart, alias] = match;
2022-11-30 18:26:47 +08:00
2023-12-22 22:55:50 +08:00
let pageExists = !client.fullSyncCompleted;
2024-01-24 21:44:39 +08:00
const pageRef = parsePageRef(page);
pageRef.page = resolvePath(client.currentPage, pageRef.page);
const lowerCasePageName = pageRef.page.toLowerCase();
for (const pageName of client.clientSystem.allKnownPages) {
2023-12-22 22:55:50 +08:00
if (pageName.toLowerCase() === lowerCasePageName) {
pageExists = 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
pageExists = true;
}
2022-11-30 18:26:47 +08:00
if (isCursorInRange(state, [from, to])) {
// Only attach a CSS class, then get out
if (!pageExists) {
2022-11-27 15:48:01 +08:00
widgets.push(
Decoration.mark({
class: "sb-wiki-link-page-missing",
}).range(from + 2, from + page.length + 2),
2022-11-27 15:48:01 +08:00
);
}
return;
}
// Hide the whole thing
widgets.push(
invisibleDecoration.range(
from,
to,
),
);
let linkText = alias || page;
if (!pipePart && text.indexOf("/") !== -1) {
// Let's use the last part of the path as the link text
linkText = page.split("/").pop()!;
}
// And replace it with a widget
widgets.push(
Decoration.widget({
widget: new LinkWidget(
{
text: linkText,
title: pageExists
2024-01-24 21:44:39 +08:00
? `Navigate to ${encodePageRef(pageRef)}`
: `Create ${pageRef.page}`,
href: `/${encodePageRef(pageRef)}`,
cssClass: pageExists
? "sb-wiki-link-page"
: "sb-wiki-link-page-missing",
callback: (e) => {
if (e.altKey) {
// Move cursor into the link
2023-12-22 22:55:50 +08:00
return client.editorView.dispatch({
selection: { anchor: from + 2 },
});
}
// 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,
);
},
},
),
}).range(from),
);
},
});
return Decoration.set(widgets, true);
});
}