From 8c62ca981ecf5ad3c9360a5881c73b892254b7bf Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 30 Nov 2022 15:59:41 +0100 Subject: [PATCH] Fix empty cell MD table render bug --- plugs/markdown/markdown_render.ts | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/plugs/markdown/markdown_render.ts b/plugs/markdown/markdown_render.ts index c6965d01..6a522021 100644 --- a/plugs/markdown/markdown_render.ts +++ b/plugs/markdown/markdown_render.ts @@ -335,11 +335,33 @@ function render( name: "td", body: cleanTags(mapRender(t.children!)), }; - case "TableRow": + case "TableRow": { + const children = t.children!; + const newChildren: ParseTree[] = []; + // Ensure there is TableCell in between every delimiter + let lookingForCell = false; + for (const child of children) { + if (child.type === "TableDelimiter" && lookingForCell) { + // We were looking for a cell, but didn't fine one: empty cell! + // Let's inject an empty one + newChildren.push({ + type: "TableCell", + children: [], + }); + } + if (child.type === "TableDelimiter") { + lookingForCell = true; + } + if (child.type === "TableCell") { + lookingForCell = false; + } + newChildren.push(child); + } return { name: "tr", - body: cleanTags(mapRender(t.children!)), + body: cleanTags(mapRender(newChildren)), }; + } // Text case undefined: return t.text!;