Skip to content

Build your personal knowledge base with Trilium Notes

test(script/jsx): basic JSX processing

Elian Doran645557b505c4
diff --git a/apps/server/src/services/script.spec.ts b/apps/server/src/services/script.spec.ts
index da69786666..5ae13bbd75 100644
--- a/apps/server/src/services/script.spec.ts
+++ b/apps/server/src/services/script.spec.ts
@@ -1,9 +1,11 @@
+import { trimIndentation } from "@triliumnext/commons";
+
import becca from "../becca/becca.js";
-import { note, NoteBuilder } from "../test/becca_mocking.js";
-import cls from "./cls.js";
-import { executeBundle, getScriptBundle } from "./script.js";
import BBranch from "../becca/entities/bbranch.js";
import BNote from "../becca/entities/bnote.js";
+import { note, NoteBuilder } from "../test/becca_mocking.js";
+import cls from "./cls.js";
+import { buildJsx, executeBundle, getScriptBundle } from "./script.js";
describe("Script", () => {
@@ -84,3 +86,19 @@ describe("Script", () => {
});
});
});
+
+describe("JSX building", () => {
+ it("processes basic JSX", () => {
+ const script = trimIndentation`\
+ function MyComponent() {
+ return <p>Hello world.</p>;
+ }
+ `;
+ const expected = trimIndentation`\
+ const _jsxFileName = "";function MyComponent() {
+ return api.preact.h('p', {__self: this, __source: {fileName: _jsxFileName, lineNumber: 2}}, "Hello world." );
+ }
+ `;
+ expect(buildJsx(script).code).toStrictEqual(expected);
+ });
+});
diff --git a/apps/server/src/services/script.ts b/apps/server/src/services/script.ts
index 5e0682fbe6..b7fff617cc 100644
--- a/apps/server/src/services/script.ts
+++ b/apps/server/src/services/script.ts
@@ -198,8 +198,7 @@ export function getScriptBundle(note: BNote, root: boolean = true, scriptEnv: st
let scriptContent = note.getContent();
if (note.isJsx()) {
- console.log("GOT JSX!!!");
- scriptContent = buildJsx(note).code;
+ scriptContent = buildJsx(scriptContent).code;
}
bundle.script += `
@@ -219,8 +218,7 @@ return module.exports;
return bundle;
}
-function buildJsx(jsxNote: BNote) {
- const contentRaw = jsxNote.getContent();
+export function buildJsx(contentRaw: string | Buffer) {
const content = Buffer.isBuffer(contentRaw) ? contentRaw.toString("utf-8") : contentRaw;
return transform(content, {
transforms: ["jsx"],