splinter: the chat renders markdown

**bold**   *italic*   `code`   # heading   ``` fence

The markers are removed before anything is measured: a line measured with its
asterisks wraps in a different place than the one that is drawn, so height and
drawing now use the same stripped string. Each kept character carries an
attribute, which keeps a bold word bold across a wrap.

Fence blocks lose their marker lines and are drawn dim - an answer full of C
should look like code, not like three backticks around code.

Measured: zero '**' and zero '```' in a rendered answer, the list intact, the C
block readable.

One bug found by looking at the result: '_' was treated as an italic marker, so
the model name came out as Qwen3.6-35B-A3B-UD-Q8KX - a different model as far as
a reader is concerned. Only the asterisk marks italics now; underscores belong to
names.
This commit is contained in:
Konrad committed 2026-11-08 19:26:44 +00:00
1 parent 5595352389
commit 3b391e740d
2 files changed
+148 -2

No files matched your search

+148 -2
View File
@@ -561,6 +561,134 @@ entry_prefix(const struct entry *e, char *buf, size_t len)
}
}
/*
* Markdown, the little of it a chat actually contains.
*
* **bold** *italic* `code` # heading ``` fence
*
* The markers are removed before anything is measured, because a line measured
* with its asterisks wraps in a different place than the one that is drawn. Each
* kept character carries an attribute, so a bold word stays bold across a wrap.
*
* Fence blocks are drawn dim and their marker lines disappear: an answer full of
* C code should look like code, not like three backticks around code.
*/
#define MD_BOLD 0x01
#define MD_ITALIC 0x02
#define MD_CODE 0x04
static void
md_strip(const char *in, char *out, size_t cap, unsigned char *attr)
{
size_t o = 0;
int bold = 0, italic = 0, code = 0, fence = 0;
const char *p = in != NULL ? in : "";
while (*p != '\0' && o + 2 < cap) {
/* a fence line is a mode switch, not text */
if ((p[0] == '`' && p[1] == '`' && p[2] == '`') &&
(p == in || p[-1] == '\n')) {
fence = !fence;
while (*p != '\0' && *p != '\n')
p++;
continue;
}
if (*p == '\n') {
out[o] = '\n';
attr[o++] = fence ? MD_CODE : 0;
p++;
bold = italic = code = 0;
continue;
}
if (p[0] == '#' && (p == in || p[-1] == '\n')) {
while (*p == '#')
p++;
while (*p == ' ')
p++;
bold = 1;
continue;
}
if (!fence && p[0] == '*' && p[1] == '*') {
bold = !bold;
p += 2;
continue;
}
if (!fence && *p == '`') {
code = !code;
p++;
continue;
}
/*
* Only the asterisk marks italics. An underscore is part of names:
* Qwen3.6-35B-A3B-UD-Q8_K_XL came out as Qwen3.6-35B-A3B-UD-Q8KX,
* which is a different model as far as a reader is concerned.
*/
if (!fence && *p == '*') {
italic = !italic;
p++;
continue;
}
out[o] = *p;
attr[o] = (unsigned char)((fence ? MD_CODE : 0) |
(bold ? MD_BOLD : 0) | (italic ? MD_ITALIC : 0) |
(code ? MD_CODE : 0));
o++;
p++;
}
out[o] = '\0';
if (o < cap)
attr[o] = 0;
}
/* draw one prepared markdown string, wrapping at 'cols', for at most 'maxrows' */
static int
md_draw(int y, int cols, int maxrows, const char *text, const unsigned char *attr)
{
int row = 0, col = 0, x = 1;
size_t i;
for (i = 0; text[i] != '\0' && row < maxrows; i++) {
char out[8];
char one[2];
unsigned a = attr[i];
if (text[i] == '\n') {
row++;
col = 0;
x = 1;
continue;
}
if (col >= cols) {
row++;
col = 0;
x = 1;
if (row >= maxrows)
break;
}
one[0] = text[i];
one[1] = '\0';
if ((unsigned char)text[i] < 0x80) {
out[0] = text[i];
out[1] = '\0';
} else if (sp_ui_ascii(one, out, sizeof(out)) != 0)
out[0] = '?', out[1] = '\0';
attrset(COLOR_PAIR(PAIR_MAIN));
if (a & MD_BOLD)
attron(A_BOLD);
if (a & MD_ITALIC)
attron(A_UNDERLINE);
if (a & MD_CODE)
attron(COLOR_PAIR(PAIR_DIM));
mvwaddstr(ui_hist, y + row, x, out);
attrset(A_NORMAL);
x += (int)strlen(out);
col += (int)strlen(out);
}
return row + 1;
}
static int
entry_lines(int cols, const struct entry *e)
{
@@ -579,7 +707,13 @@ entry_lines(int cols, const struct entry *e)
return 1;
snprintf(render, len, "%s%s", prefix, e->text);
}
rows = text_rows(render, cols);
{
char plain[4096];
unsigned char attr[4096];
md_strip(render, plain, sizeof(plain), attr);
rows = text_rows(plain, cols);
}
free(render);
if (rows == 0)
rows = 1;
@@ -666,7 +800,19 @@ draw_history(struct ui_state *s, int reserve)
wattron(ui_hist, COLOR_PAIR(PAIR_ACCENT) | A_BOLD);
else
wattron(ui_hist, COLOR_PAIR(PAIR_DIM));
ui_add_text(ui_hist, render + off, rows * cols);
{
char plain[4096];
unsigned char attr[4096];
md_strip(render, plain, sizeof(plain), attr);
(void)md_draw(y, cols, rows - y,
plain + (off > 0 ? off : 0), attr +
(off > 0 ? off : 0));
getyx(ui_hist, ny, nx);
y = nx == 0 ? ny : ny + 1;
free(render);
continue;
}
wattrset(ui_hist, A_NORMAL);
free(render);
BIN
View File
Binary file not shown.