Trim trailing whitespace in pre blocks in Markdown

This commit is contained in:
Adrià Arrufat
2026-02-16 21:34:46 +09:00
parent b49b2af11f
commit 3c14dbe382

View File

@@ -36,6 +36,7 @@ const State = struct {
list_depth: usize = 0,
list_stack: [32]ListState = undefined,
in_pre: bool = false,
pre_node: ?*Node = null,
in_code: bool = false,
in_blockquote: bool = false,
in_table: bool = false,
@@ -63,7 +64,15 @@ const State = struct {
},
.cdata => |cd| {
if (node.is(Node.CData.Text)) |_| {
try renderText(cd.getData(), state, writer);
var text = cd.getData();
if (state.in_pre) {
if (state.pre_node) |pre| {
if (node.parentNode() == pre and node.nextSibling() == null) {
text = std.mem.trimRight(u8, text, " \t\r\n");
}
}
}
try renderText(text, state, writer);
}
},
else => {}, // Ignore other node types
@@ -163,13 +172,15 @@ const State = struct {
// Add spacing
try writer.writeByte(' ');
},
.blockquote => { try writer.writeAll("> ");
.blockquote => {
try writer.writeAll("> ");
state.in_blockquote = true;
state.last_char_was_newline = false;
},
.pre => {
try writer.writeAll("```\n");
state.in_pre = true;
state.pre_node = el.asNode();
state.last_char_was_newline = true;
},
.code => {
@@ -259,6 +270,7 @@ const State = struct {
}
try writer.writeAll("```\n");
state.in_pre = false;
state.pre_node = null;
state.last_char_was_newline = true;
},
.code => {