Inline links and Cmd-click and Cmd-enter now work

pull/3/head
Zef Hemel 2022-02-28 14:58:18 +01:00
parent ad37b9ed10
commit 5cd5feeafa
5 changed files with 59 additions and 14 deletions

View File

@ -1,22 +1,35 @@
import { ClickEvent } from "../../webapp/src/app_event.ts"; import { ClickEvent } from "../../webapp/src/app_event.ts";
import { syscall } from "./lib/syscall.ts"; import { syscall } from "./lib/syscall.ts";
export async function linkNavigate() { async function navigate(syntaxNode: any) {
let syntaxNode = await syscall("editor.getSyntaxNodeUnderCursor"); if (!syntaxNode) {
if (syntaxNode && syntaxNode.name === "WikiLinkPage") { return;
await syscall("editor.navigate", syntaxNode.text); }
console.log("Attempting to navigate based on syntax node", syntaxNode);
switch (syntaxNode.name) {
case "WikiLinkPage":
await syscall("editor.navigate", syntaxNode.text);
break;
case "URL":
await syscall("editor.openUrl", syntaxNode.text);
break;
case "Link":
// Markdown link: [bla](URLHERE) needs extraction
let match = /\[[^\\]+\]\(([^\)]+)\)/.exec(syntaxNode.text);
if (match) {
await syscall("editor.openUrl", match[1]);
}
} }
} }
export async function clickNavigate(event: ClickEvent) { export async function linkNavigate() {
let syntaxNode = await syscall("editor.getSyntaxNodeAtPos", event.pos); navigate(await syscall("editor.getSyntaxNodeUnderCursor"));
}
export async function clickNavigate(event: ClickEvent) {
if (event.ctrlKey || event.metaKey) { if (event.ctrlKey || event.metaKey) {
console.log("Here", syntaxNode); let syntaxNode = await syscall("editor.getSyntaxNodeAtPos", event.pos);
if (syntaxNode && syntaxNode.name === "WikiLinkPage") { navigate(syntaxNode);
await syscall("editor.navigate", syntaxNode.text);
return;
}
} }
} }

View File

@ -5,7 +5,6 @@ import { syscall } from "./lib/syscall.ts";
const wikilinkRegex = new RegExp(pageLinkRegex, "g"); const wikilinkRegex = new RegExp(pageLinkRegex, "g");
export async function indexLinks({ name, text }: IndexEvent) { export async function indexLinks({ name, text }: IndexEvent) {
console.log("Now indexing", name);
let backLinks: { key: string; value: string }[] = []; let backLinks: { key: string; value: string }[] = [];
for (let match of text.matchAll(wikilinkRegex)) { for (let match of text.matchAll(wikilinkRegex)) {
let toPage = match[1]; let toPage = match[1];

View File

@ -1,4 +1,4 @@
import { styleTags } from "@codemirror/highlight"; import { styleTags, tags as t } from "@codemirror/highlight";
import { MarkdownConfig, TaskList } from "@lezer/markdown"; import { MarkdownConfig, TaskList } from "@lezer/markdown";
import { commonmark, mkLang } from "./markdown/markdown"; import { commonmark, mkLang } from "./markdown/markdown";
import * as ct from "./customtags"; import * as ct from "./customtags";
@ -53,8 +53,32 @@ const AtMention: MarkdownConfig = {
}, },
], ],
}; };
const urlRegexp =
/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/;
const UnmarkedUrl: MarkdownConfig = {
defineNodes: ["URL"],
parseInline: [
{
name: "URL",
parse(cx, next, pos) {
let match: RegExpMatchArray | null;
if (
next != 104 /* 'h' */ ||
!(match = urlRegexp.exec(cx.slice(pos, cx.end)))
) {
return -1;
}
return cx.addElement(cx.elt("URL", pos, pos + match[0].length));
},
after: "Emphasis",
},
],
};
const TagLink: MarkdownConfig = { const TagLink: MarkdownConfig = {
defineNodes: ["Checkbox"], defineNodes: ["TagLink"],
parseInline: [ parseInline: [
{ {
name: "TagLink", name: "TagLink",
@ -77,6 +101,7 @@ const WikiMarkdown = commonmark.configure([
AtMention, AtMention,
TagLink, TagLink,
TaskList, TaskList,
UnmarkedUrl,
{ {
props: [ props: [
styleTags({ styleTags({
@ -86,6 +111,7 @@ const WikiMarkdown = commonmark.configure([
TagLink: ct.TagTag, TagLink: ct.TagTag,
Task: ct.TaskTag, Task: ct.TaskTag,
TaskMarker: ct.TaskMarkerTag, TaskMarker: ct.TaskMarkerTag,
Url: t.url,
}), }),
], ],
}, },

View File

@ -138,6 +138,10 @@ body {
.cm-editor .link.url { .cm-editor .link.url {
color: #7e7d7d; color: #7e7d7d;
} }
.cm-editor .url:not(.link) {
color: #0330cb;
text-decoration: underline;
}
.cm-editor .wiki-link-page { .cm-editor .wiki-link-page {
color: #0330cb; color: #0330cb;

View File

@ -39,6 +39,9 @@ export default (editor: Editor) => ({
"editor.navigate": async (name: string) => { "editor.navigate": async (name: string) => {
await editor.navigate(name); await editor.navigate(name);
}, },
"editor.openUrl": async (url: string) => {
window.open(url, "_blank")!.focus();
},
"editor.insertAtPos": (text: string, pos: number) => { "editor.insertAtPos": (text: string, pos: number) => {
editor.editorView!.dispatch({ editor.editorView!.dispatch({
changes: { changes: {