From d87d46d9d03171bb91a4fb1b62f1f865dcced120 Mon Sep 17 00:00:00 2001 From: interlope Date: Wed, 3 Jun 2026 13:14:20 +0200 Subject: [PATCH] feat: clean up trailing empty rows and whitespace in highlighted code blocks --- js/blowdit.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/js/blowdit.js b/js/blowdit.js index f4bae9d..eb6495d 100644 --- a/js/blowdit.js +++ b/js/blowdit.js @@ -814,6 +814,27 @@ code.innerHTML = code.innerHTML.replace(/\n+$/, ''); try { window.hljs.lineNumbersBlock(code, { singleLine: false }); } catch (e) {} }); + // The plugin builds the table on a 0ms timer; clean up afterwards: + // remove any trailing empty row and trailing whitespace nodes that + // would leave a blank "phantom" line at the bottom of the block. + setTimeout(function () { + Array.prototype.forEach.call(codes, function (code) { + var rows = code.querySelectorAll('.hljs-ln tr'); + if (rows.length) { + var last = rows[rows.length - 1]; + var cell = last.querySelector('.hljs-ln-code'); + if (cell && cell.textContent.replace(/\s+/g, '') === '') { + last.parentNode.removeChild(last); + } + } + var n = code.lastChild; + while (n && n.nodeType === 3 && !n.textContent.trim()) { + var prev = n.previousSibling; + code.removeChild(n); + n = prev; + } + }); + }, 60); }; document.head.appendChild(ln); };