Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): map Obsidian special front matter keys (tags/aliases)

Elian Doran944f4cc64a46
diff --git a/packages/trilium-core/src/services/import/obsidian/importer.integration.spec.ts b/packages/trilium-core/src/services/import/obsidian/importer.integration.spec.ts
index 1e4923b458..d2ccbf3fa3 100644
--- a/packages/trilium-core/src/services/import/obsidian/importer.integration.spec.ts
+++ b/packages/trilium-core/src/services/import/obsidian/importer.integration.spec.ts
@@ -92,9 +92,37 @@ describe("Obsidian importer — integration", () => {
// Properties become labels with camelCased names; a list yields one label per item.
expect(note.getOwnedLabelValue("first")).toBe("First value");
- expect(note.getOwnedLabelValues("tags")).toEqual(["Tag", "AnotherTag"]);
expect(note.getOwnedLabelValue("checkboxProp")).toBe("true");
expect(note.getOwnedLabelValues("list")).toEqual(["a", "b"]);
+ // tags are an Obsidian special key → individual labels (covered in detail below). A single-token tag
+ // is fully lower-cased by toAttributeName (e.g. "AnotherTag" → "anothertag").
+ const labelNames = note.getOwnedAttributes().filter((a) => a.type === "label").map((a) => a.name);
+ expect(labelNames).toContain("tag");
+ expect(labelNames).toContain("anothertag");
+ });
+
+ it("maps Obsidian special front matter keys: tags → labels, aliases → #alias, dropping cssclasses/publish/permalink", async () => {
+ const importRoot = await importObsidian({
+ "Note.md": "---\ntags:\n - Book\n - Reading List\naliases:\n - Alt name\ncssclasses:\n - foo\npublish: true\npermalink: /x\nfirst: First value\n---\nBody."
+ });
+
+ const note = importRoot.getChildNotes().find((n) => n.title === "Note");
+ if (!note) {
+ throw new Error("note was not imported");
+ }
+ const labels = note.getOwnedAttributes().filter((a) => a.type === "label");
+ const names = labels.map((a) => a.name);
+
+ // tags → individual labels named after the (sanitized) tag.
+ expect(names).toContain("book");
+ expect(names).toContain("readingList");
+ // aliases → #alias labels preserving the alternate name.
+ expect(labels.filter((a) => a.name === "alias").map((a) => a.value)).toEqual(["Alt name"]);
+ // dropped keys produce no labels; a regular property is unaffected.
+ expect(names).not.toContain("cssclasses");
+ expect(names).not.toContain("publish");
+ expect(names).not.toContain("permalink");
+ expect(note.getOwnedLabelValue("first")).toBe("First value");
});
it("converts ==highlights== to a coloured span and %%comments%% to dropped HTML comments", async () => {
diff --git a/packages/trilium-core/src/services/import/obsidian/importer.spec.ts b/packages/trilium-core/src/services/import/obsidian/importer.spec.ts
new file mode 100644
index 0000000000..e2d36d8a99
--- /dev/null
+++ b/packages/trilium-core/src/services/import/obsidian/importer.spec.ts
@@ -0,0 +1,44 @@
+import { describe, expect, it } from "vitest";
+
+import { toObsidianLabels } from "./importer.js";
+
+describe("toObsidianLabels", () => {
+ it("maps each tag to its own label, the sanitized tag as the name", () => {
+ expect(toObsidianLabels([
+ { name: "tags", value: "Book" },
+ { name: "tags", value: "Reading List" }
+ ])).toEqual([
+ { name: "book", value: "" },
+ { name: "readingList", value: "" }
+ ]);
+ });
+
+ it("maps each alias to an #alias label, preserving the alternate name", () => {
+ expect(toObsidianLabels([
+ { name: "aliases", value: "Alias one" },
+ { name: "aliases", value: "Alias two" }
+ ])).toEqual([
+ { name: "alias", value: "Alias one" },
+ { name: "alias", value: "Alias two" }
+ ]);
+ });
+
+ it("drops cssclasses, publish and permalink", () => {
+ expect(toObsidianLabels([
+ { name: "cssclasses", value: "class1" },
+ { name: "publish", value: "true" },
+ { name: "permalink", value: "/x" }
+ ])).toEqual([]);
+ });
+
+ it("leaves other properties untouched", () => {
+ expect(toObsidianLabels([{ name: "first", value: "First value" }])).toEqual([{ name: "first", value: "First value" }]);
+ });
+
+ it("skips empty tag and alias values", () => {
+ expect(toObsidianLabels([
+ { name: "tags", value: "" },
+ { name: "aliases", value: " " }
+ ])).toEqual([]);
+ });
+});
diff --git a/packages/trilium-core/src/services/import/obsidian/importer.ts b/packages/trilium-core/src/services/import/obsidian/importer.ts
index 62458c55dd..b5165fd0ad 100644
--- a/packages/trilium-core/src/services/import/obsidian/importer.ts
+++ b/packages/trilium-core/src/services/import/obsidian/importer.ts
@@ -17,9 +17,10 @@
* Obsidian-specific inline syntax is handled during Markdown rendering (gated by the `obsidian` flag):
* `==highlight==` becomes `<mark>` and `%% comment %%` becomes an HTML comment.
*
- * Front matter is parsed generically (see {@link ../frontmatter.js}) into camelCased labels. Obsidian's
- * property typing (date/number/checkbox via `.obsidian/types.json`), special keys (tags, aliases) and
- * callouts are deferred to later passes.
+ * Front matter is parsed generically (see {@link ../frontmatter.js}) into camelCased labels, then Obsidian's
+ * special keys are applied (see {@link ./frontmatter.js}): tags become individual labels, aliases become
+ * `#alias` labels, and cssclasses/publish/permalink are dropped. Property typing (date/number/checkbox via
+ * `.obsidian/types.json`) and callouts are deferred to later passes.
*
* Invoked from the shared file-import dispatcher (routes/api/import.ts) when the upload is tagged
* `format=obsidian`, so progress, completion and failure are reported by that dispatcher's TaskContext —
@@ -35,7 +36,8 @@ import type TaskContext from "../../task_context.js";
import { decodeUtf8 } from "../../utils/binary.js";
import { basename } from "../../utils/path.js";
import { getZipProvider } from "../../zip_provider.js";
-import { extractFrontmatter } from "../frontmatter.js";
+import { toAttributeName } from "../collection_utils.js";
+import { extractFrontmatter, type FrontmatterAttribute } from "../frontmatter.js";
import markdownService from "../markdown.js";
import { applyAttachments, type AttachmentIndex, buildAttachmentIndex, isImageMime, resolveAttachment } from "./attachments.js";
import { type ExcalidrawDrawing, isExcalidrawPath, parseExcalidraw } from "./excalidraw.js";
@@ -141,9 +143,10 @@ function createNotes(importRootNote: BNote, notes: VaultNote[], attachments: Map
const rendered = markdownService.renderToHtml(body, vaultNote.title, { obsidian: true });
const { note } = noteService.createNewNote({ parentNoteId: parent.noteId, title: vaultNote.title, content: rendered, type: "text", mime: "text/html", isProtected });
- // Front matter properties become labels, their names camelCased like the other importers. Obsidian's
- // typing (date/number/checkbox) and special keys (tags, aliases) layer on in a later pass.
- for (const attribute of attributes) {
+ // Front matter properties become labels (camelCased like the other importers), then Obsidian's special
+ // keys are applied: tags become individual labels, aliases become #alias labels, and
+ // cssclasses/publish/permalink are dropped. Property typing (date/number/checkbox) layers on later.
+ for (const attribute of toObsidianLabels(attributes)) {
note.addLabel(attribute.name, attribute.value);
}
@@ -197,6 +200,35 @@ function createExcalidrawNote(parent: BNote, vaultNote: VaultNote, attachmentInd
return note;
}
+const DROPPED_FRONTMATTER_KEYS = new Set(["cssclasses", "publish", "permalink"]);
+
+/**
+ * Applies Obsidian's special front matter property semantics on top of the generic parse: each `tags` value
+ * becomes its own label (the sanitized tag as the name, e.g. `#book`), each `aliases` value becomes an
+ * `#alias` label (the alternate name preserved), and `cssclasses`/`publish`/`permalink` are dropped
+ * (presentation / publish-site metadata with no Trilium meaning). Every other property is left as-is.
+ */
+export function toObsidianLabels(attributes: FrontmatterAttribute[]): FrontmatterAttribute[] {
+ const labels: FrontmatterAttribute[] = [];
+ for (const { name, value } of attributes) {
+ if (DROPPED_FRONTMATTER_KEYS.has(name)) {
+ continue;
+ }
+ if (name === "tags") {
+ if (value.trim()) {
+ labels.push({ name: toAttributeName(value), value: "" });
+ }
+ } else if (name === "aliases") {
+ if (value.trim()) {
+ labels.push({ name: "alias", value });
+ }
+ } else {
+ labels.push({ name, value });
+ }
+ }
+ return labels;
+}
+
/** Returns (creating on demand, parents first) the container note for `folderPath`; the empty path is the root. */
function ensureFolder(folderPath: string, rootNote: BNote, folderNotes: Map<string, BNote>, isProtected: boolean): BNote {
if (folderPath === "") {