Skip to content

Build your personal knowledge base with Trilium Notes

feat(markdown): support internal & image links

Elian Doranb56f5661b962
diff --git a/apps/server/src/services/notes.spec.ts b/apps/server/src/services/notes.spec.ts
index 64d949b159..8d7a229be9 100644
--- a/apps/server/src/services/notes.spec.ts
+++ b/apps/server/src/services/notes.spec.ts
@@ -3,7 +3,8 @@ import becca from "../becca/becca.js";
import BAttachment from "../becca/entities/battachment.js";
import { buildNote } from "../test/becca_easy_mocking.js";
import { randomString } from "./utils.js";
-import { checkImageAttachments, findBookmarks } from "./notes.js";
+import BAttribute from "../becca/entities/battribute.js";
+import { checkImageAttachments, findBookmarks, saveLinks } from "./notes.js";
vi.mock("./sql.js", () => ({
default: {
@@ -244,3 +245,85 @@ describe("checkImageAttachments", () => {
});
});
});
+
+describe("saveLinks", () => {
+ beforeEach(() => {
+ becca.reset();
+ });
+
+ function makeLinkRelation(noteId: string, name: string, targetNoteId: string) {
+ const attr = new BAttribute({
+ attributeId: randomString(10),
+ noteId,
+ type: "relation",
+ name,
+ value: targetNoteId
+ });
+ attr.markAsDeleted = vi.fn();
+ return attr;
+ }
+
+ it("does not delete existing imageLink relations on markdown notes that reference images", () => {
+ const note = buildNote({ title: "Test", type: "code", mime: "text/x-markdown" });
+ const targetNote = buildNote({ title: "Image Note", type: "image" });
+ becca.notes[targetNote.noteId] = targetNote;
+
+ const imageLink = makeLinkRelation(note.noteId, "imageLink", targetNote.noteId);
+ note.getRelations = () => [imageLink];
+ note.getAttachments = () => [];
+
+ const content = `![diagram](api/images/${targetNote.noteId}/diagram.png)`;
+ saveLinks(note, content);
+
+ expect(imageLink.markAsDeleted).not.toHaveBeenCalled();
+ });
+
+ it("does not delete existing internalLink relations on markdown notes using #root links", () => {
+ const note = buildNote({ title: "Test", type: "code", mime: "text/x-markdown" });
+ const targetNote = buildNote({ title: "Other Note" });
+ becca.notes[targetNote.noteId] = targetNote;
+
+ const internalLink = makeLinkRelation(note.noteId, "internalLink", targetNote.noteId);
+ note.getRelations = () => [internalLink];
+ note.getAttachments = () => [];
+
+ const content = `See [Other Note](#root/${targetNote.noteId})`;
+ saveLinks(note, content);
+
+ expect(internalLink.markAsDeleted).not.toHaveBeenCalled();
+ });
+
+ it("does not delete existing internalLink relations on markdown notes using wiki-links", () => {
+ const note = buildNote({ title: "Test", type: "code", mime: "text/x-markdown" });
+ const targetNote = buildNote({ title: "Linked Note" });
+ becca.notes[targetNote.noteId] = targetNote;
+
+ const internalLink = makeLinkRelation(note.noteId, "internalLink", targetNote.noteId);
+ note.getRelations = () => [internalLink];
+ note.getAttachments = () => [];
+
+ const content = `See [[${targetNote.noteId}]] for details.`;
+ saveLinks(note, content);
+
+ expect(internalLink.markAsDeleted).not.toHaveBeenCalled();
+ });
+
+ it("detects both wiki-links and #root links in the same content", () => {
+ const note = buildNote({ title: "Test", type: "code", mime: "text/x-markdown" });
+ const targetA = buildNote({ title: "Note A" });
+ const targetB = buildNote({ title: "Note B" });
+ becca.notes[targetA.noteId] = targetA;
+ becca.notes[targetB.noteId] = targetB;
+
+ const linkA = makeLinkRelation(note.noteId, "internalLink", targetA.noteId);
+ const linkB = makeLinkRelation(note.noteId, "internalLink", targetB.noteId);
+ note.getRelations = () => [linkA, linkB];
+ note.getAttachments = () => [];
+
+ const content = `Link to [[${targetA.noteId}]] and [Note B](#root/${targetB.noteId})`;
+ saveLinks(note, content);
+
+ expect(linkA.markAsDeleted).not.toHaveBeenCalled();
+ expect(linkB.markAsDeleted).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/server/src/services/notes.ts b/apps/server/src/services/notes.ts
index 67cb51e435..936f9b9683 100644
--- a/apps/server/src/services/notes.ts
+++ b/apps/server/src/services/notes.ts
@@ -387,6 +387,8 @@ function protectNote(note: BNote, protect: boolean) {
}
}
+export { saveLinks };
+
export function checkImageAttachments(note: BNote, content: string) {
const foundAttachmentIds = new Set<string>();
let match;
@@ -547,6 +549,41 @@ function findInternalLinks(content: string, foundLinks: FoundLink[]) {
return content.replace(/href="[^"]*#root/g, 'href="#root');
}
+function findMarkdownImageLinks(content: string, foundLinks: FoundLink[]) {
+ const re = /!\[[^\]]*\]\([^)]*api\/images\/([a-zA-Z0-9_]+)\//g;
+ let match;
+
+ while ((match = re.exec(content))) {
+ foundLinks.push({
+ name: "imageLink",
+ value: match[1]
+ });
+ }
+}
+
+function findMarkdownInternalLinks(content: string, foundLinks: FoundLink[]) {
+ // [text](#root/.../noteId)
+ const hashRootRe = /\[[^\]]*\]\(#root[a-zA-Z0-9_\/]*\/([a-zA-Z0-9_]+)[^)]*\)/g;
+ let match;
+
+ while ((match = hashRootRe.exec(content))) {
+ foundLinks.push({
+ name: "internalLink",
+ value: match[1]
+ });
+ }
+
+ // [[noteId]] wiki-links
+ const wikiLinkRe = /\[\[([a-zA-Z0-9_]+)\]\]/g;
+
+ while ((match = wikiLinkRe.exec(content))) {
+ foundLinks.push({
+ name: "internalLink",
+ value: match[1]
+ });
+ }
+}
+
function findIncludeNoteLinks(content: string, foundLinks: FoundLink[]) {
const re = /<section class="include-note[^>]+data-note-id="([a-zA-Z0-9_]+)"[^>]*>/g;
let match;
@@ -778,6 +815,8 @@ function saveLinks(note: BNote, content: string | Buffer) {
({ forceFrontendReload, content } = checkImageAttachments(note, content));
} else if (note.isMarkdown() && typeof content === "string") {
+ findMarkdownImageLinks(content, foundLinks);
+ findMarkdownInternalLinks(content, foundLinks);
({ forceFrontendReload, content } = checkImageAttachments(note, content));
} else if (note.type === "relationMap" && typeof content === "string") {
findRelationMapLinks(content, foundLinks);