Skip to content

Build your personal knowledge base with Trilium Notes

chore(import/markdown): reuse attachment name

Elian Doran79b5cf1f0099
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Markdown.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Markdown.html
index f172885093..87648af340 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Markdown.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Markdown.html
@@ -195,22 +195,28 @@
</ul>
</aside>
<h2>Import/export</h2>
+<h3>Import</h3>
+<p>By default, when importing a single Markdown file it automatically gets
+ converted to a&nbsp;<a class="reference-link" href="#root/_help_iPIMuisry3hd">Text</a>&nbsp;note.
+ To avoid that and have it imported as a Markdown note instead:</p>
<ul>
- <li>By default, when importing a single Markdown file it automatically gets
- converted to a&nbsp;<a class="reference-link" href="#root/_help_iPIMuisry3hd">Text</a>&nbsp;note.
- To avoid that and have it imported as a Markdown note instead:
- <ul>
- <li>Right click the&nbsp;<a class="reference-link" href="#root/_help_oPVyFC7WL2Lp">Note Tree</a>&nbsp;and
- select <em>Import into note</em>.</li>
- <li>Select the file normally.</li>
- <li>Uncheck <em>Import HTML, Markdown and TXT as text notes if it's unclear from the metadata</em>.</li>
- </ul>
- </li>
- <li>When exporting Markdown files, the extension is preserved and the content
- remains the same as in the source view.</li>
- <li>Once exported as a Trilium ZIP, the ZIP will preserve the Markdown type
- without converting to text notes thanks to the meta-information in it.</li>
+ <li>Right click the&nbsp;<a class="reference-link" href="#root/_help_oPVyFC7WL2Lp">Note Tree</a>&nbsp;and
+ select <em>Import into note</em>.</li>
+ <li>Select the file normally.</li>
+ <li>Uncheck <em>Import HTML, Markdown and TXT as text notes if it's unclear from the metadata</em>.</li>
</ul>
+<p>When importing a Trilium ZIP, it will preserve the Markdown type without
+ converting to text notes thanks to the meta-information in it.</p>
+<h3>Exporting</h3>
+<p>When exporting Markdown files, the extension is preserved and the content
+ remains the same as in the source view (with some small exceptions such
+ as handling of attachments).</p>
+<p>When exporting Markdown files as ZIP, choosing HTML or Markdown as the
+ export format makes no difference as that only affects&nbsp;<a class="reference-link"
+ href="#root/pOsGYCXsbNQG/KSZ04uQ2D1St/_help_iPIMuisry3hd">Text</a>&nbsp;notes.</p>
+<p>If the Markdown note contains attachments, a ZIP export will rewrite the
+ links to attachments so that they are replaced with a relative path to
+ the attachment. On import, the links are re-written back.</p>
<h2>Conversion between text notes and Markdown notes</h2>
<p>Currently there is no built-in functionality to convert a&nbsp;<a class="reference-link"
href="#root/_help_iPIMuisry3hd">Text</a>&nbsp;note into a Markdown note or vice-versa.
diff --git a/apps/server/src/services/import/zip.spec.ts b/apps/server/src/services/import/zip.spec.ts
index 6c2cd3a86a..4a922ee11d 100644
--- a/apps/server/src/services/import/zip.spec.ts
+++ b/apps/server/src/services/import/zip.spec.ts
@@ -1,8 +1,10 @@
import { beforeAll, describe, expect, it, vi } from "vitest";
+import archiver from "archiver";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { dirname } from "path";
+import { PassThrough } from "stream";
import zip, { removeTriliumTags } from "./zip.js";
import becca from "../../becca/becca.js";
import BNote from "../../becca/entities/bnote.js";
@@ -14,6 +16,10 @@ const scriptDir = dirname(fileURLToPath(import.meta.url));
async function testImport(fileName: string) {
const mdxSample = fs.readFileSync(path.join(scriptDir, "samples", fileName));
+ return testImportBuffer(mdxSample);
+}
+
+async function testImportBuffer(buffer: Buffer) {
const taskContext = TaskContext.getInstance("import-mdx", "importNotes", {
textImportedAsText: true
});
@@ -26,7 +32,7 @@ async function testImport(fileName: string) {
return;
}
- const importedNote = await zip.importZip(taskContext, mdxSample, rootNote as BNote);
+ const importedNote = await zip.importZip(taskContext, buffer, rootNote as BNote);
resolve({
importedNote,
rootNote
@@ -35,6 +41,19 @@ async function testImport(fileName: string) {
});
}
+async function createZipBuffer(files: Record<string, string | Buffer>): Promise<Buffer> {
+ const archive = archiver("zip");
+ const chunks: Buffer[] = [];
+ const passthrough = new PassThrough();
+ passthrough.on("data", (chunk: Buffer) => chunks.push(chunk));
+ archive.pipe(passthrough);
+ for (const [name, content] of Object.entries(files)) {
+ archive.append(content, { name });
+ }
+ await archive.finalize();
+ return Buffer.concat(chunks);
+}
+
describe("processNoteContent", () => {
beforeAll(async () => {
// Prevent download of images.
@@ -97,6 +116,40 @@ describe("processNoteContent", () => {
const content = attachment.getContent();
expect(content).toStrictEqual(`{"view":{"center":{"lat":49.19598332223546,"lng":-2.1414576506668808},"zoom":12}}`);
});
+
+ it("rewrites relative attachment paths in markdown code notes on import", async () => {
+ const metaFile = {
+ formatVersion: 2,
+ appVersion: "0.0.0",
+ files: [{
+ noteId: "mdCodeNote1",
+ title: "Markdown Note",
+ type: "code",
+ mime: "text/x-markdown",
+ dataFileName: "Markdown Note.md",
+ attachments: [{
+ attachmentId: "imgAtt1",
+ title: "image.jpg",
+ role: "image",
+ mime: "image/jpeg",
+ position: 10,
+ dataFileName: "Markdown Note_image.jpg"
+ }]
+ }]
+ };
+
+ const zipBuffer = await createZipBuffer({
+ "!!!meta.json": JSON.stringify(metaFile),
+ "Markdown Note.md": "# Hello\n\n![photo](Markdown Note_image.jpg)",
+ "Markdown Note_image.jpg": Buffer.from("fake image data")
+ });
+
+ const { importedNote } = await testImportBuffer(zipBuffer);
+ const content = importedNote.getContent() as string;
+ expect(content).toContain("![photo](api/attachments/");
+ expect(content).toContain("/image/image.jpg)");
+ expect(content).not.toContain("Markdown Note_image.jpg");
+ });
}, 60_000);
function getNoteByTitlePath(parentNote: BNote, ...titlePath: string[]) {
diff --git a/apps/server/src/services/import/zip.ts b/apps/server/src/services/import/zip.ts
index 1e57773c89..42e72aeb80 100644
--- a/apps/server/src/services/import/zip.ts
+++ b/apps/server/src/services/import/zip.ts
@@ -298,9 +298,10 @@ async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Bu
if (attachmentMeta && attachmentMeta.attachmentId && noteMeta.noteId) {
return {
attachmentId: getNewAttachmentId(attachmentMeta.attachmentId),
+ attachmentTitle: attachmentMeta.title,
noteId: getNewNoteId(noteMeta.noteId)
};
- }
+ }
// don't check for noteMeta since it's not mandatory for notes
return {
noteId: getNoteId(noteMeta, absUrl)
@@ -448,9 +449,9 @@ async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Bu
const target = getEntityIdFromRelativeUrl(url, filePath);
if (target.attachmentId) {
- return `![${alt}](api/attachments/${target.attachmentId}/image/image)`;
+ return `![${alt}](api/attachments/${target.attachmentId}/image/${encodeURIComponent(target.attachmentTitle || "image")})`;
} else if (target.noteId) {
- return `![${alt}](api/images/${target.noteId}/image)`;
+ return `![${alt}](api/images/${target.noteId}/${path.basename(url)})`;
}
return match;
});
diff --git a/docs/User Guide/User Guide/Note Types/Markdown.md b/docs/User Guide/User Guide/Note Types/Markdown.md
index 4199dd021d..a9e1ead163 100644
--- a/docs/User Guide/User Guide/Note Types/Markdown.md
+++ b/docs/User Guide/User Guide/Note Types/Markdown.md
@@ -141,12 +141,23 @@ There are two ways to create a Markdown note:
## Import/export
-* By default, when importing a single Markdown file it automatically gets converted to a <a class="reference-link" href="Text.md">Text</a> note. To avoid that and have it imported as a Markdown note instead:
- * Right click the <a class="reference-link" href="../Basic%20Concepts%20and%20Features/UI%20Elements/Note%20Tree.md">Note Tree</a> and select _Import into note_.
- * Select the file normally.
- * Uncheck _Import HTML, Markdown and TXT as text notes if it's unclear from the metadata_.
-* When exporting Markdown files, the extension is preserved and the content remains the same as in the source view.
-* Once exported as a Trilium ZIP, the ZIP will preserve the Markdown type without converting to text notes thanks to the meta-information in it.
+### Import
+
+By default, when importing a single Markdown file it automatically gets converted to a <a class="reference-link" href="Text.md">Text</a> note. To avoid that and have it imported as a Markdown note instead:
+
+* Right click the <a class="reference-link" href="../Basic%20Concepts%20and%20Features/UI%20Elements/Note%20Tree.md">Note Tree</a> and select _Import into note_.
+* Select the file normally.
+* Uncheck _Import HTML, Markdown and TXT as text notes if it's unclear from the metadata_.
+
+When importing a Trilium ZIP, it will preserve the Markdown type without converting to text notes thanks to the meta-information in it.
+
+### Exporting
+
+When exporting Markdown files, the extension is preserved and the content remains the same as in the source view (with some small exceptions such as handling of attachments).
+
+When exporting Markdown files as ZIP, choosing HTML or Markdown as the export format makes no difference as that only affects <a class="reference-link" href="Text.md">Text</a> notes.
+
+If the Markdown note contains attachments, a ZIP export will rewrite the links to attachments so that they are replaced with a relative path to the attachment. On import, the links are re-written back.
## Conversion between text notes and Markdown notes