fix: preserve punctuation label colors

This commit is contained in:
Shawn Koh
2026-08-11 01:19:54 +08:00
parent e658d9f7be
commit cfbd567628
2 changed files with 13 additions and 4 deletions
@@ -76,6 +76,10 @@ describe("Mermaid rendering setting", () => {
expect(rendered).not.toMatch(/\x1b\[38;2;229;229;231m[╔═╗║╚╝]/); expect(rendered).not.toMatch(/\x1b\[38;2;229;229;231m[╔═╗║╚╝]/);
expect(rendered).not.toContain("\x1b[38;2;42;48;56m"); expect(rendered).not.toContain("\x1b[38;2;42;48;56m");
expect(rendered).not.toContain("\x1b[38;2;31;37;45m"); expect(rendered).not.toContain("\x1b[38;2;31;37;45m");
const labels = renderer("flowchart TD\n A[x=y]\n B[status=#1]", 80);
const text = "\x1b[38;2;229;229;231m";
expect(labels).toContain(`${text}x=y`);
expect(labels).toContain(`${text}status=#1`);
} finally { } finally {
setThemeInstance(dark); setThemeInstance(dark);
} }
+9 -4
View File
@@ -1225,15 +1225,18 @@ function fillRolesFromCanvases(
/** /**
* Special handling for node boxes: border chars get 'border' role, text gets 'text' role. * Special handling for node boxes: border chars get 'border' role, text gets 'text' role.
* Detects text by checking if character is alphanumeric or common punctuation. * Common final-state border characters (`#` and `=`) count only on the outer edge,
* so identical characters inside ordinary node labels retain the text role.
*/ */
function fillRolesForNodeBox( function fillRolesForNodeBox(
roleCanvas: RoleCanvas, roleCanvas: RoleCanvas,
canvas: Canvas, canvas: Canvas,
offset: DrawingCoord, offset: DrawingCoord,
): void { ): void {
const isBorderChar = (c: string) => /^[┌┐└┘├┤┬┴┼│─╭╮╰╯╔╗╚╝═║+\-|.':#=‖]$/.test(c) const isBorderChar = (c: string) => /^[┌┐└┘├┤┬┴┼│─╭╮╰╯+\-|.':]$/.test(c)
const isStateEndBorderChar = (c: string) => /^[╔╗╚╝═║#=‖]$/.test(c)
const maxX = canvas.length - 1
const maxY = (canvas[0]?.length ?? 1) - 1
for (let x = 0; x < canvas.length; x++) { for (let x = 0; x < canvas.length; x++) {
for (let y = 0; y < (canvas[0]?.length ?? 0); y++) { for (let y = 0; y < (canvas[0]?.length ?? 0); y++) {
const char = canvas[x]?.[y] const char = canvas[x]?.[y]
@@ -1242,7 +1245,9 @@ function fillRolesForNodeBox(
const ry = y + offset.y const ry = y + offset.y
// Use setRole which auto-expands the role canvas if needed // Use setRole which auto-expands the role canvas if needed
if (rx >= 0 && ry >= 0) { if (rx >= 0 && ry >= 0) {
setRole(roleCanvas, rx, ry, isBorderChar(char) ? 'border' : 'text') const isOuterEdge = x === 0 || x === maxX || y === 0 || y === maxY
const role = isBorderChar(char) || (isOuterEdge && isStateEndBorderChar(char)) ? 'border' : 'text'
setRole(roleCanvas, rx, ry, role)
} }
} }
} }