Skip to content

Build your personal knowledge base with Trilium Notes

fix(notes): keep script-deployer IDs valid and an empty noteId generated

matt wilkiecb7ad4563c96
diff --git a/apps/script-deployer/src/deploy.spec.ts b/apps/script-deployer/src/deploy.spec.ts
index ba8e70d15a..1e337fb0ca 100644
--- a/apps/script-deployer/src/deploy.spec.ts
+++ b/apps/script-deployer/src/deploy.spec.ts
@@ -145,6 +145,13 @@ describe("transpile", () => {
// ── deployScript ─────────────────────────────────────────────────────────────
+describe("note ID helpers", () => {
+ it("builds note IDs that createNewNote() accepts from a kebab-case script ID", () => {
+ expect(codeNoteId("auto-import-xopp")).toBe("_sd_auto_import_xopp");
+ expect(renderNoteId("auto-import-xopp")).toBe("_sd_auto_import_xopp_render");
+ });
+});
+
describe("deployScript", () => {
/**
* A NoteService backed by buildNote — each createNewNote call
diff --git a/apps/script-deployer/src/deploy.ts b/apps/script-deployer/src/deploy.ts
index 4853cf2791..fbefb06396 100644
--- a/apps/script-deployer/src/deploy.ts
+++ b/apps/script-deployer/src/deploy.ts
@@ -97,12 +97,17 @@ export function transpile(source: string, filePath: string): string {
// ── Note ID helpers ──────────────────────────────────────────────────────────
+/** `createNewNote()` rejects a note ID with a dash, and the script IDs are kebab-case. */
+function toNoteIdPart(scriptId: string) {
+ return scriptId.replace(/[^A-Za-z0-9_]/g, "_");
+}
+
export function codeNoteId(scriptId: string) {
- return `_sd_${scriptId}`;
+ return `_sd_${toNoteIdPart(scriptId)}`;
}
export function renderNoteId(scriptId: string) {
- return `_sd_${scriptId}_render`;
+ return `_sd_${toNoteIdPart(scriptId)}_render`;
}
// ── Deployment ───────────────────────────────────────────────────────────────
diff --git a/packages/trilium-core/src/services/notes.spec.ts b/packages/trilium-core/src/services/notes.spec.ts
index 75b97f870e..8f312c211f 100644
--- a/packages/trilium-core/src/services/notes.spec.ts
+++ b/packages/trilium-core/src/services/notes.spec.ts
@@ -3,6 +3,7 @@ import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest
import becca from "../becca/becca.js";
import type BBranch from "../becca/entities/bbranch.js";
import type BNote from "../becca/entities/bnote.js";
+import { ValidationError } from "../errors.js";
import blobService from "./blob.js";
import { disableEntityEvents, getContext } from "./context.js";
import { getLog } from "./log.js";
@@ -179,15 +180,16 @@ describe("notes service (real DB)", () => {
});
it("accepts a well-formed forced noteId and rejects one that links cannot match", () => {
- for (const noteId of [ "forcedSpecId01", "_specForced01" ]) {
+ for (const noteId of [ "forcedSpecId01", "forced_spec_id_02" ]) {
const { note } = createNote("root", { title: "spec-forced-id", noteId });
expect(note.noteId).toBe(noteId);
expect(becca.notes[noteId]).toBe(note);
}
- const generated = createNote("root", { noteId: null as unknown as string }).note;
- expect(generated.noteId).toMatch(/^[A-Za-z0-9]{12}$/);
+ for (const noteId of [ null as unknown as string, "" ]) {
+ expect(createNote("root", { noteId }).note.noteId).toMatch(/^[A-Za-z0-9]{12}$/);
+ }
const dashedId = "0d8949e4-6fe3-4f4b-82c1-679baf64fccb";
const countRows = (noteId: string) =>
@@ -199,7 +201,7 @@ describe("notes service (real DB)", () => {
}
const numericId = 12345 as unknown as string;
- expect(() => createNote("root", { noteId: numericId })).toThrow(/is not valid/);
+ expect(() => createNote("root", { noteId: numericId })).toThrow(ValidationError);
});
it("inherits the template's mime and adds a template relation when creating from a template", () => {
diff --git a/packages/trilium-core/src/services/notes.ts b/packages/trilium-core/src/services/notes.ts
index 0a9353c244..4cb7faa546 100644
--- a/packages/trilium-core/src/services/notes.ts
+++ b/packages/trilium-core/src/services/notes.ts
@@ -243,8 +243,7 @@ function createNewNote(params: NoteParams): {
throw new Error(error);
}
- if (params.noteId !== undefined && params.noteId !== null
- && (typeof params.noteId !== "string" || !isValidEntityId(params.noteId))) {
+ if (params.noteId && (typeof params.noteId !== "string" || !isValidEntityId(params.noteId))) {
throw new ValidationError(`Note ID '${params.noteId}' is not valid. `
+ "Only letters, digits and underscores are allowed, with a length of 4 to 128.");
}