Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
Merge remote-tracking branch 'origin/main' into feature/main_improvements_v2
Elian Doran6e2cd28eea2c
diff --cc apps/client/src/widgets/type_widgets/code/Markdown.tsxindex 29273ec44f,536e046774..a147561275--- a/apps/client/src/widgets/type_widgets/code/Markdown.tsx+++ b/apps/client/src/widgets/type_widgets/code/Markdown.tsx@@@ -1,15 -1,11 +1,14 @@@import "./Markdown.css";+import { autocompletion } from "@codemirror/autocomplete";+import { syntaxTree } from "@codemirror/language";- import { StateEffect } from "@codemirror/state";+import type { SyntaxNode } from "@lezer/common";import VanillaCodeMirror from "@triliumnext/codemirror";import { CustomMarkdownRenderer, renderToHtml } from "@triliumnext/commons";import DOMPurify from "dompurify";import { Marked, type Tokens } from "marked";import { createContext } from "preact";- import { useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";-import { useCallback, useContext, useEffect, useMemo, useState } from "preact/hooks";++import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";import appContext from "../../../components/app_context";import NoteContext from "../../../components/note_context";@@@ -471,125 -461,136 +484,125 @@@ function useSlashCommands(parentComponeuseEffect(() => {if (!editorView) return;- import("@codemirror/autocomplete").then(({ autocompletion }) => {- const ext = autocompletion({- override: [(ctx) => {- const match = ctx.matchBefore(/(?:^|(?<=\s))\/\w*/);- if (!match) return null;-- // Suppress inside fenced code blocks (``` ... ```).- const textBefore = ctx.state.sliceDoc(0, match.from);- const fenceCount = (textBefore.match(/^(`{3,}|~{3,})/gm) ?? []).length;- if (fenceCount % 2 !== 0) return null;-- return {- from: match.from,- options: [- {- label: "/date",- detail: "Insert current date and time",- apply(view, _completion, from, to) {- view.dispatch({ changes: { from, to } });- parentComponent?.triggerCommand("insertDateTimeToText");- }- },- {- label: "/include",- detail: "Include another note",- apply(view, _completion, from, to) {- view.dispatch({ changes: { from, to } });- parentComponent?.triggerCommand("addIncludeNoteToText");- }- },- {- label: "/link",- detail: "Insert a note link",- apply(view, _completion, from, to) {- view.dispatch({ changes: { from, to } });- parentComponent?.triggerCommand("addLinkToText");- }- },- {- label: "/math",- detail: "Insert a math equation block",- apply(view, _completion, from, to) {- const placeholder = "\\text{equation}";- const template = `$$\n${placeholder}\n$$`;- view.dispatch({- changes: { from, to, insert: template },- selection: { anchor: from + 3, head: from + 3 + placeholder.length }- });- }- },- {- label: "/footnote",- detail: "Insert a footnote",- apply(view, _completion, from, to) {- const doc = view.state.doc.toString();- let maxFootnote = 0;- for (const m of doc.matchAll(/\[\^(\d+)\]/g)) {- maxFootnote = Math.max(maxFootnote, parseInt(m[1], 10));- }- const n = maxFootnote + 1;- const ref = `[^${n}]`;- const def = `\n\n[^${n}]: `;- const docEnd = view.state.doc.length;- const newDocEnd = docEnd - (to - from) + ref.length + def.length;- view.dispatch({- changes: [- { from, to, insert: ref },- { from: docEnd, insert: def }- ],- selection: { anchor: newDocEnd }- });- }- },- {- label: "/mermaid",- detail: "Insert a Mermaid diagram",- apply(view, _completion, from, to) {- const placeholder = "graph TD\n A --> B";- const template = `\`\`\`mermaid\n${placeholder}\n\`\`\``;- view.dispatch({- changes: { from, to, insert: template },- selection: { anchor: from + 11, head: from + 11 + placeholder.length }- });- }- },- {- label: "/image",- detail: "Upload an image attachment",- apply(view, _completion, from, to) {- view.dispatch({ changes: { from, to } });-- const input = document.createElement("input");- input.type = "file";- input.accept = "image/*";- input.onchange = async () => {- const file = input.files?.[0];- if (!file) return;-- const result = await server.upload(- `notes/${note.noteId}/attachments/upload`,- file, undefined, "POST"- ) as { uploaded: boolean; url?: string };- if (!result?.uploaded || !result.url) return;-- insertText(editorView, ``);- editorView.focus();- };- input.click();- }- },- ...["note", "tip", "important", "caution", "warning"].map((admonitionType) => ({- label: `/${admonitionType}`,- detail: `Insert ${admonitionType} admonition`,- apply(view: import("@codemirror/view").EditorView, _c: unknown, from: number, to: number) {- const template = `> [!${admonitionType.toUpperCase()}]\n> `;- view.dispatch({- changes: { from, to, insert: template },- selection: { anchor: from + template.length }- });- }- }))- ]- };- }],- activateOnTyping: true- });+ const ext = autocompletion({+ override: [(ctx) => {+ const match = ctx.matchBefore(/(?:^|(?<=\s))\/\w*/);+ if (!match) return null;- editorView.setNamedExtension("slashCommands", ext);+ // Suppress slash menu inside fenced/indented code blocks and inline code spans —+ // a leading `/` there is part of the code, not a command trigger.+ for (let node: SyntaxNode | null = syntaxTree(ctx.state).resolveInner(ctx.pos, -1); node; node = node.parent) {+ if (node.name.includes("Code")) return null;+ }++ return {+ from: match.from,+ options: [+ {+ label: "/date",+ detail: t("markdown_slash_commands.date"),+ apply(view, _completion, from, to) {+ view.dispatch({ changes: { from, to } });+ parentRef.current?.triggerCommand("insertDateTimeToText");+ }+ },+ {+ label: "/include",+ detail: t("markdown_slash_commands.include"),+ apply(view, _completion, from, to) {+ view.dispatch({ changes: { from, to } });+ parentRef.current?.triggerCommand("addIncludeNoteToText");+ }+ },+ {+ label: "/image",+ detail: t("markdown_slash_commands.image"),+ apply(view, _completion, from, to) {+ view.dispatch({ changes: { from, to } });+ const input = document.createElement("input");+ input.type = "file";+ input.accept = "image/*";+ input.addEventListener("change", () => {+ const file = input.files?.[0];+ if (file) uploadImageAndInsert(editorView, noteRef.current, file);+ });+ input.click();+ }+ },+ {+ label: "/link",+ detail: t("markdown_slash_commands.link"),+ apply(view, _completion, from, to) {+ view.dispatch({ changes: { from, to } });+ parentRef.current?.triggerCommand("addLinkToText");+ }+ },+ {+ label: "/math",+ detail: t("markdown_slash_commands.math"),+ apply(view, _completion, from, to) {+ const placeholder = "\\text{equation}";+ const template = `$$\n${placeholder}\n$$`;+ view.dispatch({+ changes: { from, to, insert: template },+ selection: { anchor: from + 3, head: from + 3 + placeholder.length }+ });+ }+ },+ {+ label: "/footnote",+ detail: t("markdown_slash_commands.footnote"),+ apply(view, _completion, from, to) {+ const doc = view.state.doc.toString();+ let maxFootnote = 0;+ for (const m of doc.matchAll(/\[\^(\d+)\]/g)) {+ maxFootnote = Math.max(maxFootnote, parseInt(m[1], 10));+ }+ const n = maxFootnote + 1;+ const ref = `[^${n}]`;+ const def = `\n\n[^${n}]: `;+ const docEnd = view.state.doc.length;+ const newDocEnd = docEnd - (to - from) + ref.length + def.length;+ view.dispatch({+ changes: [+ { from, to, insert: ref },+ { from: docEnd, insert: def }+ ],+ selection: { anchor: newDocEnd }+ });+ }+ },+ {+ label: "/mermaid",+ detail: t("markdown_slash_commands.mermaid"),+ apply(view, _completion, from, to) {+ const placeholder = "graph TD\n A --> B";+ const template = `\`\`\`mermaid\n${placeholder}\n\`\`\``;+ view.dispatch({+ changes: { from, to, insert: template },+ selection: { anchor: from + 11, head: from + 11 + placeholder.length }+ });+ }+ },+ ...["note", "tip", "important", "caution", "warning"].map((admonitionType) => ({+ label: `/${admonitionType}`,+ detail: t("markdown_slash_commands.admonition", { type: admonitionType }),+ apply(view: import("@codemirror/view").EditorView, _c: unknown, from: number, to: number) {+ const template = `> [!${admonitionType.toUpperCase()}]\n> `;+ view.dispatch({+ changes: { from, to, insert: template },+ selection: { anchor: from + template.length }+ });+ }+ }))+ ]+ };+ }],+ activateOnTyping: true});- }, [editorView, parentComponent, note.noteId]);+- editorView.dispatch({ effects: StateEffect.appendConfig.of(ext) });++ editorView.setNamedExtension("slashCommands", ext);+ }, [editorView]);}//#endregion@@@ -614,9 -625,9 +627,9 @@@ function useImageDrop(note: FNote, edite.preventDefault();e.stopPropagation();- const dropPos = editorView!.posAtCoords({ x: e.clientX, y: e.clientY }) ?? undefined;+ const dropPos = editorView.posAtCoords({ x: e.clientX, y: e.clientY }) ?? undefined;for (const file of imageFiles) {- uploadAndInsert(file, dropPos);+ uploadImageAndInsert(editorView!, note, file, dropPos);}}