@@ -54,6 +54,13 @@ const FLOWCHART_SVG_RE =
5454 * @param {{family?:string, woff2?:string} } [config.font] - Font used for SSR
5555 * measurement. If `woff2` (an absolute path) is given it's inlined as a
5656 * data URL with font-display:block so Chromium measures with the real font.
57+ * @param {string } [config.measurementCss] - Extra CSS injected into the
58+ * build-time render page so Mermaid measures labels with the SAME metrics
59+ * (font-weight / letter-spacing / padding) your stylesheet displays them at —
60+ * otherwise labels whose display weight differs from the measured one clip on
61+ * the right. Selectors must NOT include the `svg[aria-roledescription…]`
62+ * ancestor (Mermaid measures before that wrapper exists); use bare
63+ * `.nodeLabel p`, `.edgeLabel p`, etc.
5764 * @param {object } [config.flowchart] - Mermaid flowchart config (non-color).
5865 * @param {object } [config.sequence] - Mermaid sequence config (non-color).
5966 * @param {string } [config.securityLevel] - Mermaid securityLevel (default strict).
@@ -64,21 +71,37 @@ export function themedMermaid(config = {}) {
6471 classDefs = [ ] ,
6572 colorReplacements = [ ] ,
6673 font = { } ,
74+ measurementCss = "" ,
6775 flowchart = { } ,
6876 sequence = { } ,
6977 securityLevel = "strict" ,
7078 cache = join ( "node_modules" , ".cache" , "astro-themed-mermaid" ) ,
7179 } = config ;
7280
73- // --- build-time font inlining --------------------------------------------
74- // Mermaid measures node widths in the build-time Chromium against whatever
75- // font is available. If the declared font isn't actually loaded there, labels
76- // are measured against a fallback (~Arial) and overflow at runtime (Inter is
77- // ~6-8% wider per char). Inlining the woff2 with font-display:block makes
78- // Chromium load it synchronously before measuring. (fontsource's own CSS uses
79- // font-display:swap, which doesn't block, so it can't substitute for this.)
81+ // --- build-time measurement CSS ------------------------------------------
82+ // Mermaid sizes each label's box by MEASURING the label DOM in the build-time
83+ // Chromium, then the host browser DISPLAYS it. Anything that makes display
84+ // wider than measurement clips the last glyph (foreignObject crops to its
85+ // box). Two independent culprits, two pieces of injected CSS:
86+ //
87+ // 1. The FONT. If the declared font isn't loaded in the build Chromium,
88+ // labels are measured against a fallback (~Arial) and overflow at
89+ // runtime (Inter is ~6-8% wider per char). Inlining the woff2 with
90+ // font-display:block makes Chromium load it synchronously before
91+ // measuring. (fontsource's own CSS uses font-display:swap, which doesn't
92+ // block, so it can't substitute for this.)
93+ //
94+ // 2. Label METRICS — font-weight / letter-spacing / padding. If your
95+ // stylesheet renders node labels at, say, weight 500 but Mermaid
96+ // measured them at the default 400, every box is ~1px too narrow and the
97+ // last letter clips. Pass those rules as `measurementCss` so the build
98+ // measures what the browser shows. IMPORTANT: Mermaid measures the label
99+ // BEFORE it's parented by the final `svg[aria-roledescription="flowchart…"]`,
100+ // so write these selectors WITHOUT that ancestor — e.g. `.nodeLabel p`,
101+ // not `svg[aria-roledescription^="flowchart"] .nodeLabel p` (the latter
102+ // matches at display time but NOT at measure time, so it's a no-op here).
80103 let fontFamily = font . family || '"Arial", sans-serif' ;
81- let fontCssDataUrl ;
104+ const cssParts = [ ] ;
82105 if ( font . woff2 ) {
83106 try {
84107 const b64 = readFileSync ( font . woff2 ) . toString ( "base64" ) ;
@@ -93,24 +116,30 @@ export function themedMermaid(config = {}) {
93116 const primaryFamily =
94117 ( font . family || "" ) . split ( "," ) [ 0 ] . trim ( ) . replace ( / ^ [ " ' ] | [ " ' ] $ / g, "" ) ||
95118 "sans-serif" ;
96- const inlineCss = [
97- "@font-face{" ,
98- ` font-family: ${ JSON . stringify ( primaryFamily ) } ;` ,
99- `src:url(data: font/woff2;base64, ${ b64 } ) format('woff2-variations') ;`,
100- "font-weight:100 900;font-style:normal;font-display:block;}" ,
101- ] . join ( "" ) ;
102- fontCssDataUrl =
103- "data:text/css;base64," + Buffer . from ( inlineCss ) . toString ( "base64" ) ;
119+ cssParts . push (
120+ [
121+ "@ font-face{" ,
122+ ` font-family: ${ JSON . stringify ( primaryFamily ) } ;`,
123+ `src:url(data:font/woff2;base64, ${ b64 } ) format('woff2-variations');` ,
124+ "font-weight:100 900;font-style:normal;font-display:block;}" ,
125+ ] . join ( "" )
126+ ) ;
104127 } catch {
105128 // Font file not on disk (e.g. fresh clone, no install). Fall back to the
106129 // default family so the build still succeeds.
107130 fontFamily = '"Arial", sans-serif' ;
108131 }
109132 }
133+ // Caller's label-metric rules, injected after the @font-face so they can
134+ // depend on the inlined font being available.
135+ if ( measurementCss ) cssParts . push ( measurementCss ) ;
136+ const measurementCssDataUrl = cssParts . length
137+ ? "data:text/css;base64," + Buffer . from ( cssParts . join ( "" ) ) . toString ( "base64" )
138+ : undefined ;
110139
111140 const rehypeMermaidOptions = {
112141 strategy : "inline-svg" ,
113- ...( fontCssDataUrl ? { css : fontCssDataUrl } : { } ) ,
142+ ...( measurementCssDataUrl ? { css : measurementCssDataUrl } : { } ) ,
114143 mermaidConfig : {
115144 // `fontFamily` must sit at the top level — mermaid-isomorphic hard-codes
116145 // arial here if absent and ignores the same key in themeVariables.
0 commit comments