Skip to content

Commit b220894

Browse files
process colored text properly
1 parent cf170e4 commit b220894

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

templates/index.tmpl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,23 @@
120120
initializeSliderValues();
121121

122122
// Fix for preserving leading whitespace in HTMX responses
123+
// We need to handle both colored text (HTML) and preserve whitespace
123124
document.body.addEventListener('htmx:beforeSwap', function(evt) {
124125
if (evt.detail.target.id === 'result-code') {
125-
evt.detail.shouldSwap = true;
126-
evt.detail.target.textContent = evt.detail.xhr.responseText;
126+
// Don't use default swap behavior
127127
evt.preventDefault();
128+
129+
// Get the response and escape it, then restore the span tags for colors
130+
let content = evt.detail.xhr.responseText;
131+
132+
// Check if the content has HTML color spans
133+
if (content.includes('<span')) {
134+
// For colored content, use innerHTML but the pre/code block should preserve whitespace
135+
evt.detail.target.innerHTML = content;
136+
} else {
137+
// For plain text, use textContent to preserve everything exactly
138+
evt.detail.target.textContent = content;
139+
}
128140
}
129141
});
130142
});
@@ -138,7 +150,7 @@
138150
<h1>Mermaid ASCII <small style="font-size: 0.5em; color: #888;">v{{ .Version }}</small></h1>
139151
<div class="container">
140152
<div class="column">
141-
<form id="input" hx-post="/" hx-target="#result-code" hx-swap="textContent"
153+
<form id="input" hx-post="/" hx-target="#result-code" hx-swap="innerHTML"
142154
hx-trigger="click from:.example-buttons">
143155
<textarea
144156
name="mermaid" placeholder="graph LR"

0 commit comments

Comments
 (0)