Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(script): logging api.startNote not working (closes #3751)
Elian Doran4ed222620654
diff --git a/packages/commons/src/lib/utils.ts b/packages/commons/src/lib/utils.tsindex 93b953f469..eb5ad3a674 100644--- a/packages/commons/src/lib/utils.ts+++ b/packages/commons/src/lib/utils.ts@@ -1,10 +1,32 @@+const MAX_LOG_DEPTH = 3;++/**+ * Creates a JSON replacer that handles circular references and limits depth.+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#examples+ */+function getCircularReplacer() {+ const ancestors: object[] = [];+ return function (this: object, _key: string, value: unknown) {+ if (typeof value !== "object" || value === null) {+ return value;+ }+ while (ancestors.length > 0 && ancestors.at(-1) !== this) {+ ancestors.pop();+ }+ if (ancestors.includes(value)) {+ return "[Circular]";+ }+ if (ancestors.length >= MAX_LOG_DEPTH) {+ return "[Object]";+ }+ ancestors.push(value);+ return value;+ };+}+export function formatLogMessage(message: string | object) {if (typeof message === "object") {- try {- return JSON.stringify(message, null, 4);- } catch (e) {- return message.toString();- }+ return JSON.stringify(message, getCircularReplacer(), 4);}return message;