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
feat(script-api): add markdown conversions to frontend script API
Elian Doran820ad34b1709
diff --git a/apps/client/src/services/frontend_script_api.spec.ts b/apps/client/src/services/frontend_script_api.spec.tsindex 891a47cdb2..58d31a7829 100644--- a/apps/client/src/services/frontend_script_api.spec.ts+++ b/apps/client/src/services/frontend_script_api.spec.ts@@ -304,6 +304,27 @@ describe("search", () => {});});+describe("markdown conversion", () => {+ it("htmlToMarkdown posts the HTML to the server and returns the markdown", async () => {+ const post = vi.spyOn(server, "post").mockResolvedValue({ markdownContent: "# Hi" } as never);++ const result = await makeApi().htmlToMarkdown("<h1>Hi</h1>");++ expect(post).toHaveBeenCalledWith("other/to-markdown", { htmlContent: "<h1>Hi</h1>" });+ expect(result).toBe("# Hi");+ });++ it("markdownToHtml renders to HTML locally without hitting the server", async () => {+ const post = vi.spyOn(server, "post");++ const html = await makeApi().markdownToHtml("This is **bold**.");++ expect(html).toContain("<strong>bold</strong>");+ // The conversion runs entirely in the browser — no server round-trip.+ expect(post).not.toHaveBeenCalled();+ });+});+describe("froca passthroughs", () => {it("getNote / getNotes / reloadNotes delegate to froca", async () => {const note = buildNote({ title: "N" });diff --git a/apps/client/src/services/frontend_script_api.ts b/apps/client/src/services/frontend_script_api.tsindex 80823f383e..4ac424215f 100644--- a/apps/client/src/services/frontend_script_api.ts+++ b/apps/client/src/services/frontend_script_api.ts@@ -1,5 +1,6 @@-import { dayjs, formatLogMessage } from "@triliumnext/commons";+import { dayjs, formatLogMessage, type ToMarkdownResponse } from "@triliumnext/commons";import type { FrontendApi as PublicFrontendApi, ScriptFNote as PublicScriptFNote } from "@triliumnext/commons/src/lib/script_api.js";+import DOMPurify from "dompurify";import appContext from "../components/app_context.js";import type Component from "../components/component.js";@@ -469,6 +470,28 @@ export interface Api {*/formatNoteSize: typeof utils.formatSize;+ /**+ * Converts the given HTML string to Markdown.+ *+ * Unlike the backend API, this runs on the server (the HTML→Markdown+ * converter is backend-only), so it returns a promise.+ *+ * @param html - HTML content to convert+ * @returns Markdown representation of the input HTML+ */+ htmlToMarkdown(html: string): Promise<string>;++ /**+ * Converts the given Markdown string to HTML.+ *+ * Runs entirely in the browser; the promise is only needed because the+ * Markdown renderer is loaded on demand.+ *+ * @param markdown - Markdown content to convert+ * @returns HTML representation of the input Markdown+ */+ markdownToHtml(markdown: string): Promise<string>;+logMessages: Record<string, string[]>;logSpacedUpdates: Record<string, SpacedUpdate>;@@ -712,6 +735,19 @@ function FrontendScriptApi(this: Api, startNote: FNote, currentNote: FNote, origthis.formatSize = utils.formatSize;this.formatNoteSize = utils.formatSize;+ this.htmlToMarkdown = async (html) => {+ const { markdownContent } = await server.post<ToMarkdownResponse>("other/to-markdown", { htmlContent: html });+ return markdownContent;+ };+ this.markdownToHtml = async (markdown) => {+ // The Markdown renderer pulls in marked, so it is only loaded on demand.+ const { renderToHtml } = await import("@triliumnext/commons/src/lib/markdown_renderer");+ return renderToHtml(markdown, "", {+ sanitize: (dirty) => DOMPurify.sanitize(dirty),+ wikiLink: { formatHref: (id) => `#root/${id}` }+ });+ };+this.logMessages = {};this.logSpacedUpdates = {};this.log = (...args) => {diff --git a/packages/commons/src/lib/script_api.ts b/packages/commons/src/lib/script_api.tsindex d558f38952..9d3ca81aaa 100644--- a/packages/commons/src/lib/script_api.ts+++ b/packages/commons/src/lib/script_api.ts@@ -643,6 +643,26 @@ export interface FrontendApi {* @deprecated - use api.formatSize()*/formatNoteSize(size: number): string;+ /**+ * Converts the given HTML string to Markdown.+ *+ * Unlike the backend API, this runs on the server (the HTML→Markdown+ * converter is backend-only), so it returns a promise.+ *+ * @param html - HTML content to convert+ * @returns Markdown representation of the input HTML+ */+ htmlToMarkdown(html: string): Promise<string>;+ /**+ * Converts the given Markdown string to HTML.+ *+ * Runs entirely in the browser; the promise is only needed because the+ * Markdown renderer is loaded on demand.+ *+ * @param markdown - Markdown content to convert+ * @returns HTML representation of the input Markdown+ */+ markdownToHtml(markdown: string): Promise<string>;/*** @returns date in YYYY-MM-DD format*/