Skip to content

Build your personal knowledge base with Trilium Notes

fix(share): mermaid manifest in docs builds and dev exports

Elian Doran98b6a858ead8
diff --git a/apps/build-docs/src/build-docs.ts b/apps/build-docs/src/build-docs.ts
index cfe7710193..0caa991359 100644
--- a/apps/build-docs/src/build-docs.ts
+++ b/apps/build-docs/src/build-docs.ts
@@ -324,9 +324,14 @@ export default async function buildDocs({ gitRootDir }: BuildContext) {
/**
* Builds the share theme and the client, whose mermaid the export copies next to the theme for
- * the pages that have a diagram.
+ * the pages that have a diagram. The client builds for production: this process runs with
+ * `NODE_ENV=development`, under which the client's Vite config writes no `share_mermaid.json`.
*/
function buildShareThemeAssets(gitRootDir: string) {
execSync("pnpm run --filter share-theme dist", { stdio: "inherit", cwd: gitRootDir });
- execSync("pnpm run --filter client build", { stdio: "inherit", cwd: gitRootDir });
+ execSync("pnpm run --filter client build", {
+ stdio: "inherit",
+ cwd: gitRootDir,
+ env: { ...process.env, NODE_ENV: "production" }
+ });
}
diff --git a/apps/server/src/routes/assets.spec.ts b/apps/server/src/routes/assets.spec.ts
index e5febebec1..0671c9f2d0 100644
--- a/apps/server/src/routes/assets.spec.ts
+++ b/apps/server/src/routes/assets.spec.ts
@@ -1,6 +1,8 @@
import { afterEach, describe, expect, it } from "vitest";
-import { getClientDir, getPdfjsAssetDir, getShareThemeAssetDir } from "./assets.js";
+import path from "path";
+
+import { getClientBuildDir, getClientDir, getPdfjsAssetDir, getShareThemeAssetDir } from "./assets.js";
// The test runner sets NODE_ENV=development, which exercises the dev branch of
// these path helpers. Toggle to production to cover the resource-dir branch too.
@@ -13,6 +15,7 @@ describe("assets path helpers", () => {
expect(getShareThemeAssetDir()).toContain("share-theme");
expect(getPdfjsAssetDir()).toContain("pdfjs-viewer");
expect(getClientDir()).toContain("client");
+ expect(getClientBuildDir()).toBe(path.join(getClientDir(), "..", "dist"));
});
it("resolves resource-dir paths under production", () => {
@@ -20,5 +23,6 @@ describe("assets path helpers", () => {
expect(getShareThemeAssetDir()).toContain("share-theme");
expect(getPdfjsAssetDir()).toContain("pdfjs-viewer");
expect(typeof getClientDir()).toBe("string");
+ expect(getClientBuildDir()).toBe(getClientDir());
});
});
diff --git a/apps/server/src/routes/assets.ts b/apps/server/src/routes/assets.ts
index 8dd3ee2e51..d4529da93d 100644
--- a/apps/server/src/routes/assets.ts
+++ b/apps/server/src/routes/assets.ts
@@ -133,10 +133,9 @@ export function getShareThemeAssetDir() {
/** The client's build output: `public/` in a packaged app, `apps/client/dist` in development. */
export function getClientBuildDir() {
if (process.env.NODE_ENV === "development") {
- const srcRoot = path.join(__dirname, "..", "..");
- return path.join(srcRoot, "../client/dist");
+ return path.join(getClientDir(), "..", "dist");
}
- return path.join(getResourceDir(), "public");
+ return getClientDir();
}
export function getPdfjsAssetDir() {
diff --git a/apps/server/src/services/export/zip/share_theme.ts b/apps/server/src/services/export/zip/share_theme.ts
index c108d2c177..a589121862 100644
--- a/apps/server/src/services/export/zip/share_theme.ts
+++ b/apps/server/src/services/export/zip/share_theme.ts
@@ -58,6 +58,11 @@ function addMermaidFiles(files: Map<string, string | Uint8Array>) {
const manifest = JSON.parse(readFileSync(manifestPath, "utf-8")) as ShareMermaidManifest;
const mapped = mapMermaidExportFiles(manifest);
+ if (!mapped) {
+ getLog().info(`Exporting without mermaid, since ${manifestPath} lists no built files.`);
+ return;
+ }
+
files.set(mapped.manifest.path, mapped.manifest.content);
for (const { source, target } of mapped.files) {
files.set(target, readFileSync(join(manifestDir, source)));
diff --git a/apps/standalone/src/lightweight/zip_export_provider_factory.spec.ts b/apps/standalone/src/lightweight/zip_export_provider_factory.spec.ts
index 8abdbdae74..ffa96fa7b8 100644
--- a/apps/standalone/src/lightweight/zip_export_provider_factory.spec.ts
+++ b/apps/standalone/src/lightweight/zip_export_provider_factory.spec.ts
@@ -76,6 +76,21 @@ describe("standaloneZipExportProviderFactory", () => {
.toBe("content of /src/entry-a.js");
});
+ it("exports without mermaid when the development server lists no built files", async () => {
+ const manifest = { entry: "/@fs/repo/apps/client/src/share_mermaid.ts", files: [] };
+ vi.stubGlobal("fetch", vi.fn(async (url: string) => new Response(
+ url.endsWith("share_mermaid.json") ? JSON.stringify(manifest) : "content"
+ )));
+
+ const provider = await standaloneZipExportProviderFactory("share",
+ makeData(`<pre><code class="language-mermaid">graph TD;</code></pre>`));
+
+ type WithAssets = { assets: { files: Map<string, string | Uint8Array> } };
+ const names = [ ...(provider as unknown as WithAssets).assets.files.keys() ];
+ expect(names).toContain("assets/scripts.js");
+ expect(names.filter((name) => name.startsWith("assets/client/"))).toEqual([]);
+ });
+
it("fails the share-theme export when a theme file cannot be fetched", async () => {
vi.stubGlobal("fetch", vi.fn(async () => new Response("", { status: 404 })));
diff --git a/apps/standalone/src/lightweight/zip_export_provider_factory.ts b/apps/standalone/src/lightweight/zip_export_provider_factory.ts
index 88e79c003e..a6176cccab 100644
--- a/apps/standalone/src/lightweight/zip_export_provider_factory.ts
+++ b/apps/standalone/src/lightweight/zip_export_provider_factory.ts
@@ -2,6 +2,7 @@ import type { ShareMermaidManifest } from "@triliumnext/commons";
import {
binary_utils,
type ExportFormat,
+ getLog,
icon_packs,
type ZipExportProviderData,
ZipExportProvider
@@ -71,7 +72,10 @@ async function loadShareThemeExportAssets(): Promise<ShareThemeExportAssets> {
};
}
-/** Fetches the client's mermaid through the manifest the build writes next to the share theme. */
+/**
+ * Fetches the client's mermaid through the manifest the build writes next to the share theme. The
+ * development server's manifest lists no built files, so its exports show diagrams as code blocks.
+ */
async function addMermaidFiles(
files: Map<string, string | Uint8Array>,
mapFiles: typeof mapMermaidExportFiles
@@ -80,6 +84,11 @@ async function addMermaidFiles(
const manifestBytes = await fetchAsset(manifestUrl.href);
const manifest = JSON.parse(binary_utils.decodeUtf8(manifestBytes)) as ShareMermaidManifest;
const mapped = mapFiles(manifest);
+ if (!mapped) {
+ getLog().info("Exporting without mermaid, since the manifest lists no built files.");
+ return;
+ }
+
const contents = await Promise.all(mapped.files.map(({ source }) =>
fetchAsset(new URL(source, manifestUrl).href)));
diff --git a/packages/trilium-core/src/services/export/zip/share_theme.spec.ts b/packages/trilium-core/src/services/export/zip/share_theme.spec.ts
index fce33a20f8..15aa7251a2 100644
--- a/packages/trilium-core/src/services/export/zip/share_theme.spec.ts
+++ b/packages/trilium-core/src/services/export/zip/share_theme.spec.ts
@@ -359,10 +359,18 @@ describe("hasMermaidDiagrams", () => {
});
describe("mapMermaidExportFiles", () => {
+ it("maps nothing from a development manifest, which lists no built files", () => {
+ expect(mapMermaidExportFiles({ entry: "/@fs/repo/apps/client/src/share_mermaid.ts", files: [] }))
+ .toBeUndefined();
+ });
+
it("flattens the listed files into assets/client and rewrites the manifest to match", () => {
const entry = "../../../src/share_mermaid-abc.js";
const core = "../../../src/mermaid.core-def.js";
const mapped = mapMermaidExportFiles({ entry, files: [ entry, core ] });
+ if (!mapped) {
+ throw new Error("The manifest mapped to nothing.");
+ }
expect(mapped.files).toEqual([
{ source: entry, target: "assets/client/share_mermaid-abc.js" },
diff --git a/packages/trilium-core/src/services/export/zip/share_theme.ts b/packages/trilium-core/src/services/export/zip/share_theme.ts
index 0bc388402a..7e732fa387 100644
--- a/packages/trilium-core/src/services/export/zip/share_theme.ts
+++ b/packages/trilium-core/src/services/export/zip/share_theme.ts
@@ -211,9 +211,14 @@ const MARKDOWN_MERMAID_FENCE = /^ {0,3}(`{3,}|~{3,})\s*mermaid\b/m;
/**
* Maps the files `manifest` lists to their place in the archive, flattened into `assets/client/`,
- * and returns the manifest the exported pages read there.
+ * and returns the manifest the exported pages read there. Returns `undefined` when the manifest
+ * does not list its own entry: a development server's points at a source module and lists no files.
*/
export function mapMermaidExportFiles(manifest: ShareMermaidManifest) {
+ if (!manifest.files.includes(manifest.entry)) {
+ return undefined;
+ }
+
return {
manifest: {
path: `${MERMAID_ARCHIVE_DIR}/share_mermaid.json`,