Files
Snow Lee (Sungwon)andGitHub 2875e033be feat(webapp): mermaid fences render as diagrams, in the viewer and on share pages (BEA-91) (#143)
A ```mermaid fence rendered as a wall of `graph TD` source on both surfaces.
It now renders as an SVG in the hub file viewer and on public /s/<token>
markdown share pages.

Mermaid ships inside the binary (no CDN, so air-gapped self-hosters keep
working) and is imported lazily: a document with no fence downloads none of
it. A fence that doesn't parse — the common case for hand-written wiki
diagrams — keeps today's <pre><code> plus a small note, and one bad fence
never stops the good ones beside it. A blocked or offline chunk lands in the
same place.

The share page is the harder half: it is server-rendered Go HTML with no
JavaScript, and its `sandbox allow-scripts` CSP makes the origin opaque, so a
module script and every import() it makes arrive with `Origin: null`. The CSP
is unchanged and gains no allow-same-origin; instead the real-asset branch of
frontend() now sets Access-Control-Allow-Origin, which only ever touches files
that are already public and cookie-less. The script tag itself is injected
only when the rendered document actually contains a fence.

Also fixes an embed bug this change surfaced: `//go:embed static` silently
skips names beginning with `_`, and Vite's first shared chunk is
`_commonjsHelpers-<hash>.js`. The build passed, the commit looked right, and
the served app was blank. It is `all:static` now, with a test that every file
on disk is in the binary.
2026-08-10 14:18:18 -07:00

47 lines
1.7 KiB
TypeScript

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
import path from "node:path";
// Build output feeds the Go binary: ../static is the go:embed target in
// server.go, and the compiled assets are committed so plain `go build`
// needs no Node. Run ./check-dist.sh to verify the committed output is
// fresh.
// Dev target: a locally running hub (`bdrive web` or the e2e harness).
const target = process.env.BDRIVE_DEV_PROXY || "http://localhost:8080";
export default defineConfig({
plugins: [react(), tailwindcss()],
resolve: { alias: { "@": path.resolve(__dirname, "src") } },
build: {
outDir: "../static",
emptyOutDir: true,
rollupOptions: {
// Two entries: the SPA, and the one script the server-rendered /s/
// share page loads when its document has a mermaid fence.
input: {
index: path.resolve(__dirname, "index.html"),
"share-mermaid": path.resolve(__dirname, "src/share-mermaid.ts"),
},
output: {
// Fixed name OUTSIDE assets/: sharedMarkdownShell is a const string
// and can't know a content hash, and server.go marks assets/
// immutable for a year — an unhashed file there would pin a stale
// bundle in shared caches. At the static root it gets no-cache.
// Mermaid's own chunks keep the hashed assets/ names.
entryFileNames: (c) =>
c.name === "share-mermaid" ? "[name].js" : "assets/[name]-[hash].js",
},
},
},
server: {
proxy: {
// Everything the Go server owns; the frontend itself only ever uses
// root-absolute URLs, so prefix proxying is enough.
"/api": target,
"/auth": target,
"^/s/": target,
},
},
});