Skip to content

Build your personal knowledge base with Trilium Notes

feat(markdown): support include note

Elian Doran583022547c66
diff --git a/apps/client/src/widgets/dialogs/include_note.tsx b/apps/client/src/widgets/dialogs/include_note.tsx
index 74d41da6be..09c4bbef39 100644
--- a/apps/client/src/widgets/dialogs/include_note.tsx
+++ b/apps/client/src/widgets/dialogs/include_note.tsx
@@ -12,11 +12,11 @@ import { useTriliumEvent, useTriliumOption } from "../react/hooks";
import { type BoxSize, CKEditorApi } from "../type_widgets/text/CKEditorWithWatchdog";
export interface IncludeNoteOpts {
- editorApi: CKEditorApi;
+ editorApi: Pick<CKEditorApi, "addIncludeNote" | "addImage">;
}
export default function IncludeNoteDialog() {
- const editorApiRef = useRef<CKEditorApi>(null);
+ const editorApiRef = useRef<Pick<CKEditorApi, "addIncludeNote" | "addImage">>(null);
const [suggestion, setSuggestion] = useState<Suggestion | null>(null);
const [defaultBoxSize, setDefaultBoxSize] = useTriliumOption("includeNoteDefaultBoxSize");
const [boxSize, setBoxSize] = useState<string>(defaultBoxSize);
@@ -77,7 +77,7 @@ export default function IncludeNoteDialog() {
)
}
-async function includeNote(notePath: string, editorApi: CKEditorApi, boxSize: BoxSize) {
+async function includeNote(notePath: string, editorApi: Pick<CKEditorApi, "addIncludeNote" | "addImage">, boxSize: BoxSize) {
const noteId = tree.getNoteIdFromUrl(notePath);
if (!noteId) {
return;
diff --git a/apps/client/src/widgets/type_widgets/code/Markdown.tsx b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
index 04730cf4d6..4f56f0a6d4 100644
--- a/apps/client/src/widgets/type_widgets/code/Markdown.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
@@ -6,8 +6,10 @@ import DOMPurify from "dompurify";
import { Marked, type Tokens } from "marked";
import { createContext } from "preact";
import { useContext, useEffect, useMemo, useState } from "preact/hooks";
+
import NoteContext from "../../../components/note_context";
import FNote from "../../../entities/fnote";
+import froca from "../../../services/froca";
import keyboard_actions from "../../../services/keyboard_actions";
import options from "../../../services/options";
import server from "../../../services/server";
@@ -280,6 +282,34 @@ function useTextCommands(parentComponent: TypeWidgetProps["parentComponent"], ed
changes: { from: pos, insert: dateString },
selection: { anchor: pos + dateString.length }
});
+ },
+
+ addIncludeNoteToTextCommand() {
+ if (!editorView) return;
+
+ function insertAtCursor(text: string) {
+ const pos = editorView!.state.selection.main.head;
+ editorView!.dispatch({
+ changes: { from: pos, insert: text },
+ selection: { anchor: pos + text.length }
+ });
+ editorView!.focus();
+ }
+
+ parentComponent?.triggerCommand("showIncludeNoteDialog", {
+ // Only addIncludeNote and addImage are used by the dialog.
+ editorApi: {
+ addIncludeNote(noteId: string, boxSize?: string) {
+ insertAtCursor(`<section class="include-note" data-note-id="${noteId}" data-box-size="${boxSize ?? "full"}"></section>\n`);
+ },
+ async addImage(noteId: string) {
+ const note = await froca.getNote(noteId);
+ if (!note) return;
+ const encodedTitle = encodeURIComponent(note.title);
+ insertAtCursor(`![${note.title}](api/images/${noteId}/${encodedTitle})\n`);
+ }
+ }
+ });
}
});
}